Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sort option
Browse files Browse the repository at this point in the history
ChristianTovar committed Dec 14, 2023

Verified

This commit was signed with the committer’s verified signature.
olegkorol Oleg Korol
1 parent 38a917c commit 9a97782
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/beacon/content.ex
Original file line number Diff line number Diff line change
@@ -751,6 +751,7 @@ defmodule Beacon.Content do
* `:offset` - offsets the number of rows selected from the result.
* `:query` - search pages by path or title.
* `:preloads` - a list of preloads to load.
* `:sort` - column in which the result will be ordered by.
"""
@doc type: :pages
@@ -760,21 +761,19 @@ defmodule Beacon.Content do
offset = Keyword.get(opts, :offset, 0)
search = Keyword.get(opts, :query)
preloads = Keyword.get(opts, :preloads, [])
sort = Keyword.get(opts, :sort, :title)

site
|> query_list_pages_base()
|> query_list_pages_limit(per_page)
|> query_list_pages_offset(offset)
|> query_list_pages_search(search)
|> query_list_pages_preloads(preloads)
|> query_list_pages_sort(sort)
|> Repo.all()
end

defp query_list_pages_base(site) do
from p in Page,
where: p.site == ^site,
order_by: [asc: p.order, asc: fragment("length(?)", p.path)]
end
defp query_list_pages_base(site), do: from(p in Page, where: p.site == ^site)

defp query_list_pages_limit(query, limit) when is_integer(limit), do: from(q in query, limit: ^limit)
defp query_list_pages_limit(query, :infinity = _limit), do: query
@@ -795,6 +794,8 @@ defmodule Beacon.Content do

defp query_list_pages_preloads(query, _preloads), do: query

defp query_list_pages_sort(query, sort), do: from(q in query, order_by: [asc: ^sort])

@doc """
Counts the total number of pages based on the amount of pages.
"""

0 comments on commit 9a97782

Please sign in to comment.