frenzy/lib/frenzy_web/controllers/fervor/paginator.ex

25 lines
624 B
Elixir
Raw Permalink Normal View History

2019-03-31 14:52:56 +00:00
defmodule FrenzyWeb.Fervor.Paginator do
import Ecto.Query
2020-06-07 15:14:01 +00:00
def paginate(query, %{"max_id" => max_id}) do
2019-03-31 14:52:56 +00:00
from(o in query, where: o.id < ^max_id, order_by: [desc: :id])
end
2020-06-07 15:14:01 +00:00
def paginate(query, %{"min_id" => min_id}) do
2019-03-31 14:52:56 +00:00
from(o in query, where: o.id > ^min_id, order_by: [asc: :id])
end
2020-06-07 15:14:01 +00:00
def paginate(query, %{"since_id" => since_id}) do
2019-03-31 14:52:56 +00:00
from(o in query, where: o.id > ^since_id, order_by: [desc: :id])
end
def paginate(query, _params) do
from(query, order_by: [desc: :id])
end
def limit(query, params) do
limit = Map.get(params, "limit", 20)
from(query, limit: ^limit)
end
end