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
+7 -1
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,7 +617,11 @@ 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
nil ->
from t in base_query, where: false
record ->
release_ids = record.release_ids release_ids = record.release_ids
main_artist_name = Record.main_artist(record).name main_artist_name = Record.main_artist(record).name
record_title = record.title record_title = record.title
@@ -627,6 +632,7 @@ defmodule MusicLibrary.ListeningStats do
(fragment("json_extract(?, '$.title')", t.album) == ^record_title and (fragment("json_extract(?, '$.title')", t.album) == ^record_title and
fragment("json_extract(?, '$.name')", t.artist) == ^main_artist_name) 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
if is_nil(artist.musicbrainz_id) or artist.musicbrainz_id == "" do if is_nil(artist.musicbrainz_id) or 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)