|
1 | 1 | defmodule CodeCorpsWeb.GithubAppInstallationController do |
2 | 2 | use CodeCorpsWeb, :controller |
3 | | - use JaResource |
4 | 3 |
|
5 | 4 | import CodeCorps.Helpers.Query, only: [id_filter: 2] |
6 | 5 |
|
7 | | - alias CodeCorps.{GithubAppInstallation} |
| 6 | + alias CodeCorps.{GithubAppInstallation, User} |
8 | 7 |
|
9 | | - @preloads [:project, :user] |
| 8 | + action_fallback CodeCorpsWeb.FallbackController |
| 9 | + plug CodeCorpsWeb.Plug.DataToAttributes |
| 10 | + plug CodeCorpsWeb.Plug.IdsToIntegers |
10 | 11 |
|
11 | | - plug :load_resource, model: GithubAppInstallation, only: [:show], preload: @preloads |
12 | | - plug :load_and_authorize_changeset, model: GithubAppInstallation, only: [:create], preload: @preloads |
13 | | - |
14 | | - plug JaResource |
15 | | - |
16 | | - @spec model :: module |
17 | | - def model, do: CodeCorps.GithubAppInstallation |
| 12 | + @spec index(Conn.t, map) :: Conn.t |
| 13 | + def index(%Conn{} = conn, %{} = params) do |
| 14 | + with installations <- GithubAppInstallation |> id_filter(params) |> Repo.all do |
| 15 | + conn |> render("index.json-api", data: installations) |
| 16 | + end |
| 17 | + end |
18 | 18 |
|
19 | | - @spec filter(Plug.Conn.t, Ecto.Query.t, String.t, String.t) :: Ecto.Query.t |
20 | | - def filter(_conn, query, "id", id_list) do |
21 | | - query |> id_filter(id_list) |
| 19 | + @spec show(Conn.t, map) :: Conn.t |
| 20 | + def show(%Conn{} = conn, %{"id" => id}) do |
| 21 | + with %GithubAppInstallation{} = installation <- GithubAppInstallation |> Repo.get(id) do |
| 22 | + conn |> render("show.json-api", data: installation) |
| 23 | + end |
22 | 24 | end |
23 | 25 |
|
24 | | - @spec handle_create(Plug.Conn.t, map) :: Ecto.Changeset.t |
25 | | - def handle_create(_conn, attributes) do |
26 | | - %GithubAppInstallation{} |
27 | | - |> GithubAppInstallation.create_changeset(attributes) |
| 26 | + @spec create(Plug.Conn.t, map) :: Conn.t |
| 27 | + def create(%Conn{} = conn, %{} = params) do |
| 28 | + with %User{} = current_user <- conn |> Guardian.Plug.current_resource, |
| 29 | + {:ok, :authorized} <- current_user |> Policy.authorize(:create, %GithubAppInstallation{}, params), |
| 30 | + {:ok, %GithubAppInstallation{} = installation} <- %GithubAppInstallation{} |> GithubAppInstallation.create_changeset(params) |> Repo.insert do |
| 31 | + conn |> put_status(:created) |> render("show.json-api", data: installation) |
| 32 | + end |
28 | 33 | end |
29 | 34 | end |
0 commit comments