shahryarjb

shahryarjb

How to improve function and remove extra from it's code

Hello, I want to try to improve my code, please see the below code :

def test() do
  with {:ok, %{acl: "admin", info: _info}} <- TrangellApiGateway.User.Login.user_acl_check(allreq),
            {:ok, ms} <- http_error_handeller(conn, Post.name(allreq), "created")  do
            conn
            |> put_status(201)
            |> json(%{message: ms})  
      else
            _ ->
               conn
               |> put_status(400)
               |> json(%{error: "Your request is invalid."}) 
      end
end

Please see the Post.name(allreq), I want to replace some Fun like `Post.other_name(allreq) insted of that, but I dont want to remove the other code of this function.

for exampel

The code concerned should be blow code :

fn 2

def test() do
  with {:ok, %{acl: "admin", info: _info}} <- TrangellApiGateway.User.Login.user_acl_check(allreq),
            {:ok, ms} <- http_error_handeller(conn, Post.other_name(allreq), "created")  do
            conn
            |> put_status(201)
            |> json(%{message: ms})  
      else
            _ ->
               conn
               |> put_status(400)
               |> json(%{error: "Your request is invalid."}) 
      end
end

I just edited Post.other_name(allreq), not anything else .

My friend told me that I could use action_fallback , but I don’t know how to use it in my code , if I can do this , My code will be 80% optimized

How do I do ?

ref :
https://swanros.com/2017/03/04/action-fallback-contexts-phoenix1-3-tiny-controllers/

Marked As Solved

LostKobrakai

LostKobrakai

Pseudo-Code as there are missing parameters in those functions, but it should convey the point. You could pass just the function name or the module and the function.

def test_1(), do: test(:name)
def test_2(), do: test(:other_name)

defp test(function_name) do
  with {:ok, %{acl: "admin", info: _info}} <- TrangellApiGateway.User.Login.user_acl_check(allreq),
            {:ok, ms} <- http_error_handeller(conn, apply(Post, function_name, [allreq]), "created")  do
            conn
            |> put_status(201)
            |> json(%{message: ms})  
      else
            _ ->
               conn
               |> put_status(400)
               |> json(%{error: "Your request is invalid."}) 
      end
end

Also Liked

fuelen

fuelen

I think it is better to pass a lambda as an argument to test function. In this case, compiler can catch errors if function or module doesn’t exist.

def test_1(), do: test(&Post.name/1)
def test_2(), do: test(&Post.other_name/1)

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
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
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement