Add wishlist section with paginated list

This commit is contained in:
Claudio Ortolina
2024-10-21 18:25:56 +01:00
parent 874fd921ec
commit 9b305ff06a
5 changed files with 352 additions and 0 deletions
+61
View File
@@ -0,0 +1,61 @@
defmodule MusicLibrary.Wishlist do
import Ecto.Query, warn: false
alias MusicLibrary.Repo
alias MusicLibrary.Records.{Record, SearchParser}
@fields [:id, :type, :artists, :format, :title, :release, :genres, :musicbrainz_id, :cover_hash]
def search_records(query, opts \\ []) do
limit = Keyword.get(opts, :limit, 20)
offset = Keyword.get(opts, :offset, 0)
search =
query
|> build_search()
|> limit(^limit)
|> offset(^offset)
|> select(^@fields)
Repo.all(search)
end
def search_records_count(query) do
search = build_search(query)
Repo.aggregate(search, :count)
end
defp build_search(query) do
{:ok, parsed_query} = SearchParser.parse(query)
base_search =
from r in Record,
where: is_nil(r.purchased_at),
order_by: [r.artists[0]["sort_name"], r.title]
Enum.reduce(parsed_query, base_search, fn
{:artist, artist}, search ->
search |> where([r], like(r.artists, ^"%#{artist}%"))
{:album, album}, search ->
search |> where([r], like(r.title, ^"%#{album}%"))
{:mbid, mbid}, search ->
search |> where([r], r.musicbrainz_id == ^mbid or like(r.artists, ^"%#{mbid}%"))
{:format, format}, search ->
search |> where([r], r.format == ^format)
{:type, type}, search ->
search |> where([r], r.type == ^type)
{:query, raw_query}, search ->
search
|> where(
[r],
like(r.title, ^"%#{raw_query}%") or like(r.artists, ^"%#{raw_query}%")
)
end)
end
end