joebew42

joebew42

What to do when you have to add a new parameter based on the result you get from a Service?

Hello folks!

I have a question about how to deal with Context, in particular I see that from the perspective of controller tests all the keys of the params are passed as strings, while from the perspective of context|model tests we pass all the params keys as atoms.

That’s not make a big difference cause what matter is that all the keys of a map have to be of the same type, but …

I need to use a Service inside one of my Context so that based on the result I get from it I need to add a new key, value pair in the parameters map.

The problem I have is that “I don’t know” if I have to put the key as an atom or as a string.

So, I end up with this solution, but I bet that there is a better way to deal with it. In short, I created an helper function add_param_to that in somehow can understand if to deal with atoms or strings.

Thank you so much for all the feedback!

Here it is my pseudo code:

defmodule Something.Blogs do

  alias Something.Post
  alias Something.{CalendarService, CalendarEvent}

  def create_blog_post(attrs, podcast, calendar_service \\ CalendarService) do
    event_start = Map.get(attrs, :recorded_at)

    attrs = case calendar_service.create(CalendarEvent.build_for(podcast, event_start)) do
      {:ok, event_id} -> add_param_to(attrs, "calendar_event_id", event_id)
      {:error, _message} -> attrs
    end

    Post.insert_changeset(attrs)
    |> Repo.insert
  end

  defp add_param_to(attrs, key, value) do
    [head|_] = Map.keys(attrs)
    add_param_to(attrs, key, value, is_atom(head))
  end

  defp add_param_to(attrs, key, value, is_atom) when is_atom do
    Map.put(attrs, String.to_atom(key), value)
  end
  defp add_param_to(attrs, key, value, _is_atom) do
    Map.put(attrs, key, value)
  end
end

Most Liked

idi527

idi527

I usually try to avoid changing anything in attrs or params. I “pre-populate” schemas’ structs instead.

  def create_blog_post(attrs, podcast, calendar_service \\ CalendarService) do
    event_start = Map.get(attrs, :recorded_at) || Map.get(attrs, "recorded_at")

    event_id = case calendar_service.create(CalendarEvent.build_for(podcast, event_start)) do
      {:ok, event_id} -> event_id
      {:error, _message} -> nil
    end
    
    %Post{calendar_event_id: event_id} 
    |> Post.insert_changeset(attrs)
    |> Repo.insert
  end

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
romenigld
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement