Add pagination to events, groups, partipants to an event and categories
lists
This commit is contained in:
34
lib/mobilizon/ecto.ex
Normal file
34
lib/mobilizon/ecto.ex
Normal 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
|
||||
Reference in New Issue
Block a user