mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-16 20:12:22 +03:00
Refactored paging/sorting options
This commit is contained in:
parent
5ca9680059
commit
87e012f3bf
@ -5,3 +5,11 @@ type BaseRepository interface {
|
||||
CountAll() (int, error)
|
||||
Exists(id string) (bool, error)
|
||||
}
|
||||
|
||||
type QueryOptions struct {
|
||||
SortBy string
|
||||
Alpha bool
|
||||
Desc bool
|
||||
Offset int
|
||||
Size int
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ func (r *albumRepository) Get(id string) (*domain.Album, error) {
|
||||
|
||||
func (r *albumRepository) FindByArtist(artistId string) (domain.Albums, error) {
|
||||
var as = make(domain.Albums, 0)
|
||||
err := r.loadChildren("artist", artistId, &as, "Year", false)
|
||||
err := r.loadChildren("artist", artistId, &as, domain.QueryOptions{SortBy:"Year"})
|
||||
return as, err
|
||||
}
|
||||
|
||||
|
@ -32,7 +32,7 @@ func (r *artistIndexRepository) Get(id string) (*domain.ArtistIndex, error) {
|
||||
|
||||
func (r *artistIndexRepository) GetAll() (domain.ArtistIndexes, error) {
|
||||
var indices = make(domain.ArtistIndexes, 0)
|
||||
err := r.loadAll(&indices, "", true)
|
||||
err := r.loadAll(&indices, domain.QueryOptions{Alpha:true})
|
||||
return indices, err
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"github.com/deluan/gosonic/utils"
|
||||
"reflect"
|
||||
"strings"
|
||||
"github.com/deluan/gosonic/domain"
|
||||
)
|
||||
|
||||
type ledisRepository struct {
|
||||
@ -127,24 +128,29 @@ func (r *ledisRepository) toEntity(response [][]byte, entity interface{}) error
|
||||
return utils.ToStruct(record, entity)
|
||||
}
|
||||
|
||||
func (r *ledisRepository) loadAll(entities interface{}, sortBy string, alpha bool) error {
|
||||
func (r *ledisRepository) loadAll(entities interface{}, qo ...domain.QueryOptions) error {
|
||||
setName := r.table + "s:all"
|
||||
return r.loadFromSet(setName, entities, sortBy, alpha)
|
||||
return r.loadFromSet(setName, entities, qo...)
|
||||
}
|
||||
|
||||
func (r *ledisRepository) loadChildren(parentTable string, parentId string, entities interface{}, sortBy string, alpha bool) error {
|
||||
func (r *ledisRepository) loadChildren(parentTable string, parentId string, entities interface{}, qo ...domain.QueryOptions) error {
|
||||
setName := fmt.Sprintf("%s:%s:%ss", parentTable, parentId, r.table)
|
||||
return r.loadFromSet(setName, entities, sortBy, alpha)
|
||||
return r.loadFromSet(setName, entities, qo...)
|
||||
}
|
||||
|
||||
// TODO Optimize it! Probably very slow (and confusing!)
|
||||
func (r *ledisRepository) loadFromSet(setName string, entities interface{}, sortBy string, alpha bool) error {
|
||||
func (r *ledisRepository) loadFromSet(setName string, entities interface{}, qo ...domain.QueryOptions) error {
|
||||
o := domain.QueryOptions{}
|
||||
if len(qo) > 0 {
|
||||
o = qo[0]
|
||||
}
|
||||
|
||||
reflected := reflect.ValueOf(entities).Elem()
|
||||
var sortKey []byte = nil
|
||||
if sortBy != "" {
|
||||
sortKey = []byte(fmt.Sprintf("%s:*:%s", r.table, sortBy))
|
||||
if o.SortBy != "" {
|
||||
sortKey = []byte(fmt.Sprintf("%s:*:%s", r.table, o.SortBy))
|
||||
}
|
||||
response, err := db().XSSort([]byte(setName), 0, 0, alpha, false, sortKey, r.getFieldKeys("*"))
|
||||
response, err := db().XSSort([]byte(setName), o.Offset, o.Size, o.Alpha, o.Desc, sortKey, r.getFieldKeys("*"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -131,7 +131,7 @@ func TestBaseRepository(t *testing.T) {
|
||||
|
||||
Convey("When I call loadAll", func() {
|
||||
var es = make([]TestEntity, 0)
|
||||
err := repo.loadAll(&es, "", false)
|
||||
err := repo.loadAll(&es)
|
||||
Convey("Then It should not return any error", func() {
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
@ -26,7 +26,7 @@ func (r *mediaFileRepository) Get(id string) (*domain.MediaFile, error) {
|
||||
|
||||
func (r *mediaFileRepository) FindByAlbum(albumId string) (domain.MediaFiles, error) {
|
||||
var mfs = make(domain.MediaFiles, 0)
|
||||
err := r.loadChildren("album", albumId, &mfs, "", false)
|
||||
err := r.loadChildren("album", albumId, &mfs)
|
||||
sort.Sort(mfs)
|
||||
return mfs, err
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user