Storm AlbumRepository complete.
This commit is contained in:
@@ -4,7 +4,9 @@ import (
|
||||
"reflect"
|
||||
|
||||
"github.com/asdine/storm"
|
||||
"github.com/asdine/storm/index"
|
||||
"github.com/asdine/storm/q"
|
||||
"github.com/cloudsonic/sonic-server/domain"
|
||||
)
|
||||
|
||||
type stormRepository struct {
|
||||
@@ -34,18 +36,26 @@ func (r *stormRepository) Exists(id string) (bool, error) {
|
||||
return err != storm.ErrNotFound, nil
|
||||
}
|
||||
|
||||
func (r *stormRepository) getID(record interface{}) string {
|
||||
func (r *stormRepository) extractID(record interface{}) string {
|
||||
v := reflect.ValueOf(record).Elem()
|
||||
id := v.FieldByName("ID").String()
|
||||
return id
|
||||
}
|
||||
|
||||
func (r *stormRepository) getByID(id string, ta interface{}) error {
|
||||
err := Db().One("ID", id, ta)
|
||||
if err == storm.ErrNotFound {
|
||||
return domain.ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *stormRepository) purgeInactive(ids []string) (deleted []string, err error) {
|
||||
query := Db().Select(q.Not(q.In("ID", ids)))
|
||||
|
||||
// Collect IDs that will be deleted
|
||||
err = query.Each(r.bucket, func(record interface{}) error {
|
||||
id := r.getID(record)
|
||||
id := r.extractID(record)
|
||||
deleted = append(deleted, id)
|
||||
return nil
|
||||
})
|
||||
@@ -63,3 +73,38 @@ func (r *stormRepository) purgeInactive(ids []string) (deleted []string, err err
|
||||
}
|
||||
return deleted, nil
|
||||
}
|
||||
|
||||
func (r *stormRepository) execute(matcher q.Matcher, result *[]_Album, options ...*domain.QueryOptions) error {
|
||||
query := Db().Select(matcher)
|
||||
if len(options) > 0 {
|
||||
query = addQueryOptions(query, options[0])
|
||||
}
|
||||
err := query.Find(result)
|
||||
if err == storm.ErrNotFound {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func stormOptions(options *domain.QueryOptions) func(*index.Options) {
|
||||
return func(opts *index.Options) {
|
||||
opts.Reverse = options.Desc
|
||||
opts.Skip = options.Offset
|
||||
if options.Size > 0 {
|
||||
opts.Limit = options.Size
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func addQueryOptions(q storm.Query, o *domain.QueryOptions) storm.Query {
|
||||
if o.SortBy != "" {
|
||||
q = q.OrderBy(o.SortBy)
|
||||
}
|
||||
if o.Desc {
|
||||
q = q.Reverse()
|
||||
}
|
||||
if o.Size > 0 {
|
||||
q = q.Limit(o.Size)
|
||||
}
|
||||
return q.Skip(o.Offset)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user