-
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.
Merge pull request #1068 from landongrindheim/remove-ja-resource-and-…
…canary Removed ja_resource and canary from Donation Goal controller Removed ja_resource and canary from project Alphabetized clauses in policy.ex
- Loading branch information
Showing
9 changed files
with
115 additions
and
195 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
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
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
56 changes: 42 additions & 14 deletions
56
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,54 @@ | ||
defmodule CodeCorpsWeb.DonationGoalController do | ||
use CodeCorpsWeb, :controller | ||
use JaResource | ||
|
||
import CodeCorps.Helpers.Query, only: [id_filter: 2] | ||
|
||
alias CodeCorps.DonationGoal | ||
alias CodeCorps.Services.DonationGoalsService | ||
alias CodeCorps.{DonationGoal, User, Helpers.Query} | ||
|
||
plug :load_and_authorize_changeset, model: DonationGoal, only: [:create] | ||
plug :load_and_authorize_resource, model: DonationGoal, only: [:update, :delete] | ||
plug JaResource | ||
action_fallback CodeCorpsWeb.FallbackController | ||
plug CodeCorpsWeb.Plug.DataToAttributes | ||
plug CodeCorpsWeb.Plug.IdsToIntegers | ||
|
||
@spec model :: module | ||
def model, do: CodeCorps.DonationGoal | ||
@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 filter(_conn, query, "id", id_list), do: id_filter(query, id_list) | ||
@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} <- DonationGoalsService.create(params) do | ||
conn |> put_status(:created) |> render("show.json-api", data: donation_goal) | ||
end | ||
end | ||
|
||
def handle_create(_conn, attributes) do | ||
attributes |> DonationGoalsService.create | ||
@spec delete(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), | ||
{:ok, %DonationGoal{} = _donation_goal} <- donation_goal |> Repo.delete | ||
do | ||
conn |> Conn.assign(:donation_goal, donation_goal) |> send_resp(:no_content, "") | ||
end | ||
end | ||
|
||
def handle_update(_conn, record, attributes) do | ||
record |> DonationGoalsService.update(attributes) | ||
@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{} = updated_donation_goal} <- donation_goal |> DonationGoalsService.update(params) do | ||
conn |> render("show.json-api", data: updated_donation_goal) | ||
end | ||
end | ||
end |
Oops, something went wrong.