Skip to content

Commit

Permalink
Integrate manager into controller
Browse files Browse the repository at this point in the history
  • Loading branch information
begedin committed Nov 25, 2016
1 parent ea8697d commit 4cdb452
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
27 changes: 14 additions & 13 deletions lib/code_corps/donation_goals_manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion test/controllers/donation_goal_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions web/controllers/donation_goal_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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

0 comments on commit 4cdb452

Please sign in to comment.