Reimplemented GetAlbumList&type=random and GetRandomSongs (now with filter by genres)

This commit is contained in:
Deluan
2020-01-21 08:49:43 -05:00
parent 6cd758faa0
commit de0816da67
9 changed files with 80 additions and 54 deletions
+24 -16
View File
@@ -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)