Centralize pagination settings
This commit is contained in:
@@ -17,6 +17,13 @@ config :music_library,
|
||||
|
||||
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,
|
||||
login_password: "change me",
|
||||
api_token: "change me"
|
||||
|
||||
@@ -6,8 +6,10 @@ defmodule MusicLibrary.Collection do
|
||||
alias MusicLibrary.Records.{Record, RecordRelease, SearchIndex}
|
||||
alias MusicLibrary.Repo
|
||||
|
||||
@pagination Application.compile_env!(:music_library, :pagination)
|
||||
|
||||
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)
|
||||
order = Keyword.get(opts, :order, :alphabetical)
|
||||
|
||||
@@ -87,7 +89,7 @@ defmodule MusicLibrary.Collection do
|
||||
end
|
||||
|
||||
def count_records_by_artist(opts \\ []) do
|
||||
limit = Keyword.get(opts, :limit, 30)
|
||||
limit = Keyword.get(opts, :limit, @pagination[:stats_limit])
|
||||
|
||||
q =
|
||||
from r in fragment("records, json_each(records.artists)"),
|
||||
@@ -105,7 +107,7 @@ defmodule MusicLibrary.Collection do
|
||||
end
|
||||
|
||||
def count_records_by_genre(opts \\ []) do
|
||||
limit = Keyword.get(opts, :limit, 30)
|
||||
limit = Keyword.get(opts, :limit, @pagination[:stats_limit])
|
||||
|
||||
q =
|
||||
from r in fragment("records, json_each(records.genres)"),
|
||||
|
||||
@@ -4,9 +4,11 @@ defmodule MusicLibrary.RecordSets do
|
||||
alias MusicLibrary.RecordSets.{RecordSet, RecordSetItem}
|
||||
alias MusicLibrary.Repo
|
||||
|
||||
@pagination Application.compile_env!(:music_library, :pagination)
|
||||
|
||||
def list_record_sets(opts \\ []) do
|
||||
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,
|
||||
order_by: [desc: rs.updated_at],
|
||||
@@ -28,7 +30,7 @@ defmodule MusicLibrary.RecordSets do
|
||||
|
||||
def search_record_sets(query, opts \\ []) do
|
||||
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)
|
||||
|
||||
record_sets_search_query(query)
|
||||
|
||||
@@ -5,6 +5,8 @@ defmodule MusicLibrary.ScrobbleActivity do
|
||||
alias MusicBrainz.Release
|
||||
alias MusicLibrary.{Artists, Collection, Records.ArtistRecord, Repo, Secrets, Wishlist}
|
||||
|
||||
@pagination Application.compile_env!(:music_library, :pagination)
|
||||
|
||||
def can_scrobble? do
|
||||
Secrets.get("last_fm_session_key") !== nil
|
||||
end
|
||||
@@ -274,7 +276,7 @@ defmodule MusicLibrary.ScrobbleActivity do
|
||||
Returns a list of maps with album information and play counts.
|
||||
"""
|
||||
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)
|
||||
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.
|
||||
"""
|
||||
def get_top_albums(opts) do
|
||||
limit = Keyword.get(opts, :limit, 10)
|
||||
limit = Keyword.get(opts, :limit, @pagination[:top_items_limit])
|
||||
|
||||
query =
|
||||
from t in Track,
|
||||
@@ -354,7 +356,7 @@ defmodule MusicLibrary.ScrobbleActivity do
|
||||
Returns a list of maps with artist information and play counts.
|
||||
"""
|
||||
def get_top_artists(opts) do
|
||||
limit = Keyword.get(opts, :limit, 10)
|
||||
limit = Keyword.get(opts, :limit, @pagination[:top_items_limit])
|
||||
|
||||
query =
|
||||
from t in Track,
|
||||
@@ -380,7 +382,7 @@ defmodule MusicLibrary.ScrobbleActivity do
|
||||
Returns a list of maps with artist information and play counts.
|
||||
"""
|
||||
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)
|
||||
timezone = Keyword.get(opts, :timezone, &MusicLibrary.default_timezone/0)
|
||||
|
||||
@@ -447,7 +449,7 @@ defmodule MusicLibrary.ScrobbleActivity do
|
||||
def list_tracks(params \\ %{}) do
|
||||
query = Map.get(params, :query, "")
|
||||
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)
|
||||
|
||||
all_artists_query =
|
||||
|
||||
@@ -14,6 +14,8 @@ defmodule MusicLibrary.Search do
|
||||
alias MusicLibrary.{Collection, RecordSets, Repo, Wishlist}
|
||||
alias MusicLibrary.Records.ArtistRecord
|
||||
|
||||
@pagination Application.compile_env!(:music_library, :pagination)
|
||||
|
||||
@doc """
|
||||
Performs a universal search across all entity types.
|
||||
|
||||
@@ -26,7 +28,7 @@ defmodule MusicLibrary.Search do
|
||||
- :limit - Limit per category (default: 5)
|
||||
"""
|
||||
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),
|
||||
@@ -39,14 +41,14 @@ defmodule MusicLibrary.Search do
|
||||
@doc """
|
||||
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)
|
||||
end
|
||||
|
||||
@doc """
|
||||
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)
|
||||
end
|
||||
|
||||
@@ -56,7 +58,7 @@ defmodule MusicLibrary.Search do
|
||||
Searches across artist names in the artist_records table,
|
||||
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
|
||||
"" ->
|
||||
[]
|
||||
@@ -99,7 +101,7 @@ defmodule MusicLibrary.Search do
|
||||
@doc """
|
||||
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)
|
||||
end
|
||||
|
||||
|
||||
@@ -5,8 +5,10 @@ defmodule MusicLibrary.Wishlist do
|
||||
alias MusicLibrary.Records.{RecordRelease, SearchIndex}
|
||||
alias MusicLibrary.Repo
|
||||
|
||||
@pagination Application.compile_env!(:music_library, :pagination)
|
||||
|
||||
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)
|
||||
order = Keyword.get(opts, :order, :alphabetical)
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
defmodule MusicLibraryWeb.LiveHelpers.Params do
|
||||
@pagination Application.compile_env!(:music_library, :pagination)
|
||||
|
||||
def parse_page(nil), do: 1
|
||||
|
||||
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
|
||||
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_size = parse_page_size(url_params["page_size"], allowed, default_page_size)
|
||||
|
||||
Reference in New Issue
Block a user