Skip to content

Commit 3b51807

Browse files
Remove JaResource from DonationGoalController
1 parent 13de440 commit 3b51807

File tree

3 files changed

+54
-26
lines changed

3 files changed

+54
-26
lines changed

lib/code_corps/policy/donation_goal.ex

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ defmodule CodeCorps.Policy.DonationGoal do
33
import CodeCorps.Policy.Helpers, only: [get_project: 1, owned_by?: 2]
44

55
alias CodeCorps.{DonationGoal, User}
6-
alias Ecto.Changeset
76

8-
def create?(%User{} = user, %Changeset{} = changeset),
9-
do: changeset |> get_project |> owned_by?(user)
7+
@spec create?(User.t, map) :: boolean
8+
def create?(%User{} = user, %{} = params),
9+
do: params |> get_project |> owned_by?(user)
1010

11-
def update?(%User{} = user, %DonationGoal{} = donation_goal), do:
12-
donation_goal |> get_project |> owned_by?(user)
11+
@spec update?(User.t, DonationGoal.t) :: boolean
12+
def update?(%User{} = user, %DonationGoal{} = donation_goal),
13+
do: donation_goal |> get_project |> owned_by?(user)
1314

14-
def delete?(%User{} = user, %DonationGoal{} = donation_goal), do:
15-
donation_goal |> get_project |> owned_by?(user)
15+
@spec delete?(User.t, DonationGoal.t) :: boolean
16+
def delete?(%User{} = user, %DonationGoal{} = donation_goal),
17+
do: donation_goal |> get_project |> owned_by?(user)
1618
end

lib/code_corps/policy/policy.ex

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ defmodule CodeCorps.Policy do
2828
defp can?(%User{} = current_user, :update, %Category{}, %{}), do: Policy.Category.update?(current_user)
2929
defp can?(%User{} = current_user, :create, %Comment{}, %{} = params), do: Policy.Comment.create?(current_user, params)
3030
defp can?(%User{} = current_user, :update, %Comment{} = comment, %{}), do: Policy.Comment.update?(current_user, comment)
31+
defp can?(%User{} = current_user, :create, %DonationGoal{}, %{} = params), do: Policy.DonationGoal.create?(current_user, params)
32+
defp can?(%User{} = current_user, :update, %DonationGoal{} = donation_goal, %{}), do: Policy.DonationGoal.update?(current_user, donation_goal)
33+
defp can?(%User{} = current_user, :delete, %DonationGoal{} = donation_goal, %{}), do: Policy.DonationGoal.delete?(current_user, donation_goal)
3134
defp can?(%User{} = current_user, :create, %Organization{}, %{}), do: Policy.Organization.create?(current_user)
3235
defp can?(%User{} = current_user, :update, %Organization{} = organization, %{}), do: Policy.Organization.update?(current_user, organization)
3336
defp can?(%User{} = current_user, :update, %User{} = user, %{}), do: Policy.User.update?(current_user, user)
@@ -100,11 +103,6 @@ defmodule CodeCorps.Policy do
100103
# NOTE: other tests are using the User policy for the time being.
101104
def can?(%User{}, _action, nil), do: true
102105

103-
def can?(%User{} = user, :create, %Changeset{data: %DonationGoal{}} = changeset), do: Policy.DonationGoal.create?(user, changeset)
104-
def can?(%User{} = user, :update, %DonationGoal{} = comment), do: Policy.DonationGoal.update?(user, comment)
105-
def can?(%User{} = user, :delete, %DonationGoal{} = comment), do: Policy.DonationGoal.delete?(user, comment)
106-
107-
108106
def can?(%User{} = user, :create, Role), do: Policy.Role.create?(user)
109107
end
110108
end
Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,54 @@
11
defmodule CodeCorpsWeb.DonationGoalController do
22
use CodeCorpsWeb, :controller
3-
use JaResource
43

5-
import CodeCorps.Helpers.Query, only: [id_filter: 2]
6-
7-
alias CodeCorps.DonationGoal
84
alias CodeCorps.Services.DonationGoalsService
5+
alias CodeCorps.{DonationGoal, User, Helpers.Query}
96

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
1310

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
1617

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
1833

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
2143
end
2244

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
2553
end
2654
end

0 commit comments

Comments
 (0)