-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial changes for removing ja_resource from donation_goal_controller
- Loading branch information
1 parent
b60d819
commit 61ec19b
Showing
2 changed files
with
50 additions
and
12 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
52 changes: 45 additions & 7 deletions
52
lib/code_corps_web/controllers/donation_goal_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,26 +1,64 @@ | ||
defmodule CodeCorpsWeb.DonationGoalController do | ||
use CodeCorpsWeb, :controller | ||
use JaResource | ||
|
||
import CodeCorps.Helpers.Query, only: [id_filter: 2] | ||
|
||
alias CodeCorps.DonationGoal | ||
alias CodeCorps.{DonationGoal, User, Helpers.Query} | ||
alias CodeCorps.Services.DonationGoalsService | ||
|
||
action_fallback CodeCorpsWeb.FallbackController | ||
plug :load_and_authorize_changeset, model: DonationGoal, only: [:create] | ||
plug :load_and_authorize_resource, model: DonationGoal, only: [:update, :delete] | ||
plug JaResource | ||
plug CodeCorpsWeb.Plug.DataToAttributes | ||
plug CodeCorpsWeb.Plug.IdsToIntegers | ||
|
||
@spec model :: module | ||
def model, do: CodeCorps.DonationGoal | ||
|
||
def filter(_conn, query, "id", id_list), do: id_filter(query, id_list) | ||
|
||
def handle_create(_conn, attributes) do | ||
attributes |> DonationGoalsService.create | ||
@spec index(Conn.t, map) :: Conn.t | ||
def index(%Conn{} = conn, %{} = params) do | ||
with donation_goals <- DonationGoal |> Query.id_filter(params) |> Repo.all do | ||
conn |> render("index.json-api", data: donation_goals) | ||
end | ||
end | ||
|
||
def handle_update(_conn, record, attributes) do | ||
record |> DonationGoalsService.update(attributes) | ||
@spec show(Conn.t, map) :: Conn.t | ||
def show(%Conn{} = conn, %{"id" => id}) do | ||
with %DonationGoal{} = donation_goal <- DonationGoal |> Repo.get(id) do | ||
conn |> render("show.json-api", data: donation_goal) | ||
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, %DonationGoal{}, params), | ||
{:ok, %DonationGoal{} = donation_goal} <- %DonationGoal{} |> DonationGoal.create_changeset(params) |> Repo.insert do | ||
conn |> put_status(:created) |> render("show.json-api", data: donation_goal) | ||
end | ||
end | ||
|
||
@spec update(Conn.t, map) :: Conn.t | ||
def update(%Conn{} = conn, %{"id" => id} = params) do | ||
with %DonationGoal{} = donation_goal <- DonationGoal |> Repo.get(id), | ||
%User{} = current_user <- conn |> Guardian.Plug.current_resource, | ||
{:ok, :authorized} <- current_user |> Policy.authorize(:update, donation_goal), | ||
{:ok, %DonationGoal{} = donation_goal} <- donation_goal |> DonationGoal.changeset(params) |> Repo.update do | ||
conn |> render("show.json-api", data: donation_goal) | ||
end | ||
end | ||
|
||
@spec delete(Plug.Conn.t, map) :: Conn.t | ||
def delete(%Conn{} = conn, %{"id" => id} = params) do | ||
with %DonationGoal{} = donation_goal <- DonationGoal |> Repo.get(id), | ||
%User{} = current_user <- conn |> Guardian.Plug.current_resource, | ||
{:ok, :authorized} <- current_user |> Policy.authorize(:delete, donation_goal, params), | ||
{:ok, _donation_goal} <- | ||
donation_goal | ||
|> Repo.delete do | ||
conn |> send_resp(:no_content, "") | ||
end | ||
end | ||
end |