Hi everyone.
I am new to start programming with Elixir and Phoenix. I have a problem with with-do
statements when control my actions flow.
Like this
with {:ok, %Product{} = product} <- Sales.create_product(attrs),
{:ok, %ProductVariation{} = product_variation} <- Sales.create_first_product_variation(product),
{:ok, _} <- Sales.create_link_between_product_and_category(product, category_id) do
conn
|> put_status(:created)
|> json(%{success: true, product: product})
end
What should I do if the second or the third pattern is not matched? The product have an unique field then user can not recreate it with the same info. Should I delete the product when creating product variation is failed? or there is a better way to implement it? Can I init a bunch of data for user with with-do
statement? Eg create account
-> create subscription
-> create inventory
…etc…
Sorry for many questions
Thank you.