Add tracks to playlist

This commit is contained in:
Deluan
2020-05-15 20:47:15 -04:00
committed by Deluan Quintão
parent fd49ae319f
commit e81a9dd1b5
13 changed files with 358 additions and 132 deletions
+38
View File
@@ -0,0 +1,38 @@
package app
import (
"encoding/json"
"fmt"
"net/http"
"github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/utils"
)
type addTracksPayload struct {
Ids []string `json:"ids"`
}
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
}
err = tracksRepo.Add(payload.Ids)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Must return an object with an ID, to satisfy ReactAdmin `create` call
_, err = w.Write([]byte(fmt.Sprintf(`{"id":"%s"}`, playlistId)))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
}
}