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
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
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
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
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir
iex(2)...
New
Other popular topics
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New







