Fresco_Bits
Need Help how to refactor this code with Elixir to be more readable and to avoid nested cases
def update( %{
full_name: full_name,
password: password,
password_confirmation: password_confirmation,
email: email,
language: language,
time_zone: time_zone
},
%{context: %{user: %{uid: user_id}}}
) do
user = User |> Repo.get(user_id)
verif_email = user.email === email
case verif_email do
true ->
case AccountClient.account_update(
user_id,
full_name,
password,
email,
language,
time_zone,
password_confirmation
) do
{
:ok,
%{
data: %{
attributes: %{
full_name: full_name,
email: email
},
id: uid
}
}
} ->
{:ok, User.update!(uid, email, full_name)}
err ->
err
end
false ->
with false <- User.check_email(email),
{
:ok,
%{
data: %{
attributes: %{
full_name: full_name,
email: email
},
id: uid
}
}
} <-
AccountClient.account_update(
user_id,
full_name,
password,
email,
language,
time_zone,
password_confirmation
) do
{:ok, User.update!(uid, email, full_name)}
else
true ->
{:error, %{message: "Email has been already taken .."}}
err ->
err
end
end
end
Marked As Solved
LostKobrakai
You want to separate out the various kinds of conditions you’re checking here.
- Checking for the users email: is is the users email, is it another and unused, is it another, but already used?
- Was the account update successful
- Updating the user based on the account update.
def update(map, %{context: %{user: %{uid: user_id}}} ) do
user = User |> Repo.get(user_id)
cond do
user.email === email -> update_account(map, user_id)
!User.check_email(email) -> update_account(map, user_id)
true -> {:error, %{message: "Email has been already taken .."}}
end
end
defp update_account(map, user_id) do
case account_update(map, user_id) do
{:ok, account_result} -> {:ok, user_update(account_result)}
err -> err
end
end
defp account_update(map, user_id) do
AccountClient.account_update(
user_id,
map.full_name,
map.password,
map.email,
map.language,
map.time_zone,
map.password_confirmation
)
end
defp user_update(account_result) do
%{
data: %{
attributes: %{
full_name: full_name,
email: email
},
id: uid
}
} = account_result
User.update!(uid, email, full_name)
end
9
Also Liked
kokolegorille
Some considerations…
- Don’t use case with true/false… use if
- Try avoiding local var if not needed
verif_email = user.email === email
case verif_email do
#instead, try this
case user.email === email do...
# or much better
if user.email === email, do: ..., else:...
- Use with with nested conditions
I see You have used one… did You write this code?
2
Popular in Questions
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it.
I’m very interested in Elixir,...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size?
Thanks
New
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
Other popular topics
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New








