navidrome/persistence/playlist_repository.go

101 lines
2.1 KiB
Go
Raw Normal View History

package persistence
2020-01-13 03:59:06 +01:00
import (
"strings"
"github.com/astaxie/beego/orm"
2020-01-15 04:22:34 +01:00
"github.com/cloudsonic/sonic-server/model"
2020-01-13 03:59:06 +01:00
)
2020-01-18 03:03:54 +01:00
type playlist struct {
2020-01-13 03:59:06 +01:00
ID string `orm:"pk;column(id)"`
Name string `orm:"index"`
Comment string
FullPath string
Duration int
Owner string
Public bool
Tracks string
}
type playlistRepository struct {
sqlRepository
}
func NewPlaylistRepository(o orm.Ormer) model.PlaylistRepository {
2020-01-13 03:59:06 +01:00
r := &playlistRepository{}
r.ormer = o
2020-01-13 06:04:11 +01:00
r.tableName = "playlist"
2020-01-13 03:59:06 +01:00
return r
}
2020-01-15 04:22:34 +01:00
func (r *playlistRepository) Put(p *model.Playlist) error {
2020-01-13 03:59:06 +01:00
tp := r.fromDomain(p)
return r.put(p.ID, &tp)
2020-01-13 03:59:06 +01:00
}
2020-01-15 04:22:34 +01:00
func (r *playlistRepository) Get(id string) (*model.Playlist, error) {
2020-01-18 03:03:54 +01:00
tp := &playlist{ID: id}
err := r.ormer.Read(tp)
2020-01-13 03:59:06 +01:00
if err == orm.ErrNoRows {
2020-01-15 04:22:34 +01:00
return nil, model.ErrNotFound
2020-01-13 03:59:06 +01:00
}
if err != nil {
return nil, err
}
a := r.toDomain(tp)
return &a, err
}
2020-01-15 04:22:34 +01:00
func (r *playlistRepository) GetAll(options ...model.QueryOptions) (model.Playlists, error) {
2020-01-18 03:03:54 +01:00
var all []playlist
_, err := r.newQuery(options...).All(&all)
2020-01-13 03:59:06 +01:00
if err != nil {
return nil, err
}
return r.toPlaylists(all)
}
2020-01-18 03:03:54 +01:00
func (r *playlistRepository) toPlaylists(all []playlist) (model.Playlists, error) {
2020-01-15 04:22:34 +01:00
result := make(model.Playlists, len(all))
2020-01-13 03:59:06 +01:00
for i, p := range all {
result[i] = r.toDomain(&p)
}
return result, nil
}
2020-01-15 04:22:34 +01:00
func (r *playlistRepository) PurgeInactive(activeList model.Playlists) ([]string, error) {
_, err := r.purgeInactive(activeList, func(item interface{}) string {
return item.(model.Playlist).ID
2020-01-13 03:59:06 +01:00
})
return nil, err
2020-01-13 03:59:06 +01:00
}
2020-01-18 03:03:54 +01:00
func (r *playlistRepository) toDomain(p *playlist) model.Playlist {
2020-01-15 04:22:34 +01:00
return model.Playlist{
2020-01-13 03:59:06 +01:00
ID: p.ID,
Name: p.Name,
Comment: p.Comment,
FullPath: p.FullPath,
Duration: p.Duration,
Owner: p.Owner,
Public: p.Public,
Tracks: strings.Split(p.Tracks, ","),
}
}
2020-01-18 03:03:54 +01:00
func (r *playlistRepository) fromDomain(p *model.Playlist) playlist {
return playlist{
2020-01-13 03:59:06 +01:00
ID: p.ID,
Name: p.Name,
Comment: p.Comment,
FullPath: p.FullPath,
Duration: p.Duration,
Owner: p.Owner,
Public: p.Public,
Tracks: strings.Join(p.Tracks, ","),
}
}
2020-01-15 04:22:34 +01:00
var _ model.PlaylistRepository = (*playlistRepository)(nil)