Skip to content

Commit

Permalink
Remove unnecessary repositories methods
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Jul 20, 2021
1 parent 5e54925 commit 1d8607e
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 64 deletions.
5 changes: 4 additions & 1 deletion core/archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions model/album.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions model/mediafile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 0 additions & 18 deletions persistence/album_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand All @@ -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
Expand Down
9 changes: 0 additions & 9 deletions persistence/album_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
17 changes: 0 additions & 17 deletions persistence/mediafile_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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})
}
Expand Down
11 changes: 0 additions & 11 deletions persistence/mediafile_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
9 changes: 5 additions & 4 deletions server/subsonic/browsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 14 additions & 0 deletions server/subsonic/filter/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()",
Expand Down

0 comments on commit 1d8607e

Please sign in to comment.