forked from code-corps/code-corps-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch GithubAppInstallationController away from ja_resource and canary
Helps support code-corps#864 Closes code-corps#890
- Loading branch information
Showing
3 changed files
with
38 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 35 additions & 15 deletions
50
lib/code_corps_web/controllers/github_app_installation_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,49 @@ | ||
defmodule CodeCorpsWeb.GithubAppInstallationController do | ||
use CodeCorpsWeb, :controller | ||
use JaResource | ||
|
||
import CodeCorps.Helpers.Query, only: [id_filter: 2] | ||
|
||
alias CodeCorps.{GithubAppInstallation} | ||
alias CodeCorps.{GithubAppInstallation, User} | ||
|
||
@preloads [:project, :user] | ||
|
||
plug :load_resource, model: GithubAppInstallation, only: [:show], preload: @preloads | ||
plug :load_and_authorize_changeset, model: GithubAppInstallation, only: [:create], preload: @preloads | ||
|
||
plug JaResource | ||
action_fallback CodeCorpsWeb.FallbackController | ||
plug CodeCorpsWeb.Plug.DataToAttributes | ||
plug CodeCorpsWeb.Plug.IdsToIntegers | ||
|
||
@spec model :: module | ||
def model, do: CodeCorps.GithubAppInstallation | ||
|
||
@spec filter(Plug.Conn.t, Ecto.Query.t, String.t, String.t) :: Ecto.Query.t | ||
def filter(_conn, query, "id", id_list) do | ||
query |> id_filter(id_list) | ||
@spec index(Conn.t, map) :: Conn.t | ||
def index(%Conn{} = conn, %{} = params) do | ||
with installations <- GithubAppInstallation |> id_filter(params) |> Repo.all do | ||
conn |> render("index.json-api", data: installations) | ||
end | ||
end | ||
|
||
@spec show(Conn.t, map) :: Conn.t | ||
def show(%Conn{} = conn, %{"id" => id}) do | ||
with %GithubAppInstallation{} = installation <- GithubAppInstallation |> Repo.get(id) do | ||
conn |> render("show.json-api", data: installation) | ||
end | ||
end | ||
|
||
@spec create(Plug.Conn.t, map) :: Conn.t | ||
def create(%Conn{} = conn, %{} = params) do | ||
with %User{} = current_user <- conn |> Guardian.Plug.current_resource, | ||
{:ok, :authorized} <- current_user |> Policy.authorize(:create, %GithubAppInstallation{}, params), | ||
{:ok, %GithubAppInstallation{} = installation} <- %GithubAppInstallation{} |> GithubAppInstallation.create_changeset(params) |> Repo.insert do | ||
conn |> put_status(:created) |> render("show.json-api", data: installation) | ||
end | ||
end | ||
|
||
@spec handle_create(Plug.Conn.t, map) :: Ecto.Changeset.t | ||
def handle_create(_conn, attributes) do | ||
%GithubAppInstallation{} | ||
|> GithubAppInstallation.create_changeset(attributes) | ||
@spec delete(Plug.Conn.t, map) :: Conn.t | ||
def delete(%Conn{} = conn, %{"id" => id} = params) do | ||
with %GithubAppInstallation{} = installation <- GithubAppInstallation |> Repo.get(id), | ||
%User{} = current_user <- conn |> Guardian.Plug.current_resource, | ||
{:ok, :authorized} <- current_user |> Policy.authorize(:delete, installation, params), | ||
{:ok, _installation} <- | ||
installation | ||
|> Repo.delete do | ||
conn |> send_resp(:no_content, "") | ||
end | ||
end | ||
end |