Remove unnecessary repositories methods

This commit is contained in:
Deluan 2021-07-16 17:39:00 -04:00 committed by Deluan Quintão
parent 5e54925520
commit 1d8607ef6a
9 changed files with 23 additions and 64 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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))

View File

@ -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})
}

View File

@ -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())

View File

@ -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
}

View File

@ -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()",