mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-03 20:47:35 +03:00
refactor: add filters
This commit is contained in:
parent
71c1844bca
commit
67ed830a68
3 changed files with 9 additions and 13 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"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) {
|
||||
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)
|
||||
}
|
||||
|
||||
func (g *listGenerator) GetFrequent(ctx context.Context, offset int, size int) (Entries, error) {
|
||||
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)
|
||||
}
|
||||
|
||||
func (g *listGenerator) GetHighest(ctx context.Context, offset int, size int) (Entries, error) {
|
||||
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)
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
options := model.QueryOptions{Max: size}
|
||||
if genre != "" {
|
||||
options.Filters = map[string]interface{}{"genre": genre}
|
||||
options.Filters = squirrel.Eq{"genre": genre}
|
||||
}
|
||||
mediaFiles, err := g.ds.MediaFile(ctx).GetRandom(options)
|
||||
if err != nil {
|
||||
|
|
|
@ -3,19 +3,16 @@ package model
|
|||
import (
|
||||
"context"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"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 {
|
||||
Sort string
|
||||
Order string
|
||||
Max int
|
||||
Offset int
|
||||
Filters map[string]interface{}
|
||||
Filters squirrel.Sqlizer
|
||||
}
|
||||
|
||||
type ResourceRepository interface {
|
||||
|
|
|
@ -60,10 +60,8 @@ func (r *sqlRepository) applyOptions(sq SelectBuilder, options ...model.QueryOpt
|
|||
sq = sq.OrderBy(options[0].Sort)
|
||||
}
|
||||
}
|
||||
if len(options[0].Filters) > 0 {
|
||||
for f, v := range options[0].Filters {
|
||||
sq = sq.Where(Eq{f: v})
|
||||
}
|
||||
if options[0].Filters != nil {
|
||||
sq = sq.Where(options[0].Filters)
|
||||
}
|
||||
}
|
||||
return sq
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue