|
1 | 1 | defmodule CodeCorpsWeb.ProjectGithubRepoController do |
2 | 2 | use CodeCorpsWeb, :controller |
3 | | - use JaResource |
4 | 3 |
|
5 | | - import CodeCorps.Helpers.Query, only: [id_filter: 2] |
| 4 | + alias CodeCorps.{ProjectGithubRepo, User, Helpers.Query} |
6 | 5 |
|
7 | | - alias CodeCorps.ProjectGithubRepo |
| 6 | + action_fallback CodeCorpsWeb.FallbackController |
| 7 | + plug CodeCorpsWeb.Plug.DataToAttributes |
| 8 | + plug CodeCorpsWeb.Plug.IdsToIntegers |
8 | 9 |
|
9 | | - plug :load_resource, model: ProjectGithubRepo, only: [:show], preload: [:github_repo, :project] |
10 | | - plug :load_and_authorize_changeset, model: ProjectGithubRepo, only: [:create] |
11 | | - plug :load_and_authorize_resource, model: ProjectGithubRepo, only: [:delete] |
12 | | - plug JaResource |
| 10 | + @spec index(Conn.t, map) :: Conn.t |
| 11 | + def index(%Conn{} = conn, %{} = params) do |
| 12 | + with organization_installations <- ProjectGithubRepo |> Query.id_filter(params) |> Repo.all do |
| 13 | + conn |> render("index.json-api", data: organization_installations) |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + @spec show(Conn.t, map) :: Conn.t |
| 18 | + def show(%Conn{} = conn, %{"id" => id}) do |
| 19 | + with %ProjectGithubRepo{} = project_github_repo <- ProjectGithubRepo |> Repo.get(id) do |
| 20 | + conn |> render("show.json-api", data: project_github_repo) |
| 21 | + end |
| 22 | + end |
13 | 23 |
|
14 | | - @spec model :: module |
15 | | - def model, do: CodeCorps.ProjectGithubRepo |
| 24 | + @spec create(Plug.Conn.t, map) :: Conn.t |
| 25 | + def create(%Conn{} = conn, %{} = params) do |
| 26 | + with %User{} = current_user <- conn |> Guardian.Plug.current_resource, |
| 27 | + {:ok, :authorized} <- current_user |> Policy.authorize(:create, %ProjectGithubRepo{}, params), |
| 28 | + {:ok, %ProjectGithubRepo{} = project_github_repo} <- create_project_repo_changeset(params) |> Repo.insert do |
| 29 | + conn |> put_status(:created) |> render("show.json-api", data: project_github_repo) |
| 30 | + end |
| 31 | + end |
16 | 32 |
|
17 | | - def filter(_conn, query, "id", id_list) do |
18 | | - query |> id_filter(id_list) |
| 33 | + @spec delete(Plug.Conn.t, map) :: Conn.t |
| 34 | + def delete(%Conn{} = conn, %{"id" => id} = params) do |
| 35 | + with %ProjectGithubRepo{} = project_github_repo <- ProjectGithubRepo |> Repo.get(id), |
| 36 | + %User{} = current_user <- conn |> Guardian.Plug.current_resource, |
| 37 | + {:ok, :authorized} <- current_user |> Policy.authorize(:delete, project_github_repo, params), |
| 38 | + {:ok, _project_github_repo} <- project_github_repo |> Repo.delete do |
| 39 | + conn |> send_resp(:no_content, "") |
| 40 | + end |
19 | 41 | end |
20 | 42 |
|
21 | | - def handle_create(_conn, attributes) do |
22 | | - %ProjectGithubRepo{} |> ProjectGithubRepo.create_changeset(attributes) |
| 43 | + @spec create_project_repo_changeset(map) :: Ecto.Changeset.t |
| 44 | + defp create_project_repo_changeset(params) do |
| 45 | + %ProjectGithubRepo{} |
| 46 | + |> ProjectGithubRepo.create_changeset(params) |
23 | 47 | end |
24 | 48 | end |
0 commit comments