refactor: add filters

This commit is contained in:
Deluan 2020-01-30 21:48:07 -05:00 committed by Deluan Quintão
parent 71c1844bca
commit 67ed830a68
3 changed files with 9 additions and 13 deletions

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"time" "time"
"github.com/Masterminds/squirrel"
"github.com/deluan/navidrome/model" "github.com/deluan/navidrome/model"
) )
@ -76,19 +77,19 @@ func (g *listGenerator) GetNewest(ctx context.Context, offset int, size int) (En
func (g *listGenerator) GetRecent(ctx context.Context, offset int, size int) (Entries, error) { func (g *listGenerator) GetRecent(ctx context.Context, offset int, size int) (Entries, error) {
qo := model.QueryOptions{Sort: "PlayDate", Order: "desc", Offset: offset, Max: size, qo := model.QueryOptions{Sort: "PlayDate", Order: "desc", Offset: offset, Max: size,
Filters: map[string]interface{}{"play_date__gt": time.Time{}}} Filters: squirrel.Gt{"play_date": time.Time{}}}
return g.queryByAnnotation(ctx, qo) return g.queryByAnnotation(ctx, qo)
} }
func (g *listGenerator) GetFrequent(ctx context.Context, offset int, size int) (Entries, error) { func (g *listGenerator) GetFrequent(ctx context.Context, offset int, size int) (Entries, error) {
qo := model.QueryOptions{Sort: "PlayCount", Order: "desc", Offset: offset, Max: size, qo := model.QueryOptions{Sort: "PlayCount", Order: "desc", Offset: offset, Max: size,
Filters: map[string]interface{}{"play_count__gt": 0}} Filters: squirrel.Gt{"play_count": 0}}
return g.queryByAnnotation(ctx, qo) return g.queryByAnnotation(ctx, qo)
} }
func (g *listGenerator) GetHighest(ctx context.Context, offset int, size int) (Entries, error) { func (g *listGenerator) GetHighest(ctx context.Context, offset int, size int) (Entries, error) {
qo := model.QueryOptions{Sort: "Rating", Order: "desc", Offset: offset, Max: size, qo := model.QueryOptions{Sort: "Rating", Order: "desc", Offset: offset, Max: size,
Filters: map[string]interface{}{"rating__gt": 0}} Filters: squirrel.Gt{"rating__gt": 0}}
return g.queryByAnnotation(ctx, qo) return g.queryByAnnotation(ctx, qo)
} }
@ -126,7 +127,7 @@ func (g *listGenerator) getAnnotationsForAlbums(ctx context.Context, albums mode
func (g *listGenerator) GetRandomSongs(ctx context.Context, size int, genre string) (Entries, error) { func (g *listGenerator) GetRandomSongs(ctx context.Context, size int, genre string) (Entries, error) {
options := model.QueryOptions{Max: size} options := model.QueryOptions{Max: size}
if genre != "" { if genre != "" {
options.Filters = map[string]interface{}{"genre": genre} options.Filters = squirrel.Eq{"genre": genre}
} }
mediaFiles, err := g.ds.MediaFile(ctx).GetRandom(options) mediaFiles, err := g.ds.MediaFile(ctx).GetRandom(options)
if err != nil { if err != nil {

View file

@ -3,19 +3,16 @@ package model
import ( import (
"context" "context"
"github.com/Masterminds/squirrel"
"github.com/deluan/rest" "github.com/deluan/rest"
) )
// Filters use the same operators as Beego ORM: See https://beego.me/docs/mvc/model/query.md#operators
// Ex: var q = QueryOptions{Filters: Filters{"name__istartswith": "Deluan","age__gt": 25}}
// All conditions will be ANDed together
// TODO Implement filter in repositories' methods
type QueryOptions struct { type QueryOptions struct {
Sort string Sort string
Order string Order string
Max int Max int
Offset int Offset int
Filters map[string]interface{} Filters squirrel.Sqlizer
} }
type ResourceRepository interface { type ResourceRepository interface {

View file

@ -60,10 +60,8 @@ func (r *sqlRepository) applyOptions(sq SelectBuilder, options ...model.QueryOpt
sq = sq.OrderBy(options[0].Sort) sq = sq.OrderBy(options[0].Sort)
} }
} }
if len(options[0].Filters) > 0 { if options[0].Filters != nil {
for f, v := range options[0].Filters { sq = sq.Where(options[0].Filters)
sq = sq.Where(Eq{f: v})
}
} }
} }
return sq return sq