Goose97

Goose97

Hi everyone. I’m designing a web backend service in Elixir and having some problems. This question is not unique for Elixir language but I will post it here anyway to see if we have a solution written in Elixir.
Here’s my case:
Let’s say my business has some coupons that users can apply. To simplify, the coupon apply procedure can be broken down into two steps: validate the coupon and increase the claim counter. I would like to split the above two steps into two separate functions, each exposes a minimal interface (Eg: we can simply pass the coupon_id to the function). But doing so will result in two roundtrips to database to fetch the coupon struct and most of the time, the second one is unnecessary.
The solution that I have in my head now is to combined these two functions into a third one validate_and_claim. But that approach contains a lot of duplication code.
So is there any better approach to this problem? I know there won’t be a perfect on which is the best of both worlds but I’m willing to consider other solutions and their trade-offs.
Thanks and have a nice day!

Showing Posts 1 to 7

LostKobrakai

LostKobrakai

With Ecto.Multi you should be able to combine both of your initial function to the composed one without duplicating code.

Goose97

Goose97 OP

Thanks @LostKobrakai
I do a quick check. Is it what you meant?

Multi.new()
|> Multi.run(:validate_coupon, fn _repo, _ ->
  validate_coupon(coupon_id)
end)
|> Multi.run(:increase_counter, fn _repo, _ ->
  increase_counter(coupon_id)
end)
|> Repo.transaction()

I’m searching in the doc but it’s seems like there are still two roundtrips to the databases. Did i miss something?

NobbZ

NobbZ

A multi will not reduce roundtrips, it will just have them in a single transaction.

kokolegorille

kokolegorille

If You are validating coupon, why pass coupon_id? Just pass a coupon struct. This way, no need to call db.

And You could pattern match on Coupon to be sure You are working with a valid struct.

validate_coupon(%Coupon{} = coupon)

This would put side effect (loading from db) outside the function, making it pure.

And if You return the updated coupon, You could chain…

coupon_id
|> load_from_db()
|> validate_coupon()
|> increase_counter()
al2o3cr

al2o3cr

IMO passing around IDs is not really “minimal”; a better alternative is to convert those incoming parameters into domain structs as soon as possible in the controller and pass the structs around in the business logic.

Goose97

Goose97 OP

Yeah that’s my first thought on this. But then the extra work that the caller has to do (request to db) makes me feel like I’m duplicating a lot of logic.
Anyway, your opinion on the side effect part is something I’ve never thought of. So I think i would go with this kinda kybrid approach

def validate_coupon(%Coupon{} = coupon) do
end

def validate_coupon(coupon_id) do
end

What do you think about this?

kokolegorille

kokolegorille

I think the previous post from @al2o3cr is a good advice.

Most prefer to have a functional core, free of side effects, as it is easier to test.

That means pushing db calls out of the core.

Anyway, I don’t see the advantage of passing the id, because sooner or later You will use it to make db call.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews