Reduce number of queries for some playlists operations.

Also allow admins to update/delete playlists from other users in the Subsonic API. Closes #1366
This commit is contained in:
Deluan
2021-10-16 13:47:10 -04:00
committed by Deluan Quintão
parent 943082ef4e
commit d200933b68
7 changed files with 26 additions and 38 deletions
+4 -4
View File
@@ -105,6 +105,10 @@ func (r *playlistRepository) Put(p *model.Playlist) error {
}
func (r *playlistRepository) Get(id string) (*model.Playlist, error) {
return r.findBy(And{Eq{"id": id}, r.userFilter()}, false)
}
func (r *playlistRepository) GetWithTracks(id string) (*model.Playlist, error) {
return r.findBy(And{Eq{"id": id}, r.userFilter()}, true)
}
@@ -112,10 +116,6 @@ func (r *playlistRepository) FindByPath(path string) (*model.Playlist, error) {
return r.findBy(Eq{"path": path}, false)
}
func (r *playlistRepository) FindByID(id string) (*model.Playlist, error) {
return r.findBy(And{Eq{"id": id}, r.userFilter()}, false)
}
func (r *playlistRepository) findBy(sql Sqlizer, includeTracks bool) (*model.Playlist, error) {
sel := r.newSelect().Columns("*").Where(sql)
var pls []dbPlaylist
+2 -2
View File
@@ -55,7 +55,7 @@ var _ = Describe("PlaylistRepository", func() {
Expect(err).To(MatchError(model.ErrNotFound))
})
It("returns all tracks", func() {
pls, err := repo.Get(plsBest.ID)
pls, err := repo.GetWithTracks(plsBest.ID)
Expect(err).To(BeNil())
Expect(pls.Name).To(Equal(plsBest.Name))
Expect(pls.Tracks).To(Equal(model.MediaFiles{
@@ -76,7 +76,7 @@ var _ = Describe("PlaylistRepository", func() {
By("adds repeated songs to a playlist and keeps the order")
newPls.Tracks = append(newPls.Tracks, model.MediaFile{ID: "1004"})
Expect(repo.Put(&newPls)).To(BeNil())
saved, _ := repo.Get(newPls.ID)
saved, _ := repo.GetWithTracks(newPls.ID)
Expect(saved.Tracks).To(HaveLen(3))
Expect(saved.Tracks[0].ID).To(Equal("1004"))
Expect(saved.Tracks[1].ID).To(Equal("1003"))
+11 -4
View File
@@ -48,8 +48,8 @@ func (r *playlistTrackRepository) Read(id string) (interface{}, error) {
return &trk, err
}
func (r *playlistTrackRepository) ReadAll(options ...rest.QueryOptions) (interface{}, error) {
sel := r.newSelect(r.parseRestOptions(options...)).
func (r *playlistTrackRepository) GetAll(options ...model.QueryOptions) (model.PlaylistTracks, error) {
sel := r.newSelect(options...).
LeftJoin("annotation on ("+
"annotation.item_id = media_file_id"+
" AND annotation.item_type = 'media_file'"+
@@ -62,6 +62,10 @@ func (r *playlistTrackRepository) ReadAll(options ...rest.QueryOptions) (interfa
return res, err
}
func (r *playlistTrackRepository) ReadAll(options ...rest.QueryOptions) (interface{}, error) {
return r.GetAll(r.parseRestOptions(options...))
}
func (r *playlistTrackRepository) EntityName() string {
return "playlist_tracks"
}
@@ -211,7 +215,10 @@ func (r *playlistTrackRepository) Delete(id string) error {
if err != nil {
return err
}
return r.updateStats()
// To renumber the playlist
_, err = r.Add(nil)
return err
}
func (r *playlistTrackRepository) Reorder(pos int, newPos int) error {
@@ -231,7 +238,7 @@ func (r *playlistTrackRepository) isWritable() bool {
if usr.IsAdmin {
return true
}
pls, err := r.playlistRepo.FindByID(r.playlistId)
pls, err := r.playlistRepo.Get(r.playlistId)
return err == nil && pls.Owner == usr.UserName
}