Skip to content

Commit

Permalink
Add resolver action to update a budget
Browse files Browse the repository at this point in the history
Add schema types for budget update input
Add tests for updateBudget mutation
  • Loading branch information
zacck committed Mar 28, 2018
1 parent bebd407 commit d1d97e6
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/spender_web/resolvers/owner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ defmodule SpenderWeb.Resolvers.Owner do
{:ok, budget}
end
end

def update_budget(_,%{input: args}, _) do
with {:ok, budget} <- MoneyLogs.get_budget(args.id),
{:ok, updated_budget} <- MoneyLogs.update_budget(budget, args) do
{:ok, updated_budget}
end
end
end
6 changes: 6 additions & 0 deletions lib/spender_web/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,11 @@ defmodule SpenderWeb.Schema do
middleware Middleware.Authorize, :any
resolve &Resolvers.Owner.create_budget/3
end

field :update_budget, :budget do
arg :input, non_null(:budget_update)
middleware Middleware.Authorize, :any
resolve &Resolvers.Owner.update_budget/3
end
end
end
17 changes: 15 additions & 2 deletions lib/spender_web/schema/money_log_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule SpenderWeb.Schema.MoneyLogTypes do

@desc "A Moneylog that belongs to a user"
object :budget do
field :id, non_null(:integer)
field :owner_id, :integer
field :amnt_in, :float
field :amnt_out, :float
Expand All @@ -24,8 +25,20 @@ defmodule SpenderWeb.Schema.MoneyLogTypes do
@desc "Inputs for a MoneyLog"
input_object :budget_input do
field :name, non_null(:string)
field :id, :integer
field :start_date, :date
field :end_date, :date
field :end_date, :date
end

@desc "Update object for a moneylog"
input_object :budget_update do
field :id, non_null(:integer)
field :owner_id, :integer
field :amnt_in, :float
field :amnt_out, :float
field :end_date, :date
field :is_active, :boolean
field :name, non_null(:string)
field :refined, :boolean
field :start_date, :date
end
end
69 changes: 69 additions & 0 deletions test/lib/spender_web/resolvers/owner_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,74 @@ defmodule SpenderWeb.Resolvers.OwnerTest do
}

end

@tag :authenticated
test "update_budget errors out when budget doesn't exist", %{conn: conn} do
variables = %{
"input" => %{
"id" => 50,
"name" => "Food Lovers",
"startDate" => "2018-07-12",
"endDate" => "2018-07-20"
}
}

query = """
mutation($input: BudgetUpdate!) {
updateBudget(input: $input) {
name
id
}
}
"""

assert Repo.aggregate(Budget, :count, :id) == 0

conn = post conn, "/graphiql", query: query, variables: variables
assert Repo.aggregate(Budget, :count, :id) == 0

%{
"errors" => [error]
} = json_response(conn, 200)

assert error["message"] == "budget not found"
end

@tag :authenticated
test "update_budget with new details", %{conn: conn, current_user: user} do
owner = insert(:owner, user: user)
budget = insert(:budget, owner: owner)
variables = %{
"input" => %{
"id" => budget.id,
"name" => "Food Lovers",
"startDate" => "2018-07-12",
"endDate" => "2018-07-20"
}
}

query = """
mutation($input: BudgetUpdate!) {
updateBudget(input: $input) {
name
id
}
}
"""

assert Repo.aggregate(Budget, :count, :id) == 1

conn = post conn, "/graphiql", query: query, variables: variables
assert Repo.aggregate(Budget, :count, :id) == 1

%{
"data" => %{
"updateBudget" => updatedBudget
}
} = json_response(conn, 200)

assert updatedBudget["name"] == variables["input"]["name"]
assert updatedBudget["id"] == budget.id
end
end
end

0 comments on commit d1d97e6

Please sign in to comment.