Add bookmark in persistence layer
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/deluan/navidrome/log"
|
||||
"github.com/deluan/navidrome/model"
|
||||
"github.com/deluan/navidrome/model/request"
|
||||
)
|
||||
|
||||
type playQueueRepository struct {
|
||||
@@ -28,7 +29,7 @@ type playQueue struct {
|
||||
UserID string `orm:"column(user_id)"`
|
||||
Comment string
|
||||
Current string
|
||||
Position float32
|
||||
Position int64
|
||||
ChangedBy string
|
||||
Items string
|
||||
CreatedAt time.Time
|
||||
@@ -63,6 +64,66 @@ func (r *playQueueRepository) Retrieve(userId string) (*model.PlayQueue, error)
|
||||
return &pls, err
|
||||
}
|
||||
|
||||
func (r *playQueueRepository) AddBookmark(userId, id, comment string, position int64) error {
|
||||
u := loggedUser(r.ctx)
|
||||
client, _ := request.ClientFrom(r.ctx)
|
||||
bm := &playQueue{
|
||||
UserID: userId,
|
||||
Comment: comment,
|
||||
Current: id,
|
||||
Position: position,
|
||||
ChangedBy: client,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
|
||||
sel := r.newSelect().Column("id").Where(And{
|
||||
Eq{"user_id": userId},
|
||||
Eq{"items": ""},
|
||||
Eq{"current": id},
|
||||
})
|
||||
var prev model.PlayQueue
|
||||
err := r.queryOne(sel, &prev)
|
||||
if err != nil && err != model.ErrNotFound {
|
||||
log.Error(r.ctx, "Error retrieving previous bookmark", "user", u.UserName, err, "mediaFileId", id, err)
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = r.put(prev.ID, bm)
|
||||
if err != nil {
|
||||
log.Error(r.ctx, "Error saving bookmark", "user", u.UserName, err, "mediaFileId", id, err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *playQueueRepository) GetBookmarks(userId string) (model.Bookmarks, error) {
|
||||
u := loggedUser(r.ctx)
|
||||
sel := r.newSelect().Column("*").Where(And{Eq{"user_id": userId}, Eq{"items": ""}})
|
||||
var pqs model.PlayQueues
|
||||
err := r.queryAll(sel, &pqs)
|
||||
if err != nil {
|
||||
log.Error(r.ctx, "Error retrieving bookmarks", "user", u.UserName, err)
|
||||
return nil, err
|
||||
}
|
||||
bms := make(model.Bookmarks, len(pqs))
|
||||
for i := range pqs {
|
||||
bms[i].ID = pqs[i].Current
|
||||
bms[i].Comment = pqs[i].Comment
|
||||
bms[i].Position = int64(pqs[i].Position)
|
||||
bms[i].CreatedAt = pqs[i].CreatedAt
|
||||
}
|
||||
return bms, nil
|
||||
}
|
||||
|
||||
func (r *playQueueRepository) DeleteBookmark(userId, id string) error {
|
||||
return r.delete(And{
|
||||
Eq{"user_id": userId},
|
||||
Eq{"items": ""},
|
||||
Eq{"current": id},
|
||||
})
|
||||
}
|
||||
|
||||
func (r *playQueueRepository) fromModel(q *model.PlayQueue) playQueue {
|
||||
pq := playQueue{
|
||||
ID: q.ID,
|
||||
|
||||
@@ -23,32 +23,76 @@ var _ = Describe("PlayQueueRepository", func() {
|
||||
repo = NewPlayQueueRepository(ctx, orm.NewOrm())
|
||||
})
|
||||
|
||||
It("returns notfound error if there's no playqueue for the user", func() {
|
||||
_, err := repo.Retrieve("user999")
|
||||
Expect(err).To(MatchError(model.ErrNotFound))
|
||||
Describe("PlayQueues", func() {
|
||||
It("returns notfound error if there's no playqueue for the user", func() {
|
||||
_, err := repo.Retrieve("user999")
|
||||
Expect(err).To(MatchError(model.ErrNotFound))
|
||||
})
|
||||
|
||||
It("stores and retrieves the playqueue for the user", func() {
|
||||
By("Storing a playqueue for the user")
|
||||
|
||||
expected := aPlayQueue("user1", songDayInALife.ID, 123, songComeTogether, songDayInALife)
|
||||
Expect(repo.Store(expected)).To(BeNil())
|
||||
|
||||
actual, err := repo.Retrieve("user1")
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
AssertPlayQueue(expected, actual)
|
||||
|
||||
By("Storing a new playqueue for the same user")
|
||||
|
||||
new := aPlayQueue("user1", songRadioactivity.ID, 321, songAntenna, songRadioactivity)
|
||||
Expect(repo.Store(new)).To(BeNil())
|
||||
|
||||
actual, err = repo.Retrieve("user1")
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
AssertPlayQueue(new, actual)
|
||||
Expect(countPlayQueues(repo, "user1")).To(Equal(1))
|
||||
})
|
||||
})
|
||||
|
||||
It("stores and retrieves the playqueue for the user", func() {
|
||||
By("Storing a playqueue for the user")
|
||||
Describe("Bookmarks", func() {
|
||||
It("returns an empty collection if there are no bookmarks", func() {
|
||||
Expect(repo.GetBookmarks("user999")).To(BeEmpty())
|
||||
})
|
||||
|
||||
expected := aPlayQueue("user1", songDayInALife.ID, 123, songComeTogether, songDayInALife)
|
||||
Expect(repo.Store(expected)).To(BeNil())
|
||||
It("saves and overrides bookmarks", func() {
|
||||
By("Saving the bookmark")
|
||||
Expect(repo.AddBookmark("user5", songAntenna.ID, "this is a comment", 123)).To(BeNil())
|
||||
|
||||
actual, err := repo.Retrieve("user1")
|
||||
Expect(err).To(BeNil())
|
||||
bms, err := repo.GetBookmarks("user5")
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
AssertPlayQueue(expected, actual)
|
||||
Expect(bms).To(HaveLen(1))
|
||||
Expect(bms[0].ID).To(Equal(songAntenna.ID))
|
||||
Expect(bms[0].Comment).To(Equal("this is a comment"))
|
||||
Expect(bms[0].Position).To(Equal(int64(123)))
|
||||
|
||||
By("Storing a new playqueue for the same user")
|
||||
By("Overriding the bookmark")
|
||||
Expect(repo.AddBookmark("user5", songAntenna.ID, "another comment", 333)).To(BeNil())
|
||||
|
||||
new := aPlayQueue("user1", songRadioactivity.ID, 321, songAntenna, songRadioactivity)
|
||||
Expect(repo.Store(new)).To(BeNil())
|
||||
bms, err = repo.GetBookmarks("user5")
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
actual, err = repo.Retrieve("user1")
|
||||
Expect(err).To(BeNil())
|
||||
Expect(bms[0].ID).To(Equal(songAntenna.ID))
|
||||
Expect(bms[0].Comment).To(Equal("another comment"))
|
||||
Expect(bms[0].Position).To(Equal(int64(333)))
|
||||
|
||||
AssertPlayQueue(new, actual)
|
||||
Expect(countPlayQueues(repo, "user1")).To(Equal(1))
|
||||
By("Saving another bookmark")
|
||||
Expect(repo.AddBookmark("user5", songComeTogether.ID, "one more comment", 444)).To(BeNil())
|
||||
bms, err = repo.GetBookmarks("user5")
|
||||
Expect(err).To(BeNil())
|
||||
Expect(bms).To(HaveLen(2))
|
||||
|
||||
By("Delete bookmark")
|
||||
Expect(repo.DeleteBookmark("user5", songAntenna.ID))
|
||||
bms, err = repo.GetBookmarks("user5")
|
||||
Expect(err).To(BeNil())
|
||||
Expect(bms).To(HaveLen(1))
|
||||
Expect(bms[0].ID).To(Equal(songComeTogether.ID))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -74,7 +118,7 @@ func AssertPlayQueue(expected, actual *model.PlayQueue) {
|
||||
}
|
||||
}
|
||||
|
||||
func aPlayQueue(userId, current string, position float32, items ...model.MediaFile) *model.PlayQueue {
|
||||
func aPlayQueue(userId, current string, position int64, items ...model.MediaFile) *model.PlayQueue {
|
||||
createdAt := time.Now()
|
||||
updatedAt := createdAt.Add(time.Minute)
|
||||
id, _ := uuid.NewRandom()
|
||||
|
||||
Reference in New Issue
Block a user