[day-02] add context to create/get a user

Create context module to create and fetch a user.
This commit is contained in:
Joao P Dubas 2021-06-22 23:57:10 +00:00
parent d578bc6cf3
commit 3709cd265a
2 changed files with 29 additions and 0 deletions

View File

@ -0,0 +1,9 @@
defmodule Wabanex.Users.Create do
alias Wabanex.{Repo, User}
def call(params) do
params
|> User.changeset()
|> Repo.insert()
end
end

20
lib/wabanex/users/get.ex Normal file
View File

@ -0,0 +1,20 @@
defmodule Wabanex.Users.Get do
alias Wabanex.{Repo, User}
def call(id) do
id
|> Ecto.UUID.cast()
|> handle_result()
end
defp handle_result(:error), do: {:error, "Invalid id format"}
defp handle_result({:ok, uuid}) do
User
|> Repo.get(uuid)
|> case do
nil -> {:error, "User not found"}
user -> {:ok, user}
end
end
end