From eb0f122140503ecede95dfb30e4bf4aa576157f1 Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Fri, 25 Nov 2016 15:14:23 +0100 Subject: [PATCH] Integrate manager into controller --- lib/code_corps/donation_goals_manager.ex | 29 ++++++++++--------- .../donation_goal_controller_test.exs | 6 ++-- web/controllers/donation_goal_controller.ex | 9 +++--- web/models/donation_goal.ex | 14 ++++++--- 4 files changed, 33 insertions(+), 25 deletions(-) diff --git a/lib/code_corps/donation_goals_manager.ex b/lib/code_corps/donation_goals_manager.ex index f238f5248..8dca48900 100644 --- a/lib/code_corps/donation_goals_manager.ex +++ b/lib/code_corps/donation_goals_manager.ex @@ -72,21 +72,22 @@ defmodule CodeCorps.DonationGoalsManager do # subscriptions relate to projects instead of plans # and by caching the total amount on the project itself - %CodeCorps.StripeConnectPlan{id: plan_id} = - CodeCorps.StripeConnectPlan - |> Repo.get_by(project_id: project_id) - - total_donated = - CodeCorps.StripeConnectSubscription - |> where([s], s.stripe_connect_plan_id == ^plan_id) - |> Repo.aggregate(:sum, :quantity) - - case total_donated do - nil -> 0 - total_donated -> total_donated - end + CodeCorps.StripeConnectPlan + |> Repo.get_by(project_id: project_id) + |> aggregate_donations + |> default_to_zero + end + + defp aggregate_donations(nil), do: 0 + defp aggregate_donations(%CodeCorps.StripeConnectPlan{id: plan_id}) do + CodeCorps.StripeConnectSubscription + |> where([s], s.stripe_connect_plan_id == ^plan_id) + |> Repo.aggregate(:sum, :quantity) end + defp default_to_zero(nil), do: 0 + defp default_to_zero(value), do: value + defp find_smallest_not_yet_reached(%Project{id: project_id}, amount_donated) do DonationGoal |> where([d], d.project_id == ^project_id and d.amount > ^amount_donated) @@ -107,7 +108,7 @@ defmodule CodeCorps.DonationGoalsManager do defp set_to_current(nil), do: {:ok, nil} defp set_to_current(%DonationGoal{} = donation_goal) do donation_goal - |> DonationGoal.update_changeset(%{current: true}) + |> DonationGoal.set_current_changeset(%{current: true}) |> Repo.update end end diff --git a/test/controllers/donation_goal_controller_test.exs b/test/controllers/donation_goal_controller_test.exs index 5869032dc..7b55a7bf9 100644 --- a/test/controllers/donation_goal_controller_test.exs +++ b/test/controllers/donation_goal_controller_test.exs @@ -1,7 +1,7 @@ defmodule CodeCorps.DonationGoalControllerTest do use CodeCorps.ApiCase, resource_name: :donation_goal - @valid_attrs %{amount: 200, current: false, description: "A description"} + @valid_attrs %{amount: 200, description: "A description"} @invalid_attrs %{description: nil} describe "index" do @@ -67,7 +67,9 @@ defmodule CodeCorps.DonationGoalControllerTest do describe "update" do @tag authenticated: :admin test "updates and renders chosen resource when data is valid", %{conn: conn} do - assert conn |> request_update(@valid_attrs) |> json_response(200) + project = insert(:project) + attrs = @valid_attrs |> Map.merge(%{project: project}) + assert conn |> request_update(attrs) |> json_response(200) end @tag authenticated: :admin diff --git a/web/controllers/donation_goal_controller.ex b/web/controllers/donation_goal_controller.ex index 62c1aee69..3d6b5603e 100644 --- a/web/controllers/donation_goal_controller.ex +++ b/web/controllers/donation_goal_controller.ex @@ -5,6 +5,7 @@ defmodule CodeCorps.DonationGoalController do import CodeCorps.Helpers.Query, only: [id_filter: 2] alias CodeCorps.DonationGoal + alias CodeCorps.DonationGoalsManager plug :load_and_authorize_changeset, model: DonationGoal, only: [:create] plug :load_and_authorize_resource, model: DonationGoal, only: [:update, :delete] @@ -13,14 +14,12 @@ defmodule CodeCorps.DonationGoalController do def filter(_conn, query, "id", id_list), do: id_filter(query, id_list) def handle_create(_conn, attributes) do - %DonationGoal{} - |> DonationGoal.create_changeset(attributes) - |> Repo.insert + attributes + |> DonationGoalsManager.create end def handle_update(_conn, record, attributes) do record - |> DonationGoal.update_changeset(attributes) - |> Repo.update + |> DonationGoalsManager.update(attributes) end end diff --git a/web/models/donation_goal.ex b/web/models/donation_goal.ex index 444274f8f..5122f319b 100644 --- a/web/models/donation_goal.ex +++ b/web/models/donation_goal.ex @@ -25,8 +25,8 @@ defmodule CodeCorps.DonationGoal do """ def create_changeset(struct, params \\ %{}) do struct - |> cast(params, [:amount, :current, :description, :project_id]) - |> validate_required([:amount, :current, :description, :project_id]) + |> cast(params, [:amount, :description, :project_id]) + |> validate_required([:amount, :description, :project_id]) |> assoc_constraint(:project) end @@ -35,7 +35,13 @@ defmodule CodeCorps.DonationGoal do """ def update_changeset(struct, params \\ %{}) do struct - |> cast(params, [:amount, :current, :description]) - |> validate_required([:amount, :current, :description]) + |> cast(params, [:amount, :description]) + |> validate_required([:amount, :description]) + end + + def set_current_changeset(struct, params \\ %{}) do + struct + |> cast(params, [:current]) + |> validate_required([:current]) end end