|
1 | 1 | defmodule CodeCorpsWeb.DonationGoalController do |
2 | 2 | use CodeCorpsWeb, :controller |
3 | | - use JaResource |
4 | 3 |
|
5 | | - import CodeCorps.Helpers.Query, only: [id_filter: 2] |
6 | | - |
7 | | - alias CodeCorps.DonationGoal |
8 | 4 | alias CodeCorps.Services.DonationGoalsService |
| 5 | + alias CodeCorps.{DonationGoal, User, Helpers.Query} |
9 | 6 |
|
10 | | - plug :load_and_authorize_changeset, model: DonationGoal, only: [:create] |
11 | | - plug :load_and_authorize_resource, model: DonationGoal, only: [:update, :delete] |
12 | | - plug JaResource |
| 7 | + action_fallback CodeCorpsWeb.FallbackController |
| 8 | + plug CodeCorpsWeb.Plug.DataToAttributes |
| 9 | + plug CodeCorpsWeb.Plug.IdsToIntegers |
13 | 10 |
|
14 | | - @spec model :: module |
15 | | - def model, do: CodeCorps.DonationGoal |
| 11 | + @spec index(Conn.t, map) :: Conn.t |
| 12 | + def index(%Conn{} = conn, %{} = params) do |
| 13 | + with donation_goals <- DonationGoal |> Query.id_filter(params) |> Repo.all do |
| 14 | + conn |> render("index.json-api", data: donation_goals) |
| 15 | + end |
| 16 | + end |
16 | 17 |
|
17 | | - def filter(_conn, query, "id", id_list), do: id_filter(query, id_list) |
| 18 | + @spec show(Conn.t, map) :: Conn.t |
| 19 | + def show(%Conn{} = conn, %{"id" => id}) do |
| 20 | + with %DonationGoal{} = donation_goal <- DonationGoal |> Repo.get(id) do |
| 21 | + conn |> render("show.json-api", data: donation_goal) |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + @spec create(Plug.Conn.t, map) :: Conn.t |
| 26 | + def create(%Conn{} = conn, %{} = params) do |
| 27 | + with %User{} = current_user <- conn |> Guardian.Plug.current_resource, |
| 28 | + {:ok, :authorized} <- current_user |> Policy.authorize(:create, %DonationGoal{}, params), |
| 29 | + {:ok, %DonationGoal{} = donation_goal} <- DonationGoalsService.create(params) do |
| 30 | + conn |> put_status(:created) |> render("show.json-api", data: donation_goal) |
| 31 | + end |
| 32 | + end |
18 | 33 |
|
19 | | - def handle_create(_conn, attributes) do |
20 | | - attributes |> DonationGoalsService.create |
| 34 | + @spec delete(Conn.t, map) :: Conn.t |
| 35 | + def delete(%Conn{} = conn, %{"id" => id} = _params) do |
| 36 | + with %DonationGoal{} = donation_goal <- DonationGoal |> Repo.get(id), |
| 37 | + %User{} = current_user <- conn |> Guardian.Plug.current_resource, |
| 38 | + {:ok, :authorized} <- current_user |> Policy.authorize(:delete, donation_goal), |
| 39 | + {:ok, %DonationGoal{} = _donation_goal} <- donation_goal |> Repo.delete |
| 40 | + do |
| 41 | + conn |> Conn.assign(:donation_goal, donation_goal) |> send_resp(:no_content, "") |
| 42 | + end |
21 | 43 | end |
22 | 44 |
|
23 | | - def handle_update(_conn, record, attributes) do |
24 | | - record |> DonationGoalsService.update(attributes) |
| 45 | + @spec update(Conn.t, map) :: Conn.t |
| 46 | + def update(%Conn{} = conn, %{"id" => id} = params) do |
| 47 | + with %DonationGoal{} = donation_goal <- DonationGoal |> Repo.get(id), |
| 48 | + %User{} = current_user <- conn |> Guardian.Plug.current_resource, |
| 49 | + {:ok, :authorized} <- current_user |> Policy.authorize(:update, donation_goal), |
| 50 | + {:ok, %DonationGoal{} = updated_donation_goal} <- donation_goal |> DonationGoalsService.update(params) do |
| 51 | + conn |> render("show.json-api", data: updated_donation_goal) |
| 52 | + end |
25 | 53 | end |
26 | 54 | end |
0 commit comments