I have been following this tutorial (https://lobotuerto.com/blog/building-a-json-api-in-elixir-with-phoenix/) to make my first JSON elixir app using Phoenix. I am doing so to create a mock banking app where users can create a profile with a starting amount and then deposit and transfer funds across to other accounts.
I am set to make a lot more functions but this is all new to me and I wasn’t able to find any decent tutorials that might help me - so I thought these two basic functions would be a good learning exercise if someone is willing to explain this to me?
I am using the pre-made create function to make the deposit one as shown below:
def create(conn, %{"user" => user_params}) do
with {:ok, %User{} = user} <- Account.create_user(user_params) do
conn
|> put_status(:created)
|> put_resp_header("location", Routes.user_path(conn, :show, user))
|> render("show.json", user: user)
end
end
def deposit(conn, %{"id" => id, "amount" => amount}) do
user = Account.get_user!(id)
with {:ok, %User{} = user} <- Account.deposit(user, amount) do
render(conn, "show.json", user: user)
end
end
Is that right so far?
Then in the account.ex file, I am wondering how I will be able to ‘add’ the current amount to the deposited amount:
def create_user(attrs \\ %{}) do
%User{}
|> User.changeset(attrs)
|> Repo.insert()
end
"""
def deposit(%User{} = user, attrs) do
user
|> User.changeset(attrs)
|> Repo.update()
end
Finally, this is how the schema is storing information as returned from a GET on the localhost:4000/api/users request:
{"data":[{"amount":200.5,"id":1,"name":"Tester"},{"amount":0.0,"id":2,"name":"Arisha Barron"},{"amount":0.0,"id":3,"name":"Branden Gibson"},{"amount":0.0,"id":4,"name":"Rhonda Church"},{"amount":0.0,"id":5,"name":"Georgina Hazel"},{"amount":5.0,"id":6,"name":"test2"}]}
Really quite confused on this so any help would be muchly appreciated!