Standardize pagination to Keyword.get pattern

Closes #137.
This commit is contained in:
Claudio Ortolina
2026-03-25 11:45:21 +00:00
parent a0be1a5b9a
commit abac91aa42
2 changed files with 16 additions and 32 deletions
+8 -12
View File
@@ -8,6 +8,8 @@ defmodule MusicLibrary.OnlineStoreTemplates do
alias MusicLibrary.OnlineStoreTemplates.OnlineStoreTemplate
alias MusicLibrary.Repo
@pagination Application.compile_env!(:music_library, :pagination)
@spec list_enabled_templates() :: [OnlineStoreTemplate.t()]
def list_enabled_templates do
OnlineStoreTemplate
@@ -32,19 +34,13 @@ defmodule MusicLibrary.OnlineStoreTemplates do
|> order_by([t], fragment("? COLLATE NOCASE ASC", t.name))
|> filter_templates(opts)
query =
case Keyword.get(opts, :offset) do
nil -> query
offset -> from(t in query, offset: ^offset)
end
offset = Keyword.get(opts, :offset, 0)
limit = Keyword.get(opts, :limit, @pagination[:default_page_size])
query =
case Keyword.get(opts, :limit) do
nil -> query
limit -> from(t in query, limit: ^limit)
end
Repo.all(query)
query
|> offset(^offset)
|> limit(^limit)
|> Repo.all()
end
@spec count_templates(list_opts()) :: non_neg_integer()
+8 -20
View File
@@ -11,6 +11,8 @@ defmodule MusicLibrary.ScrobbleRules do
alias MusicLibrary.Repo
alias MusicLibrary.ScrobbleRules.ScrobbleRule
@pagination Application.compile_env!(:music_library, :pagination)
@type list_opts :: [
type: atom(),
enabled: boolean(),
@@ -27,27 +29,13 @@ defmodule MusicLibrary.ScrobbleRules do
|> order_scrobble_rules(Keyword.get(opts, :order, :inserted_at))
|> filter_scrobble_rules(opts)
query =
case Keyword.get(opts, :offset) do
nil ->
query
offset = Keyword.get(opts, :offset, 0)
limit = Keyword.get(opts, :limit, @pagination[:default_page_size])
offset ->
from r in query,
offset: ^offset
end
query =
case Keyword.get(opts, :limit) do
nil ->
query
limit ->
from r in query,
limit: ^limit
end
Repo.all(query)
query
|> offset(^offset)
|> limit(^limit)
|> Repo.all()
end
@spec count_scrobble_rules(list_opts()) :: non_neg_integer()