sahilpaudel

sahilpaudel

Partially required request body params

I have a create function that takes conn and params as an argument and in the params, there can be 3 possible values where 2 of them are non-mandatory and 1 is required.

 def create(conn, %{}) do
    conn 
    |> put_status(:bad_request)
    |> json(%{"message" => "tenant_id is required"})
  end
def create(conn, route_params \\ %{"tenant_id" => tenant_id}) do
      with {:ok, %Route{} = route} <- Shortner.create_route(route_params) do
        conn
        |> put_status(:created)
        |> put_resp_header("location", route_path(conn, :show, route))
        |> render("show.json", route: route)
      end
  end

how can create the create function when tenant_id is missing I throw an error else just proceed as it is.

Note: I have introduced this new field just now as a requirement earlier we just had name & prefix and both were non-mandatory.

Thanks

Marked As Solved

Exadra37

Exadra37

If I am understanding you correctly you need to swap the order of the functions and remove the default value:

def create(conn, %{"tenant_id" => tenant_id} = route_params) do
  with {:ok, %Route{} = route} <- Shortner.create_route(route_params) do
    conn
    |> put_status(:created)
    |> put_resp_header("location", route_path(conn, :show, route))
    |> render("show.json", route: route)
  end
end

def create(conn, %{}) do
  conn 
  |> put_status(:bad_request)
  |> json(%{"message" => "tenant_id is required"})
end

Where Next?

Popular in Questions 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
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
gshaw
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
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement