Is it okay to put the get_one function in index action?

Hi, I’m trying to make an api for getting users by user_type.
I made getting users api with :index
and getting a specific user api using user_id with :show.

But one day, one of our team wants to make an api to find a specific user with

So I made it like this

def index(conn, %{"type" => type}) do
  with users when not is_nil(user) <- UserContext.get_user(type) do
    render(conn, "index.json", %{user: user})

But is it okay to use like getting only one user in the index action?
I used the resources for the three apis.

Thanks for reading my question.

Hi @menw in general with RESTFUL style APIs you don’t mix return shapes like this. If you have an endpoint that usually returns a list of users, having it only return one user depending on search parameters is strange.


Technically you can do whatever you like, of course. But it’s easier to maintain several functions each doing only one thing. So preferably do that.

1 Like