From 7c1387807567519e588c398ec94d74b3041fa1af Mon Sep 17 00:00:00 2001 From: Deluan Date: Wed, 12 Mar 2025 17:34:39 -0400 Subject: [PATCH] fix(subsonic): getRandomSongs with `genre` filter fix https://github.com/dweymouth/supersonic/issues/577 Signed-off-by: Deluan --- persistence/mediafile_repository_test.go | 12 ------------ server/subsonic/filter/filters.go | 16 ++++++++++------ 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/persistence/mediafile_repository_test.go b/persistence/mediafile_repository_test.go index 3b64d89fe..41b48c0c6 100644 --- a/persistence/mediafile_repository_test.go +++ b/persistence/mediafile_repository_test.go @@ -4,7 +4,6 @@ import ( "context" "time" - "github.com/Masterminds/squirrel" "github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/model/id" @@ -53,17 +52,6 @@ var _ = Describe("MediaRepository", func() { Expect(err).To(MatchError(model.ErrNotFound)) }) - XIt("filters by genre", func() { - Expect(mr.GetAll(model.QueryOptions{ - Sort: "genre.name asc, title asc", - Filters: squirrel.Eq{"genre.name": "Rock"}, - })).To(Equal(model.MediaFiles{ - songDayInALife, - songAntenna, - songComeTogether, - })) - }) - Context("Annotations", func() { It("increments play count when the tracks does not have annotations", func() { id := "incplay.firsttime" diff --git a/server/subsonic/filter/filters.go b/server/subsonic/filter/filters.go index 1cac0b674..8a333c8e2 100644 --- a/server/subsonic/filter/filters.go +++ b/server/subsonic/filter/filters.go @@ -95,7 +95,7 @@ func SongsByRandom(genre string, fromYear, toYear int) Options { } ff := And{} if genre != "" { - ff = append(ff, Eq{"genre.name": genre}) + ff = append(ff, filterByGenre(genre)) } if fromYear != 0 { ff = append(ff, GtOrEq{"year": fromYear}) @@ -118,11 +118,15 @@ func SongWithLyrics(artist, title string) Options { func ByGenre(genre string) Options { return addDefaultFilters(Options{ - Sort: "name asc", - Filters: persistence.Exists("json_tree(tags)", And{ - Like{"value": genre}, - NotEq{"atom": nil}, - }), + Sort: "name asc", + Filters: filterByGenre(genre), + }) +} + +func filterByGenre(genre string) Sqlizer { + return persistence.Exists("json_tree(tags)", And{ + Like{"value": genre}, + NotEq{"atom": nil}, }) }