Add pagination to events, groups, partipants to an event and categories

lists
This commit is contained in:
Chocobozzz
2018-12-14 11:23:36 +01:00
parent 72dbc8c261
commit 48eb72cd4c
11 changed files with 92 additions and 72 deletions

34
lib/mobilizon/ecto.ex Normal file
View File

@@ -0,0 +1,34 @@
defmodule Mobilizon.Ecto do
@moduledoc """
Mobilizon Ecto utils
"""
import Ecto.Query, warn: false
@doc """
Add limit and offset to the query
"""
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
def increment_slug(slug) do
case List.pop_at(String.split(slug, "-"), -1) do
{nil, _} ->
slug
{suffix, slug_parts} ->
case Integer.parse(suffix) do
{id, _} -> Enum.join(slug_parts, "-") <> "-" <> Integer.to_string(id + 1)
:error -> slug <> "-1"
end
end
end
end