Implemented getAlbumList with type=random

This commit is contained in:
Deluan
2016-03-08 21:54:32 -05:00
parent 867ca0580c
commit 9bea04f330
8 changed files with 44 additions and 5 deletions
@@ -1,6 +1,8 @@
package engine
import (
"math/rand"
"github.com/deluan/gosonic/domain"
)
@@ -9,6 +11,7 @@ type ListGenerator interface {
GetRecent(offset int, size int) (*domain.Albums, error)
GetFrequent(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 {
@@ -44,3 +47,21 @@ func (g listGenerator) GetHighest(offset int, size int) (*domain.Albums, error)
qo := domain.QueryOptions{SortBy: "Rating", Desc: true}
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
}