diff --git a/core/archiver.go b/core/archiver.go index 1a857b173..c8867062d 100644 --- a/core/archiver.go +++ b/core/archiver.go @@ -30,7 +30,10 @@ type archiver struct { type createHeader func(idx int, mf model.MediaFile) *zip.FileHeader func (a *archiver) ZipAlbum(ctx context.Context, id string, out io.Writer) error { - mfs, err := a.ds.MediaFile(ctx).FindByAlbum(id) + mfs, err := a.ds.MediaFile(ctx).GetAll(model.QueryOptions{ + Filters: squirrel.Eq{"album_id": id}, + Sort: "album", + }) if err != nil { log.Error(ctx, "Error loading mediafiles from album", "id", id, err) return err diff --git a/model/album.go b/model/album.go index e1e4be57f..1a6c860cb 100644 --- a/model/album.go +++ b/model/album.go @@ -45,9 +45,7 @@ type AlbumRepository interface { Exists(id string) (bool, error) Put(*Album) error Get(id string) (*Album, error) - FindByArtist(albumArtistId string) (Albums, error) GetAll(...QueryOptions) (Albums, error) - GetRandom(...QueryOptions) (Albums, error) Search(q string, offset int, size int) (Albums, error) Refresh(ids ...string) error AnnotatedRepository diff --git a/model/mediafile.go b/model/mediafile.go index f31cda87f..77f27215d 100644 --- a/model/mediafile.go +++ b/model/mediafile.go @@ -64,11 +64,9 @@ type MediaFileRepository interface { Put(m *MediaFile) error Get(id string) (*MediaFile, error) GetAll(options ...QueryOptions) (MediaFiles, error) - FindByAlbum(albumId string) (MediaFiles, error) FindAllByPath(path string) (MediaFiles, error) FindByPath(path string) (*MediaFile, error) FindPathsRecursively(basePath string) ([]string, error) - GetRandom(options ...QueryOptions) (MediaFiles, error) Search(q string, offset int, size int) (MediaFiles, error) Delete(id string) error DeleteByPath(path string) (int64, error) diff --git a/persistence/album_repository.go b/persistence/album_repository.go index 2a3f3df64..0d2b77a1e 100644 --- a/persistence/album_repository.go +++ b/persistence/album_repository.go @@ -116,15 +116,6 @@ func (r *albumRepository) Put(m *model.Album) error { return r.updateGenres(m.ID, r.tableName, genres) } -func (r *albumRepository) FindByArtist(artistId string) (model.Albums, error) { - options := model.QueryOptions{ - Sort: "max_year", - Filters: Eq{"album_artist_id": artistId}, - } - - return r.GetAll(options) -} - func (r *albumRepository) GetAll(options ...model.QueryOptions) (model.Albums, error) { sq := r.selectAlbum(options...). LeftJoin("album_genres ag on album.id = ag.album_id"). @@ -139,15 +130,6 @@ func (r *albumRepository) GetAll(options ...model.QueryOptions) (model.Albums, e return res, err } -// TODO Keep order when paginating -func (r *albumRepository) GetRandom(options ...model.QueryOptions) (model.Albums, error) { - if len(options) == 0 { - options = []model.QueryOptions{{}} - } - options[0].Sort = "random()" - return r.GetAll(options...) -} - // Return a map of mediafiles that have embedded covers for the given album ids func (r *albumRepository) getEmbeddedCovers(ids []string) (map[string]model.MediaFile, error) { var mfs model.MediaFiles diff --git a/persistence/album_repository_test.go b/persistence/album_repository_test.go index a54ef224b..cd55e6570 100644 --- a/persistence/album_repository_test.go +++ b/persistence/album_repository_test.go @@ -62,15 +62,6 @@ var _ = Describe("AlbumRepository", func() { }) }) - Describe("FindByArtist", func() { - It("returns all records from a given ArtistID", func() { - Expect(repo.FindByArtist("3")).To(Equal(model.Albums{ - albumSgtPeppers, - albumAbbeyRoad, - })) - }) - }) - Describe("getMinYear", func() { It("returns 0 when there's no valid year", func() { Expect(getMinYear("a b c")).To(Equal(0)) diff --git a/persistence/mediafile_repository.go b/persistence/mediafile_repository.go index 69492c0a0..c2a659e80 100644 --- a/persistence/mediafile_repository.go +++ b/persistence/mediafile_repository.go @@ -90,14 +90,6 @@ func (r *mediaFileRepository) GetAll(options ...model.QueryOptions) (model.Media return res, err } -func (r *mediaFileRepository) FindByAlbum(albumId string) (model.MediaFiles, error) { - options := model.QueryOptions{ - Filters: Eq{"album_id": albumId}, - Sort: "album", - } - return r.GetAll(options) -} - func (r *mediaFileRepository) FindByPath(path string) (*model.MediaFile, error) { sel := r.selectMediaFile().Where(Eq{"path": path}) var res model.MediaFiles @@ -161,15 +153,6 @@ func (r *mediaFileRepository) deleteNotInPath(basePath string) error { return err } -// TODO Keep order when paginating -func (r *mediaFileRepository) GetRandom(options ...model.QueryOptions) (model.MediaFiles, error) { - if len(options) == 0 { - options = []model.QueryOptions{{}} - } - options[0].Sort = "random()" - return r.GetAll(options...) -} - func (r *mediaFileRepository) Delete(id string) error { return r.delete(Eq{"id": id}) } diff --git a/persistence/mediafile_repository_test.go b/persistence/mediafile_repository_test.go index 496a034ca..bfb0744fc 100644 --- a/persistence/mediafile_repository_test.go +++ b/persistence/mediafile_repository_test.go @@ -41,17 +41,6 @@ var _ = Describe("MediaRepository", func() { Expect(mr.Exists("666")).To(BeFalse()) }) - It("find mediafiles by album", func() { - Expect(mr.FindByAlbum("103")).To(Equal(model.MediaFiles{ - songAntenna, - songRadioactivity, - })) - }) - - It("returns empty array when no tracks are found", func() { - Expect(mr.FindByAlbum("67")).To(Equal(model.MediaFiles{})) - }) - It("finds tracks by path when using wildcards chars", func() { Expect(mr.Put(&model.MediaFile{ID: "7001", Path: P("/Find:By'Path/_/123.mp3")})).To(BeNil()) Expect(mr.Put(&model.MediaFile{ID: "7002", Path: P("/Find:By'Path/1/123.mp3")})).To(BeNil()) diff --git a/server/subsonic/browsing.go b/server/subsonic/browsing.go index 47bbc585e..e8199ff6e 100644 --- a/server/subsonic/browsing.go +++ b/server/subsonic/browsing.go @@ -12,6 +12,7 @@ import ( "github.com/navidrome/navidrome/core" "github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/model" + "github.com/navidrome/navidrome/server/subsonic/filter" "github.com/navidrome/navidrome/server/subsonic/responses" "github.com/navidrome/navidrome/utils" ) @@ -150,7 +151,7 @@ func (c *BrowsingController) GetArtist(w http.ResponseWriter, r *http.Request) ( return nil, err } - albums, err := c.ds.Album(ctx).FindByArtist(id) + albums, err := c.ds.Album(ctx).GetAll(filter.AlbumsByArtistID(id)) if err != nil { log.Error(ctx, "Error retrieving albums by artist", "id", id, "name", artist.Name, err) return nil, err @@ -175,7 +176,7 @@ func (c *BrowsingController) GetAlbum(w http.ResponseWriter, r *http.Request) (* return nil, err } - mfs, err := c.ds.MediaFile(ctx).FindByAlbum(id) + mfs, err := c.ds.MediaFile(ctx).GetAll(filter.SongsByAlbum(id)) if err != nil { log.Error(ctx, "Error retrieving tracks from album", "id", id, "name", album.Name, err) return nil, err @@ -341,7 +342,7 @@ func (c *BrowsingController) buildArtistDirectory(ctx context.Context, artist *m dir.Starred = &artist.StarredAt } - albums, err := c.ds.Album(ctx).FindByArtist(artist.ID) + albums, err := c.ds.Album(ctx).GetAll(filter.AlbumsByArtistID(artist.ID)) if err != nil { return nil, err } @@ -370,7 +371,7 @@ func (c *BrowsingController) buildAlbumDirectory(ctx context.Context, album *mod dir.Starred = &album.StarredAt } - mfs, err := c.ds.MediaFile(ctx).FindByAlbum(album.ID) + mfs, err := c.ds.MediaFile(ctx).GetAll(filter.SongsByAlbum(album.ID)) if err != nil { return nil, err } diff --git a/server/subsonic/filter/filters.go b/server/subsonic/filter/filters.go index b1dd01a94..da6e339d1 100644 --- a/server/subsonic/filter/filters.go +++ b/server/subsonic/filter/filters.go @@ -48,6 +48,13 @@ func AlbumsByGenre(genre string) Options { } } +func AlbumsByArtistID(artistId string) Options { + return Options{ + Sort: "max_year", + Filters: squirrel.Eq{"album_artist_id": artistId}, + } +} + func AlbumsByYear(fromYear, toYear int) Options { sortOption := "max_year, name" if fromYear > toYear { @@ -76,6 +83,13 @@ func SongsByGenre(genre string) Options { } } +func SongsByAlbum(albumId string) Options { + return Options{ + Filters: squirrel.Eq{"album_id": albumId}, + Sort: "album", + } +} + func SongsByRandom(genre string, fromYear, toYear int) Options { options := Options{ Sort: "random()",