merge create and update playlist
This commit is contained in:
@@ -121,6 +121,7 @@ type Playlist struct {
|
|||||||
|
|
||||||
type PlaylistItem struct {
|
type PlaylistItem struct {
|
||||||
ID int `gorm:"primary_key"`
|
ID int `gorm:"primary_key"`
|
||||||
|
CreatedAt time.Time
|
||||||
Playlist Playlist
|
Playlist Playlist
|
||||||
PlaylistID int `sql:"default: null; type:int REFERENCES playlists(id) ON DELETE CASCADE"`
|
PlaylistID int `sql:"default: null; type:int REFERENCES playlists(id) ON DELETE CASCADE"`
|
||||||
Track Track
|
Track Track
|
||||||
|
|||||||
@@ -156,6 +156,7 @@ func (c *Controller) ServeGetPlaylist(r *http.Request) *spec.Response {
|
|||||||
`).
|
`).
|
||||||
Where("playlist_items.playlist_id = ?", playlistID).
|
Where("playlist_items.playlist_id = ?", playlistID).
|
||||||
Group("tracks.id").
|
Group("tracks.id").
|
||||||
|
Order("playlist_items.created_at").
|
||||||
Preload("Album").
|
Preload("Album").
|
||||||
Find(&tracks)
|
Find(&tracks)
|
||||||
user := r.Context().Value(key.User).(*model.User)
|
user := r.Context().Value(key.User).(*model.User)
|
||||||
@@ -169,22 +170,45 @@ func (c *Controller) ServeGetPlaylist(r *http.Request) *spec.Response {
|
|||||||
return sub
|
return sub
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) ServeCreatePlaylist(r *http.Request) *spec.Response {
|
func (c *Controller) ServeUpdatePlaylist(r *http.Request) *spec.Response {
|
||||||
user := r.Context().Value(key.User).(*model.User)
|
playlistID, _ := parsing.GetFirstIntParamOf(r, "id", "playlistId")
|
||||||
|
//
|
||||||
|
// begin updating meta
|
||||||
playlist := &model.Playlist{}
|
playlist := &model.Playlist{}
|
||||||
c.DB.
|
c.DB.
|
||||||
Select("id").
|
Where("id = ?", playlistID).
|
||||||
Where("id = ?", parsing.GetIntParamOr(r, "id", 0)).
|
|
||||||
First(playlist)
|
First(playlist)
|
||||||
|
user := r.Context().Value(key.User).(*model.User)
|
||||||
playlist.UserID = user.ID
|
playlist.UserID = user.ID
|
||||||
playlist.Name = parsing.GetStrParam(r, "name")
|
if name := parsing.GetStrParam(r, "name"); name != "" {
|
||||||
c.DB.Save(playlist)
|
playlist.Name = name
|
||||||
sub := spec.NewResponse()
|
|
||||||
tracks, ok := r.URL.Query()["songId"]
|
|
||||||
if !ok {
|
|
||||||
return sub
|
|
||||||
}
|
}
|
||||||
for _, trackIDStr := range tracks {
|
if comment := parsing.GetStrParam(r, "comment"); comment != "" {
|
||||||
|
playlist.Comment = comment
|
||||||
|
}
|
||||||
|
c.DB.Save(playlist)
|
||||||
|
//
|
||||||
|
// begin delete tracks
|
||||||
|
if indexes, ok := r.URL.Query()["songIndexToRemove"]; ok {
|
||||||
|
trackIDs := []int{}
|
||||||
|
c.DB.
|
||||||
|
Order("created_at").
|
||||||
|
Model(&model.PlaylistItem{}).
|
||||||
|
Where("playlist_id = ?", playlistID).
|
||||||
|
Pluck("track_id", &trackIDs)
|
||||||
|
for _, indexStr := range indexes {
|
||||||
|
i, err := strconv.Atoi(indexStr)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
c.DB.Delete(&model.PlaylistItem{},
|
||||||
|
"track_id = ?", trackIDs[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//
|
||||||
|
// begin add tracks
|
||||||
|
if toAdd := parsing.GetFirstParamOf(r, "songId", "songIdToAdd"); toAdd != nil {
|
||||||
|
for _, trackIDStr := range toAdd {
|
||||||
trackID, err := strconv.Atoi(trackIDStr)
|
trackID, err := strconv.Atoi(trackIDStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
@@ -194,13 +218,8 @@ func (c *Controller) ServeCreatePlaylist(r *http.Request) *spec.Response {
|
|||||||
TrackID: trackID,
|
TrackID: trackID,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return sub
|
}
|
||||||
}
|
return spec.NewResponse()
|
||||||
|
|
||||||
func (c *Controller) ServeUpdatePlaylist(r *http.Request) *spec.Response {
|
|
||||||
// user := r.Context().Value(key.User).(*model.User)
|
|
||||||
sub := spec.NewResponse()
|
|
||||||
return sub
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) ServeDeletePlaylist(r *http.Request) *spec.Response {
|
func (c *Controller) ServeDeletePlaylist(r *http.Request) *spec.Response {
|
||||||
|
|||||||
@@ -7,5 +7,7 @@ func NewPlaylist(p *model.Playlist) *Playlist {
|
|||||||
ID: p.ID,
|
ID: p.ID,
|
||||||
Name: p.Name,
|
Name: p.Name,
|
||||||
Comment: p.Comment,
|
Comment: p.Comment,
|
||||||
|
Duration: "1",
|
||||||
|
Public: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -210,5 +210,7 @@ type Playlist struct {
|
|||||||
Owner string `xml:"owner,attr" json:"owner,omitempty"`
|
Owner string `xml:"owner,attr" json:"owner,omitempty"`
|
||||||
SongCount string `xml:"songCount,attr" json:"songCount,omitempty"`
|
SongCount string `xml:"songCount,attr" json:"songCount,omitempty"`
|
||||||
Created string `xml:"created,attr" json:"created,omitempty"`
|
Created string `xml:"created,attr" json:"created,omitempty"`
|
||||||
|
Duration string `xml:"duration,attr" json:"duration,omitempty"`
|
||||||
|
Public bool `xml:"public,attr" json:"public,omitempty"`
|
||||||
List []*TrackChild `xml:"entry" json:"entry,omitempty"`
|
List []*TrackChild `xml:"entry" json:"entry,omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,3 +37,21 @@ func GetIntParamOr(r *http.Request, key string, or int) int {
|
|||||||
}
|
}
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetFirstParamOf(r *http.Request, keys ...string) []string {
|
||||||
|
for _, key := range keys {
|
||||||
|
if val, ok := r.URL.Query()[key]; ok {
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetFirstIntParamOf(r *http.Request, keys ...string) (int, bool) {
|
||||||
|
for _, key := range keys {
|
||||||
|
if v, err := GetIntParam(r, key); err == nil {
|
||||||
|
return v, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
|||||||
@@ -128,7 +128,7 @@ func (s *Server) SetupSubsonic() error {
|
|||||||
rout.Handle("/getUser{_:(?:\\.view)?}", ctrl.H(ctrl.ServeGetUser))
|
rout.Handle("/getUser{_:(?:\\.view)?}", ctrl.H(ctrl.ServeGetUser))
|
||||||
rout.Handle("/getPlaylists{_:(?:\\.view)?}", ctrl.H(ctrl.ServeGetPlaylists))
|
rout.Handle("/getPlaylists{_:(?:\\.view)?}", ctrl.H(ctrl.ServeGetPlaylists))
|
||||||
rout.Handle("/getPlaylist{_:(?:\\.view)?}", ctrl.H(ctrl.ServeGetPlaylist))
|
rout.Handle("/getPlaylist{_:(?:\\.view)?}", ctrl.H(ctrl.ServeGetPlaylist))
|
||||||
rout.Handle("/createPlaylist{_:(?:\\.view)?}", ctrl.H(ctrl.ServeCreatePlaylist))
|
rout.Handle("/createPlaylist{_:(?:\\.view)?}", ctrl.H(ctrl.ServeUpdatePlaylist))
|
||||||
rout.Handle("/updatePlaylist{_:(?:\\.view)?}", ctrl.H(ctrl.ServeUpdatePlaylist))
|
rout.Handle("/updatePlaylist{_:(?:\\.view)?}", ctrl.H(ctrl.ServeUpdatePlaylist))
|
||||||
rout.Handle("/deletePlaylist{_:(?:\\.view)?}", ctrl.H(ctrl.ServeDeletePlaylist))
|
rout.Handle("/deletePlaylist{_:(?:\\.view)?}", ctrl.H(ctrl.ServeDeletePlaylist))
|
||||||
//
|
//
|
||||||
|
|||||||
Reference in New Issue
Block a user