refactor: add GetStarred to artists

This commit is contained in:
Deluan 2020-01-31 15:58:17 -05:00 committed by Deluan Quintão
parent 5a4c763510
commit a260e65307
3 changed files with 26 additions and 11 deletions

View File

@ -76,8 +76,7 @@ func (r *artistRepository) GetAll(options ...model.QueryOptions) (model.Artists,
// TODO Cache the index (recalculate when there are changes to the DB)
func (r *artistRepository) GetIndex() (model.ArtistIndexes, error) {
sq := Select("artist_id as id", "artist as name", "count(distinct album_id) as album_count").
From("media_file").GroupBy("artist_id").OrderBy("name")
sq := r.selectArtist().OrderBy("name")
var all model.Artists
// TODO Paginate
err := r.queryAll(sq, &all)
@ -93,7 +92,7 @@ func (r *artistRepository) GetIndex() (model.ArtistIndexes, error) {
idx = &model.ArtistIndex{ID: ax}
fullIdx[ax] = idx
}
idx.Artists = append(idx.Artists, model.Artist(a))
idx.Artists = append(idx.Artists, a)
}
var result model.ArtistIndexes
for _, idx := range fullIdx {
@ -158,7 +157,10 @@ where f.artist_id in ('%s') group by f.artist_id order by f.id`, strings.Join(id
}
func (r *artistRepository) GetStarred(options ...model.QueryOptions) (model.Artists, error) {
return nil, nil // TODO
sq := r.selectArtist(options...).Where("starred = true")
var starred model.Artists
err := r.queryAll(sq, &starred)
return starred, err
}
func (r *artistRepository) PurgeEmpty() error {

View File

@ -1,6 +1,8 @@
package persistence
import (
"context"
"github.com/astaxie/beego/orm"
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model"
@ -12,7 +14,8 @@ var _ = Describe("ArtistRepository", func() {
var repo model.ArtistRepository
BeforeEach(func() {
repo = NewArtistRepository(log.NewContext(nil), orm.NewOrm())
ctx := context.WithValue(log.NewContext(nil), "user", &model.User{ID: "userid"})
repo = NewArtistRepository(ctx, orm.NewOrm())
})
Describe("Count", func() {
@ -36,6 +39,14 @@ var _ = Describe("ArtistRepository", func() {
})
})
Describe("GetStarred", func() {
It("returns all starred records", func() {
Expect(repo.GetStarred(model.QueryOptions{})).To(Equal(model.Artists{
artistBeatles,
}))
})
})
Describe("GetIndex", func() {
It("returns the index", func() {
idx, err := repo.GetIndex()

View File

@ -20,9 +20,9 @@ import (
func TestPersistence(t *testing.T) {
tests.Init(t, true)
//os.Remove("./test-123.db")
//conf.Server.DbPath = "./test-123.db"
conf.Server.DbPath = "file::memory:?cache=shared"
os.Remove("./test-123.db")
conf.Server.DbPath = "./test-123.db"
//conf.Server.DbPath = "file::memory:?cache=shared"
New()
db.EnsureDB()
log.SetLevel(log.LevelCritical)
@ -31,7 +31,7 @@ func TestPersistence(t *testing.T) {
}
var artistKraftwerk = model.Artist{ID: "2", Name: "Kraftwerk", AlbumCount: 1}
var artistBeatles = model.Artist{ID: "3", Name: "The Beatles", AlbumCount: 2}
var artistBeatles = model.Artist{ID: "3", Name: "The Beatles", AlbumCount: 2, Starred: true}
var testArtists = model.Artists{
artistKraftwerk,
artistBeatles,
@ -57,9 +57,11 @@ var testSongs = model.MediaFiles{
songAntenna,
}
var annAlbumRadioactivity = model.Annotation{AnnID: "1", UserID: "userid", ItemType: model.AlbumItemType, ItemID: "3", Starred: true}
var annSongComeTogether = model.Annotation{AnnID: "2", UserID: "userid", ItemType: model.MediaItemType, ItemID: "2", Starred: true}
var annArtistBeatles = model.Annotation{AnnID: "3", UserID: "userid", ItemType: model.ArtistItemType, ItemID: artistBeatles.ID, Starred: true}
var annAlbumRadioactivity = model.Annotation{AnnID: "1", UserID: "userid", ItemType: model.AlbumItemType, ItemID: albumRadioactivity.ID, Starred: true}
var annSongComeTogether = model.Annotation{AnnID: "2", UserID: "userid", ItemType: model.MediaItemType, ItemID: songComeTogether.ID, Starred: true}
var testAnnotations = []model.Annotation{
annArtistBeatles,
annAlbumRadioactivity,
annSongComeTogether,
}