(Separation of concern) Controller Code Review

def index(conn, %{"user_id" => user_id}) do
    %Stipe.Accounts.User{name: name, daily_updates: daily_updates} =
      Accounts.get_user!(user_id)
      |> Stipe.Repo.preload(:daily_updates)

    # daily_updates = Standup.list_daily_updates(%Stipe.Accounts.User{id: user_id})
    render(conn, "index.html", user_name: name, daily_updates: daily_updates)
  end

Is it ok if I pipe Stipe.Repo.preload in the controller code? Will that leak persistence concern into the controller layer?

I would have a get_daily_updates(user) or load_daily_updates(user) function instead.
The controller should not care about how the core app gets the information, it just consumes the app´s api. Think of your phoenix frontend(StipeWeb) as a client of your core app(Stipe).

This way you can also reuse the dialy_updates loading logic in other places if needed(other controller, other functions inside your core app, etc).

2 Likes