Handle possible non-existent record id in search filter

This commit is contained in:
Claudio Ortolina
2026-04-15 15:10:28 +01:00
parent f6e78022a4
commit 2124c982f7
2 changed files with 18 additions and 9 deletions
+15 -9
View File
@@ -16,6 +16,7 @@ defmodule MusicLibrary.ListeningStats do
Artists, Artists,
BackgroundRepo, BackgroundRepo,
ListeningStats.SearchParser, ListeningStats.SearchParser,
Records,
Records.Record, Records.Record,
Repo, Repo,
Worker Worker
@@ -616,16 +617,21 @@ defmodule MusicLibrary.ListeningStats do
end end
defp apply_record_filter(base_query, record_id) do defp apply_record_filter(base_query, record_id) do
record = Repo.get!(Record, record_id) case Records.get_record(record_id) do
release_ids = record.release_ids nil ->
main_artist_name = Record.main_artist(record).name from t in base_query, where: false
record_title = record.title
from t in base_query, record ->
where: release_ids = record.release_ids
fragment("json_extract(?, '$.musicbrainz_id')", t.album) in ^release_ids or main_artist_name = Record.main_artist(record).name
(fragment("json_extract(?, '$.title')", t.album) == ^record_title and record_title = record.title
fragment("json_extract(?, '$.name')", t.artist) == ^main_artist_name)
from t in base_query,
where:
fragment("json_extract(?, '$.musicbrainz_id')", t.album) in ^release_ids or
(fragment("json_extract(?, '$.title')", t.album) == ^record_title and
fragment("json_extract(?, '$.name')", t.artist) == ^main_artist_name)
end
end end
defp polyfill_artist(artist, musicbrainz_id) do defp polyfill_artist(artist, musicbrainz_id) do
+3
View File
@@ -189,6 +189,9 @@ defmodule MusicLibrary.Records do
Repo.all(q) Repo.all(q)
end end
@spec get_record(String.t()) :: Record.t() | nil
def get_record(id), do: Repo.get(Record, id)
@spec get_record!(String.t()) :: Record.t() @spec get_record!(String.t()) :: Record.t()
def get_record!(id), do: Repo.get!(Record, id) def get_record!(id), do: Repo.get!(Record, id)