end
def get_all(params) do
- params
- |> get_all_query()
- |> maybe_filter_by_date(params)
- |> maybe_filter_by_user(params)
- |> maybe_filter_by_search(params)
- |> Repo.all()
+ base_query =
+ get_all_query()
+ |> maybe_filter_by_date(params)
+ |> maybe_filter_by_user(params)
+ |> maybe_filter_by_search(params)
+
+ query_with_pagination = base_query |> paginate_query(params)
+
+ %{
+ items: Repo.all(query_with_pagination),
+ count: Repo.aggregate(base_query, :count, :id)
+ }
end
defp maybe_filter_by_date(query, %{start_date: nil, end_date: nil}), do: query
)
end
- defp get_all_query(%{page: page, page_size: page_size}) do
- from(q in __MODULE__,
- order_by: [desc: q.inserted_at],
+ defp paginate_query(query, %{page: page, page_size: page_size}) do
+ from(q in query,
limit: ^page_size,
offset: ^((page - 1) * page_size)
)
end
+ defp get_all_query do
+ from(q in __MODULE__,
+ order_by: [desc: q.inserted_at]
+ )
+ end
+
defp parse_datetime(datetime) do
{:ok, parsed_datetime, _} = DateTime.from_iso8601(datetime)
alias Pleroma.ModerationLog
def render("index.json", %{log: log}) do
- render_many(log, __MODULE__, "show.json", as: :log_entry)
+ %{
+ items: render_many(log.items, __MODULE__, "show.json", as: :log_entry),
+ total: log.count
+ }
end
def render("show.json", %{log_entry: log_entry}) do