Centralize pagination settings

This commit is contained in:
Claudio Ortolina
2026-02-17 08:24:07 +00:00
parent 0c4af8340b
commit 10b6a0703c
7 changed files with 36 additions and 17 deletions
+7
View File
@@ -17,6 +17,13 @@ config :music_library,
config :music_library, default_timezone: "Europe/London" config :music_library, default_timezone: "Europe/London"
config :music_library, :pagination,
default_page_size: 20,
stats_limit: 30,
top_items_limit: 10,
tracks_page_size: 200,
search_preview_limit: 5
config :music_library, MusicLibraryWeb, config :music_library, MusicLibraryWeb,
login_password: "change me", login_password: "change me",
api_token: "change me" api_token: "change me"
+5 -3
View File
@@ -6,8 +6,10 @@ defmodule MusicLibrary.Collection do
alias MusicLibrary.Records.{Record, RecordRelease, SearchIndex} alias MusicLibrary.Records.{Record, RecordRelease, SearchIndex}
alias MusicLibrary.Repo alias MusicLibrary.Repo
@pagination Application.compile_env!(:music_library, :pagination)
def search_records(query, opts \\ []) do def search_records(query, opts \\ []) do
limit = Keyword.get(opts, :limit, 20) limit = Keyword.get(opts, :limit, @pagination[:default_page_size])
offset = Keyword.get(opts, :offset, 0) offset = Keyword.get(opts, :offset, 0)
order = Keyword.get(opts, :order, :alphabetical) order = Keyword.get(opts, :order, :alphabetical)
@@ -87,7 +89,7 @@ defmodule MusicLibrary.Collection do
end end
def count_records_by_artist(opts \\ []) do def count_records_by_artist(opts \\ []) do
limit = Keyword.get(opts, :limit, 30) limit = Keyword.get(opts, :limit, @pagination[:stats_limit])
q = q =
from r in fragment("records, json_each(records.artists)"), from r in fragment("records, json_each(records.artists)"),
@@ -105,7 +107,7 @@ defmodule MusicLibrary.Collection do
end end
def count_records_by_genre(opts \\ []) do def count_records_by_genre(opts \\ []) do
limit = Keyword.get(opts, :limit, 30) limit = Keyword.get(opts, :limit, @pagination[:stats_limit])
q = q =
from r in fragment("records, json_each(records.genres)"), from r in fragment("records, json_each(records.genres)"),
+4 -2
View File
@@ -4,9 +4,11 @@ defmodule MusicLibrary.RecordSets do
alias MusicLibrary.RecordSets.{RecordSet, RecordSetItem} alias MusicLibrary.RecordSets.{RecordSet, RecordSetItem}
alias MusicLibrary.Repo alias MusicLibrary.Repo
@pagination Application.compile_env!(:music_library, :pagination)
def list_record_sets(opts \\ []) do def list_record_sets(opts \\ []) do
offset = Keyword.get(opts, :offset, 0) offset = Keyword.get(opts, :offset, 0)
limit = Keyword.get(opts, :limit, 20) limit = Keyword.get(opts, :limit, @pagination[:default_page_size])
from(rs in RecordSet, from(rs in RecordSet,
order_by: [desc: rs.updated_at], order_by: [desc: rs.updated_at],
@@ -28,7 +30,7 @@ defmodule MusicLibrary.RecordSets do
def search_record_sets(query, opts \\ []) do def search_record_sets(query, opts \\ []) do
offset = Keyword.get(opts, :offset, 0) offset = Keyword.get(opts, :offset, 0)
limit = Keyword.get(opts, :limit, 20) limit = Keyword.get(opts, :limit, @pagination[:default_page_size])
order = Keyword.get(opts, :order, :updated_at) order = Keyword.get(opts, :order, :updated_at)
record_sets_search_query(query) record_sets_search_query(query)
+7 -5
View File
@@ -5,6 +5,8 @@ defmodule MusicLibrary.ScrobbleActivity do
alias MusicBrainz.Release alias MusicBrainz.Release
alias MusicLibrary.{Artists, Collection, Records.ArtistRecord, Repo, Secrets, Wishlist} alias MusicLibrary.{Artists, Collection, Records.ArtistRecord, Repo, Secrets, Wishlist}
@pagination Application.compile_env!(:music_library, :pagination)
def can_scrobble? do def can_scrobble? do
Secrets.get("last_fm_session_key") !== nil Secrets.get("last_fm_session_key") !== nil
end end
@@ -274,7 +276,7 @@ defmodule MusicLibrary.ScrobbleActivity do
Returns a list of maps with album information and play counts. Returns a list of maps with album information and play counts.
""" """
def get_top_albums_by_days(days, opts) do def get_top_albums_by_days(days, opts) do
limit = Keyword.get(opts, :limit, 10) limit = Keyword.get(opts, :limit, @pagination[:top_items_limit])
current_time = Keyword.get_lazy(opts, :current_time, &DateTime.utc_now/0) current_time = Keyword.get_lazy(opts, :current_time, &DateTime.utc_now/0)
timezone = Keyword.get(opts, :timezone, &MusicLibrary.default_timezone/0) timezone = Keyword.get(opts, :timezone, &MusicLibrary.default_timezone/0)
@@ -319,7 +321,7 @@ defmodule MusicLibrary.ScrobbleActivity do
Returns a list of maps with album information and play counts. Returns a list of maps with album information and play counts.
""" """
def get_top_albums(opts) do def get_top_albums(opts) do
limit = Keyword.get(opts, :limit, 10) limit = Keyword.get(opts, :limit, @pagination[:top_items_limit])
query = query =
from t in Track, from t in Track,
@@ -354,7 +356,7 @@ defmodule MusicLibrary.ScrobbleActivity do
Returns a list of maps with artist information and play counts. Returns a list of maps with artist information and play counts.
""" """
def get_top_artists(opts) do def get_top_artists(opts) do
limit = Keyword.get(opts, :limit, 10) limit = Keyword.get(opts, :limit, @pagination[:top_items_limit])
query = query =
from t in Track, from t in Track,
@@ -380,7 +382,7 @@ defmodule MusicLibrary.ScrobbleActivity do
Returns a list of maps with artist information and play counts. Returns a list of maps with artist information and play counts.
""" """
def get_top_artists_by_days(days, opts) do def get_top_artists_by_days(days, opts) do
limit = Keyword.get(opts, :limit, 10) limit = Keyword.get(opts, :limit, @pagination[:top_items_limit])
current_time = Keyword.get_lazy(opts, :current_time, &DateTime.utc_now/0) current_time = Keyword.get_lazy(opts, :current_time, &DateTime.utc_now/0)
timezone = Keyword.get(opts, :timezone, &MusicLibrary.default_timezone/0) timezone = Keyword.get(opts, :timezone, &MusicLibrary.default_timezone/0)
@@ -447,7 +449,7 @@ defmodule MusicLibrary.ScrobbleActivity do
def list_tracks(params \\ %{}) do def list_tracks(params \\ %{}) do
query = Map.get(params, :query, "") query = Map.get(params, :query, "")
page = Map.get(params, :page, 1) page = Map.get(params, :page, 1)
page_size = Map.get(params, :page_size, 200) page_size = Map.get(params, :page_size, @pagination[:tracks_page_size])
order = Map.get(params, :order, :scrobbled_at) order = Map.get(params, :order, :scrobbled_at)
all_artists_query = all_artists_query =
+7 -5
View File
@@ -14,6 +14,8 @@ defmodule MusicLibrary.Search do
alias MusicLibrary.{Collection, RecordSets, Repo, Wishlist} alias MusicLibrary.{Collection, RecordSets, Repo, Wishlist}
alias MusicLibrary.Records.ArtistRecord alias MusicLibrary.Records.ArtistRecord
@pagination Application.compile_env!(:music_library, :pagination)
@doc """ @doc """
Performs a universal search across all entity types. Performs a universal search across all entity types.
@@ -26,7 +28,7 @@ defmodule MusicLibrary.Search do
- :limit - Limit per category (default: 5) - :limit - Limit per category (default: 5)
""" """
def universal_search(query, opts \\ []) do def universal_search(query, opts \\ []) do
limit = Keyword.get(opts, :limit, 5) limit = Keyword.get(opts, :limit, @pagination[:search_preview_limit])
%{ %{
collection: search_collection(query, limit), collection: search_collection(query, limit),
@@ -39,14 +41,14 @@ defmodule MusicLibrary.Search do
@doc """ @doc """
Searches records in the collection (purchased records). Searches records in the collection (purchased records).
""" """
def search_collection(query, limit \\ 5) do def search_collection(query, limit \\ @pagination[:search_preview_limit]) do
Collection.search_records(query, limit: limit) Collection.search_records(query, limit: limit)
end end
@doc """ @doc """
Searches records in the wishlist (unpurchased records). Searches records in the wishlist (unpurchased records).
""" """
def search_wishlist(query, limit \\ 5) do def search_wishlist(query, limit \\ @pagination[:search_preview_limit]) do
Wishlist.search_records(query, limit: limit) Wishlist.search_records(query, limit: limit)
end end
@@ -56,7 +58,7 @@ defmodule MusicLibrary.Search do
Searches across artist names in the artist_records table, Searches across artist names in the artist_records table,
returning distinct artists that match the query. returning distinct artists that match the query.
""" """
def search_artists(query, limit \\ 5) do def search_artists(query, limit \\ @pagination[:search_preview_limit]) do
case String.trim(query) do case String.trim(query) do
"" -> "" ->
[] []
@@ -99,7 +101,7 @@ defmodule MusicLibrary.Search do
@doc """ @doc """
Searches record sets by name, description, and contained records. Searches record sets by name, description, and contained records.
""" """
def search_record_sets(query, limit \\ 5) do def search_record_sets(query, limit \\ @pagination[:search_preview_limit]) do
RecordSets.search_record_sets(query, limit: limit) RecordSets.search_record_sets(query, limit: limit)
end end
+3 -1
View File
@@ -5,8 +5,10 @@ defmodule MusicLibrary.Wishlist do
alias MusicLibrary.Records.{RecordRelease, SearchIndex} alias MusicLibrary.Records.{RecordRelease, SearchIndex}
alias MusicLibrary.Repo alias MusicLibrary.Repo
@pagination Application.compile_env!(:music_library, :pagination)
def search_records(query, opts \\ []) do def search_records(query, opts \\ []) do
limit = Keyword.get(opts, :limit, 20) limit = Keyword.get(opts, :limit, @pagination[:default_page_size])
offset = Keyword.get(opts, :offset, 0) offset = Keyword.get(opts, :offset, 0)
order = Keyword.get(opts, :order, :alphabetical) order = Keyword.get(opts, :order, :alphabetical)
+3 -1
View File
@@ -1,4 +1,6 @@
defmodule MusicLibraryWeb.LiveHelpers.Params do defmodule MusicLibraryWeb.LiveHelpers.Params do
@pagination Application.compile_env!(:music_library, :pagination)
def parse_page(nil), do: 1 def parse_page(nil), do: 1
def parse_page(page) when is_binary(page) do def parse_page(page) when is_binary(page) do
@@ -26,7 +28,7 @@ defmodule MusicLibraryWeb.LiveHelpers.Params do
def merge_pagination(params, url_params, total_entries, opts \\ []) do def merge_pagination(params, url_params, total_entries, opts \\ []) do
allowed = Keyword.get(opts, :allowed_page_sizes) allowed = Keyword.get(opts, :allowed_page_sizes)
default_page_size = params[:page_size] || 20 default_page_size = params[:page_size] || @pagination[:default_page_size]
page = parse_page(url_params["page"]) page = parse_page(url_params["page"])
page_size = parse_page_size(url_params["page_size"], allowed, default_page_size) page_size = parse_page_size(url_params["page_size"], allowed, default_page_size)