For some query in Controller of Phoenix, there're two plans for me
Plan 1:
defmodule Demo.UserController do
# ...
def index do
# This is just for example
# The point is Repo in used here
Repo.all(User)
end
end
Plan 2:
defmodule Demo.User do
# ...
def all do
# Put all Repo API and building query logic in Model
Repo.all(__MODULE__)
end
end
I prefer the Plan 2. Because in most situations, I can put all logic about fetching data in Model.
But I find official guide use Plan 1(docs/model) and Phoenix default code alias Repo
in Controller instead of Model (web/web.ex)
Which one is better? And why?