Add MediaFile to Bookmark

This commit is contained in:
Deluan 2020-07-31 16:03:27 -04:00 committed by Deluan Quintão
parent 34e843a4b3
commit 3d0e70e907
3 changed files with 16 additions and 13 deletions

View file

@ -27,7 +27,7 @@ type PlayQueueRepository interface {
} }
type Bookmark struct { type Bookmark struct {
ID string `json:"id" orm:"column(id)"` Item MediaFile `json:"item"`
Comment string `json:"comment"` Comment string `json:"comment"`
Position int64 `json:"position"` Position int64 `json:"position"`
CreatedAt time.Time `json:"createdAt"` CreatedAt time.Time `json:"createdAt"`

View file

@ -112,7 +112,8 @@ func (r *playQueueRepository) GetBookmarks(userId string) (model.Bookmarks, erro
} }
bms := make(model.Bookmarks, len(pqs)) bms := make(model.Bookmarks, len(pqs))
for i := range pqs { for i := range pqs {
bms[i].ID = pqs[i].Current items := r.loadTracks(model.MediaFiles{{ID: pqs[i].Current}})
bms[i].Item = items[0]
bms[i].Comment = pqs[i].Comment bms[i].Comment = pqs[i].Comment
bms[i].Position = int64(pqs[i].Position) bms[i].Position = int64(pqs[i].Position)
bms[i].CreatedAt = pqs[i].CreatedAt bms[i].CreatedAt = pqs[i].CreatedAt
@ -165,18 +166,18 @@ func (r *playQueueRepository) toModel(pq *playQueue) model.PlayQueue {
q.Items = append(q.Items, model.MediaFile{ID: t}) q.Items = append(q.Items, model.MediaFile{ID: t})
} }
} }
q.Items = r.loadTracks(&q) q.Items = r.loadTracks(q.Items)
return q return q
} }
func (r *playQueueRepository) loadTracks(p *model.PlayQueue) model.MediaFiles { func (r *playQueueRepository) loadTracks(tracks model.MediaFiles) model.MediaFiles {
if len(p.Items) == 0 { if len(tracks) == 0 {
return nil return nil
} }
// Collect all ids // Collect all ids
ids := make([]string, len(p.Items)) ids := make([]string, len(tracks))
for i, t := range p.Items { for i, t := range tracks {
ids[i] = t.ID ids[i] = t.ID
} }
@ -200,7 +201,7 @@ func (r *playQueueRepository) loadTracks(p *model.PlayQueue) model.MediaFiles {
tracks, err := mfRepo.GetAll(model.QueryOptions{Filters: idsFilter}) tracks, err := mfRepo.GetAll(model.QueryOptions{Filters: idsFilter})
if err != nil { if err != nil {
u := loggedUser(r.ctx) u := loggedUser(r.ctx)
log.Error(r.ctx, "Could not load playqueue's tracks", "user", u.UserName, err) log.Error(r.ctx, "Could not load playqueue/bookmark's tracks", "user", u.UserName, err)
} }
for _, t := range tracks { for _, t := range tracks {
trackMap[t.ID] = t trackMap[t.ID] = t
@ -208,8 +209,8 @@ func (r *playQueueRepository) loadTracks(p *model.PlayQueue) model.MediaFiles {
} }
// Create a new list of tracks with the same order as the original // Create a new list of tracks with the same order as the original
newTracks := make(model.MediaFiles, len(p.Items)) newTracks := make(model.MediaFiles, len(tracks))
for i, t := range p.Items { for i, t := range tracks {
newTracks[i] = trackMap[t.ID] newTracks[i] = trackMap[t.ID]
} }
return newTracks return newTracks

View file

@ -66,7 +66,8 @@ var _ = Describe("PlayQueueRepository", func() {
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(bms).To(HaveLen(1)) Expect(bms).To(HaveLen(1))
Expect(bms[0].ID).To(Equal(songAntenna.ID)) Expect(bms[0].Item.ID).To(Equal(songAntenna.ID))
Expect(bms[0].Item.Title).To(Equal(songAntenna.Title))
Expect(bms[0].Comment).To(Equal("this is a comment")) Expect(bms[0].Comment).To(Equal("this is a comment"))
Expect(bms[0].Position).To(Equal(int64(123))) Expect(bms[0].Position).To(Equal(int64(123)))
@ -79,7 +80,7 @@ var _ = Describe("PlayQueueRepository", func() {
bms, err = repo.GetBookmarks("user5") bms, err = repo.GetBookmarks("user5")
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(bms[0].ID).To(Equal(songAntenna.ID)) Expect(bms[0].Item.ID).To(Equal(songAntenna.ID))
Expect(bms[0].Comment).To(Equal("another comment")) Expect(bms[0].Comment).To(Equal("another comment"))
Expect(bms[0].Position).To(Equal(int64(333))) Expect(bms[0].Position).To(Equal(int64(333)))
Expect(bms[0].CreatedAt).To(Equal(created)) Expect(bms[0].CreatedAt).To(Equal(created))
@ -96,7 +97,8 @@ var _ = Describe("PlayQueueRepository", func() {
bms, err = repo.GetBookmarks("user5") bms, err = repo.GetBookmarks("user5")
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(bms).To(HaveLen(1)) Expect(bms).To(HaveLen(1))
Expect(bms[0].ID).To(Equal(songComeTogether.ID)) Expect(bms[0].Item.ID).To(Equal(songComeTogether.ID))
Expect(bms[0].Item.Title).To(Equal(songComeTogether.Title))
}) })
}) })
}) })