Return "total" optionally

This commit is contained in:
Maxim Filippov 2019-09-03 13:58:27 +03:00
commit b15cfd80ef
10 changed files with 36 additions and 41 deletions

View file

@ -16,31 +16,39 @@ defmodule Pleroma.Pagination do
def fetch_paginated(query, params, type \\ :keyset)
def fetch_paginated(query, params, :keyset) do
options = cast_params(params)
def fetch_paginated(query, %{"total" => true} = params, :keyset) do
total = Repo.aggregate(query, :count, :id)
%{
total: total,
items:
query
|> paginate(options, :keyset)
|> Repo.all()
|> enforce_order(options)
items: fetch_paginated(query, Map.drop(params, ["total"]), :keyset)
}
end
def fetch_paginated(query, params, :keyset) do
options = cast_params(params)
query
|> paginate(options, :keyset)
|> Repo.all()
|> enforce_order(options)
end
def fetch_paginated(query, %{"total" => true} = params, :offset) do
total = Repo.aggregate(query, :count, :id)
%{
total: total,
items: fetch_paginated(query, Map.drop(params, ["total"]), :offset)
}
end
def fetch_paginated(query, params, :offset) do
options = cast_params(params)
total = Repo.aggregate(query, :count, :id)
%{
total: total,
items:
query
|> paginate(options, :offset)
|> Repo.all()
}
query
|> paginate(options, :offset)
|> Repo.all()
end
def paginate(query, options, method \\ :keyset)