Implemented getAlbumList with type=random
This commit is contained in:
@@ -23,6 +23,7 @@ func (c *GetAlbumListController) Prepare() {
|
|||||||
inject.ExtractAssignable(utils.Graph, &c.listGen)
|
inject.ExtractAssignable(utils.Graph, &c.listGen)
|
||||||
|
|
||||||
c.types = map[string]strategy{
|
c.types = map[string]strategy{
|
||||||
|
"random": func(o int, s int) (*domain.Albums, error) { return c.listGen.GetRandom(o, s) },
|
||||||
"newest": func(o int, s int) (*domain.Albums, error) { return c.listGen.GetNewest(o, s) },
|
"newest": func(o int, s int) (*domain.Albums, error) { return c.listGen.GetNewest(o, s) },
|
||||||
"recent": func(o int, s int) (*domain.Albums, error) { return c.listGen.GetRecent(o, s) },
|
"recent": func(o int, s int) (*domain.Albums, error) { return c.listGen.GetRecent(o, s) },
|
||||||
"frequent": func(o int, s int) (*domain.Albums, error) { return c.listGen.GetFrequent(o, s) },
|
"frequent": func(o int, s int) (*domain.Albums, error) { return c.listGen.GetFrequent(o, s) },
|
||||||
|
|||||||
@@ -30,4 +30,5 @@ type AlbumRepository interface {
|
|||||||
FindByArtist(artistId string) (*Albums, error)
|
FindByArtist(artistId string) (*Albums, error)
|
||||||
GetAll(QueryOptions) (*Albums, error)
|
GetAll(QueryOptions) (*Albums, error)
|
||||||
PurgeInactive(active *Albums) error
|
PurgeInactive(active *Albums) error
|
||||||
|
GetAllIds() (*[]string, error)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package engine
|
package engine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math/rand"
|
||||||
|
|
||||||
"github.com/deluan/gosonic/domain"
|
"github.com/deluan/gosonic/domain"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -9,6 +11,7 @@ type ListGenerator interface {
|
|||||||
GetRecent(offset int, size int) (*domain.Albums, error)
|
GetRecent(offset int, size int) (*domain.Albums, error)
|
||||||
GetFrequent(offset int, size int) (*domain.Albums, error)
|
GetFrequent(offset int, size int) (*domain.Albums, error)
|
||||||
GetHighest(offset int, size int) (*domain.Albums, error)
|
GetHighest(offset int, size int) (*domain.Albums, error)
|
||||||
|
GetRandom(offset int, size int) (*domain.Albums, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewListGenerator(alr domain.AlbumRepository) ListGenerator {
|
func NewListGenerator(alr domain.AlbumRepository) ListGenerator {
|
||||||
@@ -44,3 +47,21 @@ func (g listGenerator) GetHighest(offset int, size int) (*domain.Albums, error)
|
|||||||
qo := domain.QueryOptions{SortBy: "Rating", Desc: true}
|
qo := domain.QueryOptions{SortBy: "Rating", Desc: true}
|
||||||
return g.query(qo, offset, size)
|
return g.query(qo, offset, size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g listGenerator) GetRandom(offset int, size int) (*domain.Albums, error) {
|
||||||
|
ids, err := g.albumRepo.GetAllIds()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
r := make(domain.Albums, len(*ids))
|
||||||
|
perm := rand.Perm(len(*ids))
|
||||||
|
|
||||||
|
for i, v := range perm {
|
||||||
|
al, err := g.albumRepo.Get((*ids)[v])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
r[i] = *al
|
||||||
|
}
|
||||||
|
return &r, nil
|
||||||
|
}
|
||||||
@@ -41,8 +41,24 @@ func (r *albumRepository) GetAll(options domain.QueryOptions) (*domain.Albums, e
|
|||||||
return &as, err
|
return &as, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *albumRepository) GetAllIds() (*[]string, error) {
|
||||||
|
idMap, err := r.getAllIds()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
ids := make([]string, len(idMap))
|
||||||
|
|
||||||
|
i := 0
|
||||||
|
for id, _ := range idMap {
|
||||||
|
ids[i] = id
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ids, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *albumRepository) PurgeInactive(active *domain.Albums) error {
|
func (r *albumRepository) PurgeInactive(active *domain.Albums) error {
|
||||||
currentIds, err := r.GetAllIds()
|
currentIds, err := r.getAllIds()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ func (r *artistRepository) GetByName(name string) (*domain.Artist, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *artistRepository) PurgeInactive(active *domain.Artists) error {
|
func (r *artistRepository) PurgeInactive(active *domain.Artists) error {
|
||||||
currentIds, err := r.GetAllIds()
|
currentIds, err := r.getAllIds()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ func (r *ledisRepository) CountAll() (int64, error) {
|
|||||||
return size, err
|
return size, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *ledisRepository) GetAllIds() (map[string]bool, error) {
|
func (r *ledisRepository) getAllIds() (map[string]bool, error) {
|
||||||
m := make(map[string]bool)
|
m := make(map[string]bool)
|
||||||
pairs, err := db().ZRange([]byte(r.table+"s:all"), 0, -1)
|
pairs, err := db().ZRange([]byte(r.table+"s:all"), 0, -1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -155,7 +155,7 @@ func TestBaseRepository(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
Convey("When I call GetAllIds", func() {
|
Convey("When I call GetAllIds", func() {
|
||||||
ids, err := repo.GetAllIds()
|
ids, err := repo.getAllIds()
|
||||||
Convey("Then It should not return any error", func() {
|
Convey("Then It should not return any error", func() {
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ func (r *mediaFileRepository) FindByAlbum(albumId string) (*domain.MediaFiles, e
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *mediaFileRepository) PurgeInactive(active *domain.MediaFiles) error {
|
func (r *mediaFileRepository) PurgeInactive(active *domain.MediaFiles) error {
|
||||||
currentIds, err := r.GetAllIds()
|
currentIds, err := r.getAllIds()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user