The default generated code in phoenix creates functions like this:
def create_post(attrs \\ %{}) do
%Post{}
|> Post.changeset(attrs)
|> Repo.insert()
end
However typically code has to be filters/cleaned, associations checked, etc… before one can actually create_post
(as in this example).
I see two ways of doing the work before creating a post, either in the function, or as part of another function.
# option 1: in function
def create_post(attrs \\ %{}) do
# check author is a publisher on the platform maybe?
# check if this publication is accepting new posts?
# maybe can only publish post if author has filled out their profile?
# collect metadata not coming in attrs maybe?
%Post{}
|> Post.changeset(attrs)
|> Repo.insert()
end
# option 2: other function
# *_service?
def post_service(attrs \\ {}) do
# check author is a publisher on the platform maybe?
# check if this publication is accepting new posts?
# maybe can only publish post if author has filled out their profile?
# collect metadata not coming in attrs maybe?
create_post(attrs)
end
Assuming all those comments I added belong to functions in the same context, which style do you prefer and why? Please note, the end goal of the function is to Create A Post (not asking about side-effects/associations/callbacks/etc…).
Thank you for all opinions/suggestions.