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
+3 -3
View File
@@ -46,7 +46,7 @@ func handleExportPlaylist(ds model.DataStore) http.HandlerFunc {
ctx := r.Context()
plsRepo := ds.Playlist(ctx)
plsId := chi.URLParam(r, "playlistId")
pls, err := plsRepo.Get(plsId)
pls, err := plsRepo.GetWithTracks(plsId)
if err == model.ErrNotFound {
log.Warn("Playlist not found", "playlistId", plsId)
http.Error(w, "not found", http.StatusNotFound)
@@ -114,13 +114,13 @@ func addToPlaylist(ds model.DataStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
playlistId := utils.ParamString(r, ":playlistId")
tracksRepo := ds.Playlist(r.Context()).Tracks(playlistId)
var payload addTracksPayload
err := json.NewDecoder(r.Body).Decode(&payload)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
tracksRepo := ds.Playlist(r.Context()).Tracks(playlistId)
count, c := 0, 0
if c, err = tracksRepo.Add(payload.Ids); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
@@ -163,7 +163,6 @@ func reorderItem(ds model.DataStore) http.HandlerFunc {
http.Error(w, "invalid id", http.StatusBadRequest)
return
}
tracksRepo := ds.Playlist(r.Context()).Tracks(playlistId)
var payload reorderPayload
err := json.NewDecoder(r.Body).Decode(&payload)
if err != nil {
@@ -175,6 +174,7 @@ func reorderItem(ds model.DataStore) http.HandlerFunc {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
tracksRepo := ds.Playlist(r.Context()).Tracks(playlistId)
err = tracksRepo.Reorder(id, newPos)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
+3 -23
View File
@@ -46,7 +46,7 @@ func (c *PlaylistsController) GetPlaylist(w http.ResponseWriter, r *http.Request
}
func (c *PlaylistsController) getPlaylist(ctx context.Context, id string) (*responses.Subsonic, error) {
pls, err := c.ds.Playlist(ctx).Get(id)
pls, err := c.ds.Playlist(ctx).GetWithTracks(id)
switch {
case err == model.ErrNotFound:
log.Error(ctx, err.Error(), "id", id)
@@ -110,27 +110,12 @@ func (c *PlaylistsController) CreatePlaylist(w http.ResponseWriter, r *http.Requ
return c.getPlaylist(ctx, id)
}
func (c *PlaylistsController) delete(ctx context.Context, playlistId string) error {
return c.ds.WithTx(func(tx model.DataStore) error {
pls, err := tx.Playlist(ctx).Get(playlistId)
if err != nil {
return err
}
owner := getUser(ctx)
if owner != pls.Owner {
return model.ErrNotAuthorized
}
return tx.Playlist(ctx).Delete(playlistId)
})
}
func (c *PlaylistsController) DeletePlaylist(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
id, err := requiredParamString(r, "id")
if err != nil {
return nil, err
}
err = c.delete(r.Context(), id)
err = c.ds.Playlist(r.Context()).Delete(id)
if err == model.ErrNotAuthorized {
return nil, newError(responses.ErrorAuthorizationFail)
}
@@ -143,16 +128,11 @@ func (c *PlaylistsController) DeletePlaylist(w http.ResponseWriter, r *http.Requ
func (c *PlaylistsController) update(ctx context.Context, playlistId string, name *string, comment *string, public *bool, idsToAdd []string, idxToRemove []int) error {
return c.ds.WithTx(func(tx model.DataStore) error {
pls, err := tx.Playlist(ctx).Get(playlistId)
pls, err := tx.Playlist(ctx).GetWithTracks(playlistId)
if err != nil {
return err
}
owner := getUser(ctx)
if owner != pls.Owner {
return model.ErrNotAuthorized
}
if name != nil {
pls.Name = *name
}