quintodrome/engine/playlists.go

87 lines
1.8 KiB
Go
Raw Normal View History

package engine
import (
2016-03-24 08:06:39 -08:00
"fmt"
"github.com/astaxie/beego"
"github.com/deluan/gosonic/domain"
2016-03-24 08:06:39 -08:00
"github.com/deluan/gosonic/itunesbridge"
)
type Playlists interface {
GetAll() (domain.Playlists, error)
2016-03-09 14:28:11 -09:00
Get(id string) (*PlaylistInfo, error)
2016-03-24 08:06:39 -08:00
Create(name string, ids []string) error
2016-03-24 08:17:35 -08:00
Delete(id string) error
}
2016-03-24 08:06:39 -08:00
func NewPlaylists(itunes itunesbridge.ItunesControl, pr domain.PlaylistRepository, mr domain.MediaFileRepository) Playlists {
return &playlists{itunes, pr, mr}
}
2016-03-09 14:28:11 -09:00
type playlists struct {
2016-03-24 08:06:39 -08:00
itunes itunesbridge.ItunesControl
2016-03-09 14:28:11 -09:00
plsRepo domain.PlaylistRepository
mfileRepo domain.MediaFileRepository
}
2016-03-21 19:11:57 -08:00
func (p *playlists) GetAll() (domain.Playlists, error) {
return p.plsRepo.GetAll(domain.QueryOptions{})
}
2016-03-09 14:28:11 -09:00
type PlaylistInfo struct {
2016-03-21 08:26:55 -08:00
Id string
Name string
Entries Entries
SongCount int
Duration int
Public bool
Owner string
2016-03-09 14:28:11 -09:00
}
2016-03-24 08:06:39 -08:00
func (p *playlists) Create(name string, ids []string) error {
pid, err := p.itunes.CreatePlaylist(name, ids)
if err != nil {
return err
}
beego.Info(fmt.Sprintf("Created playlist '%s' with id '%s'", name, pid))
return nil
}
2016-03-24 08:17:35 -08:00
func (p *playlists) Delete(id string) error {
err := p.itunes.DeletePlaylist(id)
if err != nil {
return err
}
beego.Info(fmt.Sprintf("Deleted playlist with id '%s'", id))
return nil
}
2016-03-21 19:11:57 -08:00
func (p *playlists) Get(id string) (*PlaylistInfo, error) {
2016-03-09 14:28:11 -09:00
pl, err := p.plsRepo.Get(id)
if err != nil {
return nil, err
}
2016-03-21 08:26:55 -08:00
pinfo := &PlaylistInfo{
Id: pl.Id,
Name: pl.Name,
SongCount: len(pl.Tracks),
Duration: pl.Duration,
Public: pl.Public,
Owner: pl.Owner,
}
2016-03-14 07:42:33 -08:00
pinfo.Entries = make(Entries, len(pl.Tracks))
2016-03-09 14:28:11 -09:00
// TODO Optimize: Get all tracks at once
for i, mfId := range pl.Tracks {
mf, err := p.mfileRepo.Get(mfId)
if err != nil {
return nil, err
}
2016-03-11 05:10:40 -09:00
pinfo.Entries[i] = FromMediaFile(mf)
2016-03-09 14:28:11 -09:00
}
return pinfo, nil
}