navidrome/api/playlists.go

71 lines
1.7 KiB
Go
Raw Normal View History

package api
import (
"github.com/astaxie/beego"
"github.com/deluan/gosonic/api/responses"
"github.com/deluan/gosonic/domain"
"github.com/deluan/gosonic/engine"
"github.com/deluan/gosonic/utils"
)
type PlaylistsController struct {
BaseAPIController
pls engine.Playlists
}
func (c *PlaylistsController) Prepare() {
2016-03-11 21:16:17 +01:00
utils.ResolveDependencies(&c.pls)
}
func (c *PlaylistsController) GetAll() {
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
playlists[i].Comment = "Original: " + p.FullPath
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
func (c *PlaylistsController) Get() {
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)
}
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
}