Refactored paging/sorting options

This commit is contained in:
Deluan
2016-03-03 22:44:28 -05:00
parent 5ca9680059
commit 87e012f3bf
6 changed files with 26 additions and 12 deletions
+8
View File
@@ -5,3 +5,11 @@ type BaseRepository interface {
CountAll() (int, error) CountAll() (int, error)
Exists(id string) (bool, error) Exists(id string) (bool, error)
} }
type QueryOptions struct {
SortBy string
Alpha bool
Desc bool
Offset int
Size int
}
+1 -1
View File
@@ -29,7 +29,7 @@ func (r *albumRepository) Get(id string) (*domain.Album, error) {
func (r *albumRepository) FindByArtist(artistId string) (domain.Albums, error) { func (r *albumRepository) FindByArtist(artistId string) (domain.Albums, error) {
var as = make(domain.Albums, 0) 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 return as, err
} }
+1 -1
View File
@@ -32,7 +32,7 @@ func (r *artistIndexRepository) Get(id string) (*domain.ArtistIndex, error) {
func (r *artistIndexRepository) GetAll() (domain.ArtistIndexes, error) { func (r *artistIndexRepository) GetAll() (domain.ArtistIndexes, error) {
var indices = make(domain.ArtistIndexes, 0) var indices = make(domain.ArtistIndexes, 0)
err := r.loadAll(&indices, "", true) err := r.loadAll(&indices, domain.QueryOptions{Alpha:true})
return indices, err return indices, err
} }
+14 -8
View File
@@ -7,6 +7,7 @@ import (
"github.com/deluan/gosonic/utils" "github.com/deluan/gosonic/utils"
"reflect" "reflect"
"strings" "strings"
"github.com/deluan/gosonic/domain"
) )
type ledisRepository struct { type ledisRepository struct {
@@ -127,24 +128,29 @@ func (r *ledisRepository) toEntity(response [][]byte, entity interface{}) error
return utils.ToStruct(record, entity) 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" 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) 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!) // 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() reflected := reflect.ValueOf(entities).Elem()
var sortKey []byte = nil var sortKey []byte = nil
if sortBy != "" { if o.SortBy != "" {
sortKey = []byte(fmt.Sprintf("%s:*:%s", r.table, 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 { if err != nil {
return err return err
} }
+1 -1
View File
@@ -131,7 +131,7 @@ func TestBaseRepository(t *testing.T) {
Convey("When I call loadAll", func() { Convey("When I call loadAll", func() {
var es = make([]TestEntity, 0) var es = make([]TestEntity, 0)
err := repo.loadAll(&es, "", false) err := repo.loadAll(&es)
Convey("Then It should not return any error", func() { Convey("Then It should not return any error", func() {
So(err, ShouldBeNil) So(err, ShouldBeNil)
}) })
+1 -1
View File
@@ -26,7 +26,7 @@ func (r *mediaFileRepository) Get(id string) (*domain.MediaFile, error) {
func (r *mediaFileRepository) FindByAlbum(albumId string) (domain.MediaFiles, error) { func (r *mediaFileRepository) FindByAlbum(albumId string) (domain.MediaFiles, error) {
var mfs = make(domain.MediaFiles, 0) var mfs = make(domain.MediaFiles, 0)
err := r.loadChildren("album", albumId, &mfs, "", false) err := r.loadChildren("album", albumId, &mfs)
sort.Sort(mfs) sort.Sort(mfs)
return mfs, err return mfs, err
} }