navidrome/api/playlists.go

120 lines
3.2 KiB
Go
Raw Normal View History

package api
import (
2016-03-24 18:28:20 +01:00
"fmt"
"github.com/astaxie/beego"
2017-04-01 15:47:14 +02:00
"github.com/cloudsonic/sonic-server/api/responses"
"github.com/cloudsonic/sonic-server/domain"
"github.com/cloudsonic/sonic-server/engine"
"github.com/cloudsonic/sonic-server/utils"
)
type PlaylistsController struct {
BaseAPIController
pls engine.Playlists
}
func (c *PlaylistsController) Prepare() {
2016-03-11 21:16:17 +01:00
utils.ResolveDependencies(&c.pls)
}
2016-03-24 19:44:21 +01:00
func (c *PlaylistsController) GetPlaylists() {
allPls, err := c.pls.GetAll()
if err != nil {
beego.Error(err)
2016-03-23 17:35:10 +01:00
c.SendError(responses.ErrorGeneric, "Internal error")
}
playlists := make([]responses.Playlist, len(allPls))
2016-03-21 17:26:55 +01:00
for i, p := range allPls {
playlists[i].Id = p.Id
playlists[i].Name = p.Name
2016-03-24 18:28:20 +01:00
playlists[i].Comment = p.Comment
2016-03-21 17:26:55 +01:00
playlists[i].SongCount = len(p.Tracks)
playlists[i].Duration = p.Duration
playlists[i].Owner = p.Owner
playlists[i].Public = p.Public
}
response := c.NewEmpty()
response.Playlists = &responses.Playlists{Playlist: playlists}
c.SendResponse(response)
}
2016-03-10 00:28:11 +01:00
2016-03-24 19:44:21 +01:00
func (c *PlaylistsController) GetPlaylist() {
2016-03-10 00:28:11 +01:00
id := c.RequiredParamString("id", "id parameter required")
pinfo, err := c.pls.Get(id)
switch {
case err == domain.ErrNotFound:
2016-03-10 00:28:11 +01:00
beego.Error(err, "Id:", id)
2016-03-23 17:35:10 +01:00
c.SendError(responses.ErrorDataNotFound, "Directory not found")
2016-03-10 00:28:11 +01:00
case err != nil:
beego.Error(err)
2016-03-23 17:35:10 +01:00
c.SendError(responses.ErrorGeneric, "Internal Error")
2016-03-10 00:28:11 +01:00
}
response := c.NewEmpty()
response.Playlist = c.buildPlaylist(pinfo)
c.SendResponse(response)
}
2016-03-24 19:44:21 +01:00
func (c *PlaylistsController) CreatePlaylist() {
2016-03-24 17:06:39 +01:00
songIds := c.RequiredParamStrings("songId", "Required parameter songId is missing")
name := c.RequiredParamString("name", "Required parameter name is missing")
err := c.pls.Create(name, songIds)
if err != nil {
beego.Error(err)
c.SendError(responses.ErrorGeneric, "Internal Error")
}
c.SendEmptyResponse()
}
2016-03-24 19:44:21 +01:00
func (c *PlaylistsController) DeletePlaylist() {
2016-03-24 17:17:35 +01:00
id := c.RequiredParamString("id", "Required parameter id is missing")
err := c.pls.Delete(id)
if err != nil {
beego.Error(err)
c.SendError(responses.ErrorGeneric, "Internal Error")
}
c.SendEmptyResponse()
}
2016-03-24 19:44:21 +01:00
func (c *PlaylistsController) UpdatePlaylist() {
2016-03-24 18:28:20 +01:00
playlistId := c.RequiredParamString("playlistId", "Required parameter playlistId is missing")
songsToAdd := c.ParamStrings("songIdToAdd")
songIndexesToRemove := c.ParamInts("songIndexToRemove")
var pname *string
if len(c.Input()["name"]) > 0 {
s := c.Input()["name"][0]
pname = &s
}
beego.Info(fmt.Sprintf("Updating playlist with id '%s'", playlistId))
if pname != nil {
2016-03-24 18:53:52 +01:00
beego.Debug(fmt.Sprintf("-- New Name: '%s'", *pname))
2016-03-24 18:28:20 +01:00
}
2016-03-24 18:53:52 +01:00
beego.Debug(fmt.Sprintf("-- Adding: '%v'", songsToAdd))
beego.Debug(fmt.Sprintf("-- Removing: '%v'", songIndexesToRemove))
2016-03-24 18:28:20 +01:00
err := c.pls.Update(playlistId, pname, songsToAdd, songIndexesToRemove)
if err != nil {
beego.Error(err)
c.SendError(responses.ErrorGeneric, "Internal Error")
}
c.SendEmptyResponse()
}
2016-03-10 00:28:11 +01:00
func (c *PlaylistsController) buildPlaylist(d *engine.PlaylistInfo) *responses.PlaylistWithSongs {
pls := &responses.PlaylistWithSongs{}
pls.Id = d.Id
pls.Name = d.Name
2016-03-21 17:26:55 +01:00
pls.SongCount = d.SongCount
pls.Owner = d.Owner
pls.Duration = d.Duration
pls.Public = d.Public
2016-03-10 00:28:11 +01:00
pls.Entry = c.ToChildren(d.Entries)
2016-03-10 00:28:11 +01:00
return pls
}