Skip to content

Commit

Permalink
Add MediaFile to Bookmark
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Jul 31, 2020
1 parent 34e843a commit 3d0e70e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion model/playqueue.go
Expand Up @@ -27,7 +27,7 @@ type PlayQueueRepository interface {
}

type Bookmark struct {
ID string `json:"id" orm:"column(id)"`
Item MediaFile `json:"item"`
Comment string `json:"comment"`
Position int64 `json:"position"`
CreatedAt time.Time `json:"createdAt"`
Expand Down
19 changes: 10 additions & 9 deletions persistence/playqueue_repository.go
Expand Up @@ -112,7 +112,8 @@ func (r *playQueueRepository) GetBookmarks(userId string) (model.Bookmarks, erro
}
bms := make(model.Bookmarks, len(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].Position = int64(pqs[i].Position)
bms[i].CreatedAt = pqs[i].CreatedAt
Expand Down Expand Up @@ -165,18 +166,18 @@ func (r *playQueueRepository) toModel(pq *playQueue) model.PlayQueue {
q.Items = append(q.Items, model.MediaFile{ID: t})
}
}
q.Items = r.loadTracks(&q)
q.Items = r.loadTracks(q.Items)
return q
}

func (r *playQueueRepository) loadTracks(p *model.PlayQueue) model.MediaFiles {
if len(p.Items) == 0 {
func (r *playQueueRepository) loadTracks(tracks model.MediaFiles) model.MediaFiles {
if len(tracks) == 0 {
return nil
}

// Collect all ids
ids := make([]string, len(p.Items))
for i, t := range p.Items {
ids := make([]string, len(tracks))
for i, t := range tracks {
ids[i] = t.ID
}

Expand All @@ -200,16 +201,16 @@ func (r *playQueueRepository) loadTracks(p *model.PlayQueue) model.MediaFiles {
tracks, err := mfRepo.GetAll(model.QueryOptions{Filters: idsFilter})
if err != nil {
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 {
trackMap[t.ID] = t
}
}

// Create a new list of tracks with the same order as the original
newTracks := make(model.MediaFiles, len(p.Items))
for i, t := range p.Items {
newTracks := make(model.MediaFiles, len(tracks))
for i, t := range tracks {
newTracks[i] = trackMap[t.ID]
}
return newTracks
Expand Down
8 changes: 5 additions & 3 deletions persistence/playqueue_repository_test.go
Expand Up @@ -66,7 +66,8 @@ var _ = Describe("PlayQueueRepository", func() {
Expect(err).To(BeNil())

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].Position).To(Equal(int64(123)))

Expand All @@ -79,7 +80,7 @@ var _ = Describe("PlayQueueRepository", func() {
bms, err = repo.GetBookmarks("user5")
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].Position).To(Equal(int64(333)))
Expect(bms[0].CreatedAt).To(Equal(created))
Expand All @@ -96,7 +97,8 @@ var _ = Describe("PlayQueueRepository", func() {
bms, err = repo.GetBookmarks("user5")
Expect(err).To(BeNil())
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))
})
})
})
Expand Down

0 comments on commit 3d0e70e

Please sign in to comment.