Fix album lists, to use annotations
This commit is contained in:
+39
-16
@@ -30,10 +30,7 @@ type listGenerator struct {
|
|||||||
npRepo NowPlayingRepository
|
npRepo NowPlayingRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Only return albums that have the Sort field != empty
|
func (g *listGenerator) query(ctx context.Context, qo model.QueryOptions) (Entries, error) {
|
||||||
func (g *listGenerator) query(ctx context.Context, qo model.QueryOptions, offset int, size int) (Entries, error) {
|
|
||||||
qo.Offset = offset
|
|
||||||
qo.Max = size
|
|
||||||
albums, err := g.ds.Album().GetAll(qo)
|
albums, err := g.ds.Album().GetAll(qo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -49,34 +46,60 @@ func (g *listGenerator) query(ctx context.Context, qo model.QueryOptions, offset
|
|||||||
return FromAlbums(albums, annMap), err
|
return FromAlbums(albums, annMap), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *listGenerator) queryByAnnotation(ctx context.Context, qo model.QueryOptions) (Entries, error) {
|
||||||
|
annotations, err := g.ds.Annotation().GetAll(getUserID(ctx), model.AlbumItemType, qo)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
albumIds := make([]string, len(annotations))
|
||||||
|
for i, ann := range annotations {
|
||||||
|
albumIds[i] = ann.ItemID
|
||||||
|
}
|
||||||
|
|
||||||
|
albumMap, err := g.ds.Album().GetMap(albumIds)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var albums Entries
|
||||||
|
for _, ann := range annotations {
|
||||||
|
album := albumMap[ann.ItemID]
|
||||||
|
albums = append(albums, FromAlbum(&album, &ann))
|
||||||
|
}
|
||||||
|
return albums, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (g *listGenerator) GetNewest(ctx context.Context, offset int, size int) (Entries, error) {
|
func (g *listGenerator) GetNewest(ctx context.Context, offset int, size int) (Entries, error) {
|
||||||
qo := model.QueryOptions{Sort: "CreatedAt", Order: "desc"}
|
qo := model.QueryOptions{Sort: "CreatedAt", Order: "desc", Offset: offset, Max: size}
|
||||||
return g.query(ctx, qo, offset, size)
|
return g.query(ctx, qo)
|
||||||
}
|
}
|
||||||
|
|
||||||
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"}
|
qo := model.QueryOptions{Sort: "PlayDate", Order: "desc", Offset: offset, Max: size,
|
||||||
return g.query(ctx, qo, offset, size)
|
Filters: map[string]interface{}{"play_date__gt": time.Time{}}}
|
||||||
|
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"}
|
qo := model.QueryOptions{Sort: "PlayCount", Order: "desc", Offset: offset, Max: size,
|
||||||
return g.query(ctx, qo, offset, size)
|
Filters: map[string]interface{}{"play_count__gt": 0}}
|
||||||
|
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"}
|
qo := model.QueryOptions{Sort: "Rating", Order: "desc", Offset: offset, Max: size,
|
||||||
return g.query(ctx, qo, offset, size)
|
Filters: map[string]interface{}{"rating__gt": 0}}
|
||||||
|
return g.queryByAnnotation(ctx, qo)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *listGenerator) GetByName(ctx context.Context, offset int, size int) (Entries, error) {
|
func (g *listGenerator) GetByName(ctx context.Context, offset int, size int) (Entries, error) {
|
||||||
qo := model.QueryOptions{Sort: "Name"}
|
qo := model.QueryOptions{Sort: "Name", Offset: offset, Max: size}
|
||||||
return g.query(ctx, qo, offset, size)
|
return g.query(ctx, qo)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *listGenerator) GetByArtist(ctx context.Context, offset int, size int) (Entries, error) {
|
func (g *listGenerator) GetByArtist(ctx context.Context, offset int, size int) (Entries, error) {
|
||||||
qo := model.QueryOptions{Sort: "Artist"}
|
qo := model.QueryOptions{Sort: "Artist", Offset: offset, Max: size}
|
||||||
return g.query(ctx, qo, offset, size)
|
return g.query(ctx, qo)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *listGenerator) GetRandom(ctx context.Context, offset int, size int) (Entries, error) {
|
func (g *listGenerator) GetRandom(ctx context.Context, offset int, size int) (Entries, error) {
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ type AlbumRepository interface {
|
|||||||
Get(id string) (*Album, error)
|
Get(id string) (*Album, error)
|
||||||
FindByArtist(artistId string) (Albums, error)
|
FindByArtist(artistId string) (Albums, error)
|
||||||
GetAll(...QueryOptions) (Albums, error)
|
GetAll(...QueryOptions) (Albums, error)
|
||||||
|
GetMap(ids []string) (map[string]Album, error)
|
||||||
GetRandom(...QueryOptions) (Albums, error)
|
GetRandom(...QueryOptions) (Albums, error)
|
||||||
GetStarred(userId string, options ...QueryOptions) (Albums, error)
|
GetStarred(userId string, options ...QueryOptions) (Albums, error)
|
||||||
Search(q string, offset int, size int) (Albums, error)
|
Search(q string, offset int, size int) (Albums, error)
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ type AnnotationMap map[string]Annotation
|
|||||||
|
|
||||||
type AnnotationRepository interface {
|
type AnnotationRepository interface {
|
||||||
Get(userID, itemType string, itemID string) (*Annotation, error)
|
Get(userID, itemType string, itemID string) (*Annotation, error)
|
||||||
|
GetAll(userID, itemType string, options ...QueryOptions) ([]Annotation, error)
|
||||||
GetMap(userID, itemType string, itemID []string) (AnnotationMap, error)
|
GetMap(userID, itemType string, itemID []string) (AnnotationMap, error)
|
||||||
Delete(userID, itemType string, itemID ...string) error
|
Delete(userID, itemType string, itemID ...string) error
|
||||||
IncPlayCount(userID, itemType string, itemID string, ts time.Time) error
|
IncPlayCount(userID, itemType string, itemID string, ts time.Time) error
|
||||||
|
|||||||
@@ -75,6 +75,19 @@ func (r *albumRepository) GetAll(options ...model.QueryOptions) (model.Albums, e
|
|||||||
return r.toAlbums(all), nil
|
return r.toAlbums(all), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *albumRepository) GetMap(ids []string) (map[string]model.Album, error) {
|
||||||
|
var all []album
|
||||||
|
_, err := r.newQuery().Filter("id__in", ids).All(&all)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
res := make(map[string]model.Album)
|
||||||
|
for _, a := range all {
|
||||||
|
res[a.ID] = model.Album(a)
|
||||||
|
}
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
// TODO Keep order when paginating
|
// TODO Keep order when paginating
|
||||||
func (r *albumRepository) GetRandom(options ...model.QueryOptions) (model.Albums, error) {
|
func (r *albumRepository) GetRandom(options ...model.QueryOptions) (model.Albums, error) {
|
||||||
sq := r.newRawQuery(options...)
|
sq := r.newRawQuery(options...)
|
||||||
|
|||||||
@@ -75,6 +75,24 @@ func (r *annotationRepository) GetMap(userID, itemType string, itemID []string)
|
|||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *annotationRepository) GetAll(userID, itemType string, options ...model.QueryOptions) ([]model.Annotation, error) {
|
||||||
|
if userID == "" {
|
||||||
|
return nil, model.ErrInvalidAuth
|
||||||
|
}
|
||||||
|
q := r.newQuery(options...).Filter("user_id", userID).Filter("item_type", itemType)
|
||||||
|
var res []annotation
|
||||||
|
_, err := q.All(&res)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
all := make([]model.Annotation, len(res))
|
||||||
|
for i, a := range res {
|
||||||
|
all[i] = model.Annotation(a)
|
||||||
|
}
|
||||||
|
return all, err
|
||||||
|
}
|
||||||
|
|
||||||
func (r *annotationRepository) new(userID, itemType string, itemID string) *annotation {
|
func (r *annotationRepository) new(userID, itemType string, itemID string) *annotation {
|
||||||
id, _ := uuid.NewRandom()
|
id, _ := uuid.NewRandom()
|
||||||
return &annotation{
|
return &annotation{
|
||||||
|
|||||||
@@ -26,6 +26,9 @@ func (r *sqlRepository) newQuery(options ...model.QueryOptions) orm.QuerySeter {
|
|||||||
q = q.OrderBy(opts.Sort)
|
q = q.OrderBy(opts.Sort)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for field, value := range opts.Filters {
|
||||||
|
q = q.Filter(field, value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return q
|
return q
|
||||||
}
|
}
|
||||||
@@ -46,9 +49,6 @@ func (r *sqlRepository) newRawQuery(options ...model.QueryOptions) squirrel.Sele
|
|||||||
sq = sq.OrderBy(options[0].Sort)
|
sq = sq.OrderBy(options[0].Sort)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for field, value := range options[0].Filters {
|
|
||||||
sq = sq.Where(squirrel.Like{field: value.(string) + "%"})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return sq
|
return sq
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user