Reimplemented GetAlbumList&type=random and GetRandomSongs (now with filter by genres)
This commit is contained in:
@@ -79,6 +79,24 @@ func (r *albumRepository) GetAll(options ...model.QueryOptions) (model.Albums, e
|
||||
return r.toAlbums(all), nil
|
||||
}
|
||||
|
||||
// TODO Keep order when paginating
|
||||
func (r *albumRepository) GetRandom(options ...model.QueryOptions) (model.Albums, error) {
|
||||
sq := r.newRawQuery(options...)
|
||||
switch r.ormer.Driver().Type() {
|
||||
case orm.DRMySQL:
|
||||
sq = sq.OrderBy("RAND()")
|
||||
default:
|
||||
sq = sq.OrderBy("RANDOM()")
|
||||
}
|
||||
sql, args, err := sq.ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var results []album
|
||||
_, err = r.ormer.Raw(sql, args...).QueryRows(&results)
|
||||
return r.toAlbums(results), err
|
||||
}
|
||||
|
||||
func (r *albumRepository) toAlbums(all []album) model.Albums {
|
||||
result := make(model.Albums, len(all))
|
||||
for i, a := range all {
|
||||
|
||||
@@ -42,12 +42,6 @@ var _ = Describe("AlbumRepository", func() {
|
||||
})
|
||||
})
|
||||
|
||||
Describe("GetAllIds", func() {
|
||||
It("returns all records", func() {
|
||||
Expect(repo.GetAllIds()).To(ConsistOf("1", "2", "3"))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("GetStarred", func() {
|
||||
It("returns all starred records", func() {
|
||||
Expect(repo.GetStarred(model.QueryOptions{})).To(Equal(model.Albums{
|
||||
|
||||
@@ -127,6 +127,23 @@ func (r *mediaFileRepository) DeleteByPath(path string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *mediaFileRepository) GetRandom(options ...model.QueryOptions) (model.MediaFiles, error) {
|
||||
sq := r.newRawQuery(options...)
|
||||
switch r.ormer.Driver().Type() {
|
||||
case orm.DRMySQL:
|
||||
sq = sq.OrderBy("RAND()")
|
||||
default:
|
||||
sq = sq.OrderBy("RANDOM()")
|
||||
}
|
||||
sql, args, err := sq.ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var results []mediaFile
|
||||
_, err = r.ormer.Raw(sql, args...).QueryRows(&results)
|
||||
return r.toMediaFiles(results), err
|
||||
}
|
||||
|
||||
func (r *mediaFileRepository) GetStarred(options ...model.QueryOptions) (model.MediaFiles, error) {
|
||||
var starred []mediaFile
|
||||
_, err := r.newQuery(options...).Filter("starred", true).All(&starred)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/cloudsonic/sonic-server/model"
|
||||
)
|
||||
@@ -29,6 +30,29 @@ func (r *sqlRepository) newQuery(options ...model.QueryOptions) orm.QuerySeter {
|
||||
return q
|
||||
}
|
||||
|
||||
func (r *sqlRepository) newRawQuery(options ...model.QueryOptions) squirrel.SelectBuilder {
|
||||
sq := squirrel.Select("*").From(r.tableName)
|
||||
if len(options) > 0 {
|
||||
if options[0].Max > 0 {
|
||||
sq = sq.Limit(uint64(options[0].Max))
|
||||
}
|
||||
if options[0].Offset > 0 {
|
||||
sq = sq.Offset(uint64(options[0].Max))
|
||||
}
|
||||
if options[0].Sort != "" {
|
||||
if options[0].Order == "desc" {
|
||||
sq = sq.OrderBy(options[0].Sort + " desc")
|
||||
} else {
|
||||
sq = sq.OrderBy(options[0].Sort)
|
||||
}
|
||||
}
|
||||
for field, value := range options[0].Filters {
|
||||
sq = sq.Where(squirrel.Like{field: value.(string) + "%"})
|
||||
}
|
||||
}
|
||||
return sq
|
||||
}
|
||||
|
||||
func (r *sqlRepository) CountAll() (int64, error) {
|
||||
return r.newQuery().Count()
|
||||
}
|
||||
@@ -38,22 +62,6 @@ func (r *sqlRepository) Exists(id string) (bool, error) {
|
||||
return c == 1, err
|
||||
}
|
||||
|
||||
// TODO This is used to generate random lists. Can be optimized in SQL: https://stackoverflow.com/a/19419
|
||||
func (r *sqlRepository) GetAllIds() ([]string, error) {
|
||||
qs := r.newQuery()
|
||||
var values []orm.Params
|
||||
num, err := qs.Values(&values, "id")
|
||||
if num == 0 {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := collectField(values, func(item interface{}) string {
|
||||
return item.(orm.Params)["ID"].(string)
|
||||
})
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// "Hack" to bypass Postgres driver limitation
|
||||
func (r *sqlRepository) insert(record interface{}) error {
|
||||
_, err := r.ormer.Insert(record)
|
||||
|
||||
Reference in New Issue
Block a user