diff --git a/engine/playlists.go b/engine/playlists.go index 88c749d26..0b6a905c6 100644 --- a/engine/playlists.go +++ b/engine/playlists.go @@ -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 diff --git a/model/playlist.go b/model/playlist.go index 36f8a8021..bec2d89e9 100644 --- a/model/playlist.go +++ b/model/playlist.go @@ -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 } diff --git a/persistence/playlist_repository.go b/persistence/playlist_repository.go index 7f2d8d6ae..480600a8f 100644 --- a/persistence/playlist_repository.go +++ b/persistence/playlist_repository.go @@ -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,