Make QueryOptions optional in AlbumRepository.GetAll

This commit is contained in:
Deluan 2020-01-11 21:25:37 -05:00
parent bfac3e3c91
commit d4ed6a0569
7 changed files with 26 additions and 18 deletions

View File

@ -31,7 +31,7 @@ type AlbumRepository interface {
Put(m *Album) error
Get(id string) (*Album, error)
FindByArtist(artistId string) (Albums, error)
GetAll(QueryOptions) (Albums, error)
GetAll(...QueryOptions) (Albums, error)
PurgeInactive(active Albums) ([]string, error)
GetAllIds() ([]string, error)
GetStarred(QueryOptions) (Albums, error)

View File

@ -36,9 +36,9 @@ func (r *albumRepository) FindByArtist(artistId string) (domain.Albums, error) {
return as, err
}
func (r *albumRepository) GetAll(options domain.QueryOptions) (domain.Albums, error) {
func (r *albumRepository) GetAll(options ...domain.QueryOptions) (domain.Albums, error) {
var as = make(domain.Albums, 0)
err := r.loadAll(&as, options)
err := r.loadAll(&as, options...)
return as, err
}

View File

@ -63,9 +63,9 @@ func (r *albumRepository) FindByArtist(artistId string) (domain.Albums, error) {
return r.toAlbums(albums)
}
func (r *albumRepository) GetAll(options domain.QueryOptions) (domain.Albums, error) {
func (r *albumRepository) GetAll(options ...domain.QueryOptions) (domain.Albums, error) {
var all []_Album
err := r.getAll(&all, &options)
err := r.getAll(&all, options...)
if err != nil {
return nil, err
}
@ -82,7 +82,7 @@ func (r *albumRepository) toAlbums(all []_Album) (domain.Albums, error) {
func (r *albumRepository) GetAllIds() ([]string, error) {
var all []_Album
err := r.getAll(&all, &domain.QueryOptions{})
err := r.getAll(&all)
if err != nil {
return nil, err
}

View File

@ -36,7 +36,7 @@ func (r *artistIndexRepository) Get(id string) (*domain.ArtistIndex, error) {
func (r *artistIndexRepository) GetAll() (domain.ArtistIndexes, error) {
var all []_ArtistIndex
err := r.getAll(&all, &domain.QueryOptions{})
err := r.getAll(&all)
if err != nil {
return nil, err
}

View File

@ -88,7 +88,7 @@ func (r *mediaFileRepository) GetStarred(options domain.QueryOptions) (domain.Me
func (r *mediaFileRepository) GetAllIds() ([]string, error) {
var all []_MediaFile
err := r.getAll(&all, &domain.QueryOptions{})
err := r.getAll(&all)
if err != nil {
return nil, err
}

View File

@ -42,7 +42,7 @@ func (r *playlistRepository) Get(id string) (*domain.Playlist, error) {
func (r *playlistRepository) GetAll(options domain.QueryOptions) (domain.Playlists, error) {
var all []_Playlist
err := r.getAll(&all, &options)
err := r.getAll(&all)
if err != nil {
return nil, err
}

View File

@ -91,21 +91,29 @@ func (r *stormRepository) execute(matcher q.Matcher, result interface{}, options
return err
}
func (r *stormRepository) getAll(all interface{}, options *domain.QueryOptions) (err error) {
if options.SortBy != "" {
err = Db().AllByIndex(options.SortBy, all, stormOptions(options))
func (r *stormRepository) getAll(all interface{}, options ...domain.QueryOptions) (err error) {
o := domain.QueryOptions{}
if len(options) > 0 {
o = options[0]
}
if o.SortBy != "" {
err = Db().AllByIndex(o.SortBy, all, stormOptions(o))
} else {
err = Db().All(all, stormOptions(options))
err = Db().All(all, stormOptions(o))
}
return
}
func stormOptions(options *domain.QueryOptions) func(*index.Options) {
func stormOptions(options ...domain.QueryOptions) func(*index.Options) {
o := domain.QueryOptions{}
if len(options) > 0 {
o = options[0]
}
return func(opts *index.Options) {
opts.Reverse = options.Desc
opts.Skip = options.Offset
if options.Size > 0 {
opts.Limit = options.Size
opts.Reverse = o.Desc
opts.Skip = o.Offset
if o.Size > 0 {
opts.Limit = o.Size
}
}
}