Start extracting a Collection context

This commit is contained in:
Claudio Ortolina
2024-11-10 13:38:50 +00:00
parent b247cb2536
commit bd0336c4a2
6 changed files with 96 additions and 114 deletions
+8 -53
View File
@@ -2,71 +2,26 @@ 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]
alias MusicLibrary.Records
alias MusicLibrary.Records.Record
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)
Records.search_records(base_search(), query, limit: limit, offset: offset)
end
def search_records_count(query) do
search = build_search(query)
Repo.aggregate(search, :count)
Records.search_records_count(base_search(), query)
end
def count do
q =
from r in Record,
where: is_nil(r.purchased_at)
Repo.aggregate(q, :count)
Repo.aggregate(base_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:
fragment(
"json_extract(artists, '$[0].sort_name') COLLATE NOCASE ASC, title COLLATE NOCASE ASC"
)
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)
defp base_search do
from r in Record,
where: is_nil(r.purchased_at)
end
end