-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7aefa05
commit 9641d01
Showing
6 changed files
with
100 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
defmodule Rocketpay do | ||
alias Rocketpay.Users.Create, as: UserCreate | ||
|
||
alias Rocketpay.Accouns.Deposit | ||
|
||
defdelegate create_user(params), to: UserCreate, as: :call | ||
|
||
defdelegate deposit(params), to: Deposit, as: :call | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
defmodule Rocketpay.Accounts.Deposit do | ||
alias Ecto.Multi | ||
|
||
alias Rocketpay.{Account, Repo} | ||
|
||
def call(%{"id" => id, "value" => value}) do | ||
Multi.new() | ||
|> Multi.run(:account, fn repo, _changes -> get_account(repo, id) end) | ||
|> Multi.run(:update_balance, fn repo, %{account: account} -> | ||
update_balance(repo, account, value) | ||
end) | ||
end | ||
|
||
defp get_account(repo, id) do | ||
case repo.get(Account, id) do | ||
nil -> {:error, "Account, not found!"} | ||
account -> {:ok, account} | ||
end | ||
end | ||
|
||
defp update_balance(repo, account, value) do | ||
account | ||
|> sum_values(value) | ||
|> update_account(repo) | ||
end | ||
|
||
defp sum_values(%Account{balance: balance}, value) do | ||
value | ||
|>Decimal.cast() | ||
|> handle_cast(balance) | ||
end | ||
|
||
defp handle_cast({:ok, value}, balance), do: Decimal.add(value, balance) | ||
defp handle_cast(:error, _balanace), do: {:error, "Invalid deposit value!"} | ||
|
||
defp update_account({:error, _reason} = error, _repo), do: error | ||
|
||
defp update_account(value, repo) do | ||
params = %{balance: value} | ||
|
||
params | ||
|> Account.changeset() | ||
|> repo.update() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule RocketpayWeb.AccountsController do | ||
use RocketpayWeb, :controller | ||
|
||
alias Rocketpay.Account | ||
|
||
action_fallback RocketpayWeb.FallbackController | ||
|
||
def deposit(conn, params) do | ||
with {:ok, %Account{} = account} <- Rocketpay.deposit(params) do | ||
conn | ||
|> put_status(:ok) | ||
|> render("update.json", account: account) | ||
end | ||
end | ||
|
||
def withdraw(conn, params) do | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters