refactor: remove annotation handling from engine
This commit is contained in:
@@ -101,7 +101,7 @@ func (r *albumRepository) PurgeEmpty() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *albumRepository) GetStarred(userId string, options ...model.QueryOptions) (model.Albums, error) {
|
||||
func (r *albumRepository) GetStarred(options ...model.QueryOptions) (model.Albums, error) {
|
||||
sq := r.selectAlbum(options...).Where("starred = true")
|
||||
var starred model.Albums
|
||||
err := r.queryAll(sq, &starred)
|
||||
|
||||
@@ -57,7 +57,7 @@ var _ = Describe("AlbumRepository", func() {
|
||||
|
||||
Describe("GetStarred", func() {
|
||||
It("returns all starred records", func() {
|
||||
Expect(repo.GetStarred("userid", model.QueryOptions{})).To(Equal(model.Albums{
|
||||
Expect(repo.GetStarred(model.QueryOptions{})).To(Equal(model.Albums{
|
||||
albumRadioactivity,
|
||||
}))
|
||||
})
|
||||
|
||||
@@ -10,24 +10,6 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type annotation struct {
|
||||
AnnotationID string `orm:"pk;column(ann_id)"`
|
||||
UserID string `orm:"column(user_id)"`
|
||||
ItemID string `orm:"column(item_id)"`
|
||||
ItemType string `orm:"column(item_type)"`
|
||||
PlayCount int `orm:"column(play_count);index;null"`
|
||||
PlayDate time.Time `orm:"column(play_date);index;null"`
|
||||
Rating int `orm:"null"`
|
||||
Starred bool `orm:"index"`
|
||||
StarredAt time.Time `orm:"column(starred_at);null"`
|
||||
}
|
||||
|
||||
func (u *annotation) TableUnique() [][]string {
|
||||
return [][]string{
|
||||
{"UserID", "ItemID", "ItemType"},
|
||||
}
|
||||
}
|
||||
|
||||
type annotationRepository struct {
|
||||
sqlRepository
|
||||
}
|
||||
@@ -40,144 +22,70 @@ func NewAnnotationRepository(ctx context.Context, o orm.Ormer) model.AnnotationR
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *annotationRepository) Get(userID, itemType string, itemID string) (*model.Annotation, error) {
|
||||
q := Select("*").From(r.tableName).Where(And{
|
||||
Eq{"user_id": userId(r.ctx)},
|
||||
Eq{"item_type": itemType},
|
||||
Eq{"item_id": itemID},
|
||||
})
|
||||
var ann annotation
|
||||
err := r.queryOne(q, &ann)
|
||||
if err == model.ErrNotFound {
|
||||
return nil, nil
|
||||
func (r *annotationRepository) upsert(values map[string]interface{}, itemType string, itemIDs ...string) error {
|
||||
upd := Update(r.tableName).Where(r.getId(itemType, itemIDs...))
|
||||
for f, v := range values {
|
||||
upd = upd.Set(f, v)
|
||||
}
|
||||
resp := model.Annotation(ann)
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func (r *annotationRepository) GetMap(userID, itemType string, itemIDs []string) (model.AnnotationMap, error) {
|
||||
if len(itemIDs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
q := Select("*").From(r.tableName).Where(And{
|
||||
Eq{"user_id": userId(r.ctx)},
|
||||
Eq{"item_type": itemType},
|
||||
Eq{"item_id": itemIDs},
|
||||
})
|
||||
var res []annotation
|
||||
err := r.queryAll(q, &res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m := make(model.AnnotationMap)
|
||||
for _, a := range res {
|
||||
m[a.ItemID] = model.Annotation(a)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (r *annotationRepository) GetAll(userID, itemType string, options ...model.QueryOptions) ([]model.Annotation, error) {
|
||||
q := Select("*").From(r.tableName).Where(And{
|
||||
Eq{"user_id": userId(r.ctx)},
|
||||
Eq{"item_type": itemType},
|
||||
})
|
||||
var res []annotation
|
||||
err := r.queryAll(q, &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 {
|
||||
id, _ := uuid.NewRandom()
|
||||
return &annotation{
|
||||
AnnotationID: id.String(),
|
||||
UserID: userID,
|
||||
ItemID: itemID,
|
||||
ItemType: itemType,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *annotationRepository) IncPlayCount(userID, itemType string, itemID string, ts time.Time) error {
|
||||
uid := userId(r.ctx)
|
||||
q := Update(r.tableName).
|
||||
Set("play_count", Expr("play_count + 1")).
|
||||
Set("play_date", ts).
|
||||
Where(And{
|
||||
Eq{"user_id": uid},
|
||||
Eq{"item_type": itemType},
|
||||
Eq{"item_id": itemID},
|
||||
})
|
||||
c, err := r.executeSQL(q)
|
||||
c, err := r.executeSQL(upd)
|
||||
if c == 0 || err == orm.ErrNoRows {
|
||||
ann := r.new(uid, itemType, itemID)
|
||||
ann.PlayCount = 1
|
||||
ann.PlayDate = ts
|
||||
_, err = r.ormer.Insert(ann)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *annotationRepository) SetStar(starred bool, userID, itemType string, ids ...string) error {
|
||||
uid := userId(r.ctx)
|
||||
var starredAt time.Time
|
||||
if starred {
|
||||
starredAt = time.Now()
|
||||
}
|
||||
q := Update(r.tableName).
|
||||
Set("starred", starred).
|
||||
Set("starred_at", starredAt).
|
||||
Where(And{
|
||||
Eq{"user_id": uid},
|
||||
Eq{"item_type": itemType},
|
||||
Eq{"item_id": ids},
|
||||
})
|
||||
c, err := r.executeSQL(q)
|
||||
if c == 0 || err == orm.ErrNoRows {
|
||||
for _, id := range ids {
|
||||
ann := r.new(uid, itemType, id)
|
||||
ann.Starred = starred
|
||||
ann.StarredAt = starredAt
|
||||
_, err = r.ormer.Insert(ann)
|
||||
for _, itemID := range itemIDs {
|
||||
id, _ := uuid.NewRandom()
|
||||
values["ann_id"] = id.String()
|
||||
values["user_id"] = userId(r.ctx)
|
||||
values["item_type"] = itemType
|
||||
values["item_id"] = itemID
|
||||
ins := Insert(r.tableName).SetMap(values)
|
||||
_, err = r.executeSQL(ins)
|
||||
if err != nil {
|
||||
if err.Error() != "LastInsertId is not supported by this driver" {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *annotationRepository) SetRating(rating int, userID, itemType string, itemID string) error {
|
||||
uid := userId(r.ctx)
|
||||
q := Update(r.tableName).
|
||||
Set("rating", rating).
|
||||
Where(And{
|
||||
Eq{"user_id": uid},
|
||||
Eq{"item_type": itemType},
|
||||
Eq{"item_id": itemID},
|
||||
})
|
||||
c, err := r.executeSQL(q)
|
||||
func (r *annotationRepository) IncPlayCount(itemType, itemID string, ts time.Time) error {
|
||||
upd := Update(r.tableName).Where(r.getId(itemType, itemID)).
|
||||
Set("play_count", Expr("play_count+1")).
|
||||
Set("play_date", ts)
|
||||
c, err := r.executeSQL(upd)
|
||||
|
||||
if c == 0 || err == orm.ErrNoRows {
|
||||
ann := r.new(uid, itemType, itemID)
|
||||
ann.Rating = rating
|
||||
_, err = r.ormer.Insert(ann)
|
||||
id, _ := uuid.NewRandom()
|
||||
values := map[string]interface{}{}
|
||||
values["ann_id"] = id.String()
|
||||
values["user_id"] = userId(r.ctx)
|
||||
values["item_type"] = itemType
|
||||
values["item_id"] = itemID
|
||||
values["play_count"] = 1
|
||||
values["play_date"] = ts
|
||||
ins := Insert(r.tableName).SetMap(values)
|
||||
_, err = r.executeSQL(ins)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *annotationRepository) Delete(userID, itemType string, ids ...string) error {
|
||||
return r.delete(And{
|
||||
func (r *annotationRepository) getId(itemType string, itemID ...string) And {
|
||||
return And{
|
||||
Eq{"user_id": userId(r.ctx)},
|
||||
Eq{"item_type": itemType},
|
||||
Eq{"item_id": ids},
|
||||
})
|
||||
Eq{"item_id": itemID},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *annotationRepository) SetStar(starred bool, itemType string, ids ...string) error {
|
||||
starredAt := time.Now()
|
||||
return r.upsert(map[string]interface{}{"starred": starred, "starred_at": starredAt}, itemType, ids...)
|
||||
}
|
||||
|
||||
func (r *annotationRepository) SetRating(rating int, itemType, itemID string) error {
|
||||
return r.upsert(map[string]interface{}{"rating": rating}, itemType, itemID)
|
||||
}
|
||||
|
||||
func (r *annotationRepository) Delete(itemType string, itemIDs ...string) error {
|
||||
return r.delete(r.getId(itemType, itemIDs...))
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ func (r *artistRepository) Refresh(ids ...string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *artistRepository) GetStarred(userId string, options ...model.QueryOptions) (model.Artists, error) {
|
||||
func (r *artistRepository) GetStarred(options ...model.QueryOptions) (model.Artists, error) {
|
||||
return nil, nil // TODO
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ func (r *artistRepository) Search(q string, offset int, size int) (model.Artists
|
||||
}
|
||||
|
||||
func (r *artistRepository) Count(options ...rest.QueryOptions) (int64, error) {
|
||||
return r.CountAll(r.parseRestOptions(options...))
|
||||
return r.CountAll(r.parseRestOptions(options...)) // TODO Don't apply pagination!
|
||||
}
|
||||
|
||||
func (r *artistRepository) Read(id string) (interface{}, error) {
|
||||
|
||||
@@ -78,7 +78,7 @@ func (r mediaFileRepository) FindByPath(path string) (model.MediaFiles, error) {
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) GetStarred(userId string, options ...model.QueryOptions) (model.MediaFiles, error) {
|
||||
func (r mediaFileRepository) GetStarred(options ...model.QueryOptions) (model.MediaFiles, error) {
|
||||
sq := r.selectMediaFile(options...).Where("starred = true")
|
||||
var starred model.MediaFiles
|
||||
err := r.queryAll(sq, &starred)
|
||||
|
||||
@@ -44,7 +44,7 @@ var _ = Describe("MediaRepository", func() {
|
||||
})
|
||||
|
||||
It("returns empty array when no tracks are found", func() {
|
||||
Expect(mr.FindByAlbum("67")).To(Equal(model.MediaFiles{}))
|
||||
Expect(mr.FindByAlbum("67")).To(Equal(model.MediaFiles(nil)))
|
||||
})
|
||||
|
||||
It("finds tracks by path", func() {
|
||||
@@ -54,7 +54,7 @@ var _ = Describe("MediaRepository", func() {
|
||||
})
|
||||
|
||||
It("returns starred tracks", func() {
|
||||
Expect(mr.GetStarred("userid")).To(Equal(model.MediaFiles{
|
||||
Expect(mr.GetStarred()).To(Equal(model.MediaFiles{
|
||||
songComeTogether,
|
||||
}))
|
||||
})
|
||||
|
||||
@@ -125,17 +125,8 @@ func (db *NewSQLStore) getOrmer() orm.Ormer {
|
||||
}
|
||||
|
||||
func initORM(dbPath string) error {
|
||||
//verbose := conf.Server.LogLevel == "trace"
|
||||
//orm.Debug = verbose
|
||||
if strings.Contains(dbPath, "postgres") {
|
||||
driver = "postgres"
|
||||
}
|
||||
err := orm.RegisterDataBase("default", driver, dbPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO Remove all RegisterModels (i.e. don't use orm.Insert/Update)
|
||||
orm.RegisterModel(new(annotation))
|
||||
|
||||
return nil
|
||||
return orm.RegisterDataBase("default", driver, dbPath)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/deluan/navidrome/conf"
|
||||
"github.com/deluan/navidrome/db"
|
||||
@@ -52,8 +53,8 @@ var testSongs = model.MediaFiles{
|
||||
songAntenna,
|
||||
}
|
||||
|
||||
var annAlbumRadioactivity = model.Annotation{AnnotationID: "1", UserID: "userid", ItemType: model.AlbumItemType, ItemID: "3", Starred: true}
|
||||
var annSongComeTogether = model.Annotation{AnnotationID: "2", UserID: "userid", ItemType: model.MediaItemType, ItemID: "2", Starred: true}
|
||||
var annAlbumRadioactivity = model.Annotation{AnnID: "1", UserID: "userid", ItemType: model.AlbumItemType, ItemID: "3", Starred: true}
|
||||
var annSongComeTogether = model.Annotation{AnnID: "2", UserID: "userid", ItemType: model.MediaItemType, ItemID: "2", Starred: true}
|
||||
var testAnnotations = []model.Annotation{
|
||||
annAlbumRadioactivity,
|
||||
annSongComeTogether,
|
||||
@@ -81,7 +82,6 @@ var _ = Describe("Initialize test DB", func() {
|
||||
BeforeSuite(func() {
|
||||
o := orm.NewOrm()
|
||||
mr := NewMediaFileRepository(nil, o)
|
||||
|
||||
for _, s := range testSongs {
|
||||
err := mr.Put(&s)
|
||||
if err != nil {
|
||||
@@ -90,8 +90,13 @@ var _ = Describe("Initialize test DB", func() {
|
||||
}
|
||||
|
||||
for _, a := range testAnnotations {
|
||||
ann := annotation(a)
|
||||
_, err := o.Insert(&ann)
|
||||
values, _ := toSqlArgs(a)
|
||||
ins := squirrel.Insert("annotation").SetMap(values)
|
||||
query, args, err := ins.ToSql()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
_, err = o.Raw(query, args...).Exec()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -55,9 +55,9 @@ func (r *sqlRepository) applyOptions(sq SelectBuilder, options ...model.QueryOpt
|
||||
}
|
||||
if options[0].Sort != "" {
|
||||
if options[0].Order == "desc" {
|
||||
sq = sq.OrderBy(options[0].Sort + " desc")
|
||||
sq = sq.OrderBy(toSnakeCase(options[0].Sort + " desc"))
|
||||
} else {
|
||||
sq = sq.OrderBy(options[0].Sort)
|
||||
sq = sq.OrderBy(toSnakeCase(options[0].Sort))
|
||||
}
|
||||
}
|
||||
if options[0].Filters != nil {
|
||||
@@ -151,7 +151,7 @@ func (r sqlRepository) toSql(sq Sqlizer) (string, []interface{}, error) {
|
||||
func (r sqlRepository) parseRestOptions(options ...rest.QueryOptions) model.QueryOptions {
|
||||
qo := model.QueryOptions{}
|
||||
if len(options) > 0 {
|
||||
qo.Sort = toSnakeCase(options[0].Sort)
|
||||
qo.Sort = options[0].Sort
|
||||
qo.Order = options[0].Order
|
||||
qo.Max = options[0].Max
|
||||
qo.Offset = options[0].Offset
|
||||
|
||||
Reference in New Issue
Block a user