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 eb0f122
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
29 changes: 15 additions & 14 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 All @@ -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
6 changes: 4 additions & 2 deletions test/controllers/donation_goal_controller_test.exs
Original file line number Diff line number Diff line change
@@ -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
Expand Down 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
14 changes: 10 additions & 4 deletions web/models/donation_goal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

0 comments on commit eb0f122

Please sign in to comment.