Return playlist duration in getPlayList

This commit is contained in:
Deluan 2020-01-21 18:10:29 -05:00
parent dce2a1399d
commit d4adc784cc
3 changed files with 26 additions and 14 deletions

View file

@ -119,15 +119,16 @@ type PlaylistInfo struct {
}
func (p *playlists) Get(id string) (*PlaylistInfo, error) {
pl, err := p.ds.Playlist().Get(id)
pl, err := p.ds.Playlist().GetWithTracks(id)
if err != nil {
return nil, err
}
// TODO Use model.Playlist when got rid of Entries
pinfo := &PlaylistInfo{
Id: pl.ID,
Name: pl.Name,
SongCount: len(pl.Tracks), // TODO Use model.Playlist
SongCount: len(pl.Tracks),
Duration: pl.Duration,
Public: pl.Public,
Owner: pl.Owner,
@ -135,13 +136,8 @@ func (p *playlists) Get(id string) (*PlaylistInfo, error) {
}
pinfo.Entries = make(Entries, len(pl.Tracks))
// TODO Optimize: Get all tracks at once
for i, mf := range pl.Tracks {
mf, err := p.ds.MediaFile().Get(mf.ID)
if err != nil {
return nil, err
}
pinfo.Entries[i] = FromMediaFile(mf)
pinfo.Entries[i] = FromMediaFile(&mf)
}
return pinfo, nil

View file

@ -4,7 +4,6 @@ type Playlist struct {
ID string
Name string
Comment string
FullPath string
Duration int
Owner string
Public bool
@ -16,6 +15,7 @@ type PlaylistRepository interface {
Exists(id string) (bool, error)
Put(pls *Playlist) error
Get(id string) (*Playlist, error)
GetWithTracks(id string) (*Playlist, error)
GetAll(options ...QueryOptions) (Playlists, error)
Delete(id string) error
}

View file

@ -12,7 +12,6 @@ type playlist struct {
ID string `orm:"pk;column(id)"`
Name string `orm:"index"`
Comment string
FullPath string
Duration int
Owner string
Public bool
@ -52,8 +51,27 @@ func (r *playlistRepository) Get(id string) (*model.Playlist, error) {
if err != nil {
return nil, err
}
a := r.toModel(tp)
return &a, err
pls := r.toModel(tp)
return &pls, err
}
func (r *playlistRepository) GetWithTracks(id string) (*model.Playlist, error) {
pls, err := r.Get(id)
if err != nil {
return nil, err
}
qs := r.ormer.QueryTable(&mediaFile{})
pls.Duration = 0
var newTracks model.MediaFiles
for _, t := range pls.Tracks {
mf := &mediaFile{}
if err := qs.Filter("id", t.ID).One(mf); err == nil {
pls.Duration += mf.Duration
newTracks = append(newTracks, model.MediaFile(*mf))
}
}
pls.Tracks = newTracks
return pls, err
}
func (r *playlistRepository) GetAll(options ...model.QueryOptions) (model.Playlists, error) {
@ -78,7 +96,6 @@ func (r *playlistRepository) toModel(p *playlist) model.Playlist {
ID: p.ID,
Name: p.Name,
Comment: p.Comment,
FullPath: p.FullPath,
Duration: p.Duration,
Owner: p.Owner,
Public: p.Public,
@ -97,7 +114,6 @@ func (r *playlistRepository) fromModel(p *model.Playlist) playlist {
ID: p.ID,
Name: p.Name,
Comment: p.Comment,
FullPath: p.FullPath,
Duration: p.Duration,
Owner: p.Owner,
Public: p.Public,