From abac91aa425062f2345d9d5cb6408354db8ea8be Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Wed, 25 Mar 2026 11:45:21 +0000 Subject: [PATCH] Standardize pagination to Keyword.get pattern Closes #137. --- lib/music_library/online_store_templates.ex | 20 ++++++--------- lib/music_library/scrobble_rules.ex | 28 ++++++--------------- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/lib/music_library/online_store_templates.ex b/lib/music_library/online_store_templates.ex index b3448a90..8486e228 100644 --- a/lib/music_library/online_store_templates.ex +++ b/lib/music_library/online_store_templates.ex @@ -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() diff --git a/lib/music_library/scrobble_rules.ex b/lib/music_library/scrobble_rules.ex index 328e5ada..dd942fe6 100644 --- a/lib/music_library/scrobble_rules.ex +++ b/lib/music_library/scrobble_rules.ex @@ -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()