mobilizon.chapril.org-mobil.../lib/mobilizon/storage/page.ex

49 lines
1.1 KiB
Elixir
Raw Normal View History

2019-09-08 01:49:56 +02:00
defmodule Mobilizon.Storage.Page do
@moduledoc """
Module for pagination of queries.
"""
2019-09-08 01:49:56 +02:00
import Ecto.Query
2019-09-08 01:49:56 +02:00
alias Mobilizon.Storage.Repo
defstruct [
:total,
:elements
]
@type t :: %__MODULE__{
total: integer,
elements: struct
}
@doc """
Returns a Page struct for a query.
"""
@spec build_page(Ecto.Query.t(), integer | nil, integer | nil) :: t
def build_page(query, page, limit) do
[total, elements] =
[
fn -> Repo.aggregate(query, :count, :id) end,
fn -> Repo.all(paginate(query, page, limit)) end
]
|> Enum.map(&Task.async/1)
|> Enum.map(&Task.await/1)
%__MODULE__{total: total, elements: elements}
end
@doc """
Add limit and offset to the query.
"""
2019-09-08 01:49:56 +02:00
@spec paginate(Ecto.Query.t() | struct, integer | nil, integer | nil) :: Ecto.Query.t()
def paginate(query, page \\ 1, size \\ 10)
def paginate(query, page, _size) when is_nil(page), do: paginate(query)
def paginate(query, page, size) when is_nil(size), do: paginate(query, page)
def paginate(query, page, size) do
from(query, limit: ^size, offset: ^((page - 1) * size))
end
end