mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-04 13:07:36 +03:00
170 lines
4.1 KiB
Go
170 lines
4.1 KiB
Go
package persistence
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
. "github.com/Masterminds/squirrel"
|
|
"github.com/astaxie/beego/orm"
|
|
"github.com/deluan/navidrome/log"
|
|
"github.com/deluan/navidrome/model"
|
|
"github.com/deluan/rest"
|
|
)
|
|
|
|
type playlistRepository struct {
|
|
sqlRepository
|
|
sqlRestful
|
|
}
|
|
|
|
func NewPlaylistRepository(ctx context.Context, o orm.Ormer) model.PlaylistRepository {
|
|
r := &playlistRepository{}
|
|
r.ctx = ctx
|
|
r.ormer = o
|
|
r.tableName = "playlist"
|
|
return r
|
|
}
|
|
|
|
func (r *playlistRepository) userFilter() Sqlizer {
|
|
user := loggedUser(r.ctx)
|
|
if user.IsAdmin {
|
|
return And{}
|
|
}
|
|
return Or{
|
|
Eq{"public": true},
|
|
Eq{"owner": user.UserName},
|
|
}
|
|
}
|
|
|
|
func (r *playlistRepository) CountAll(options ...model.QueryOptions) (int64, error) {
|
|
sql := Select().Where(r.userFilter())
|
|
return r.count(sql, options...)
|
|
}
|
|
|
|
func (r *playlistRepository) Exists(id string) (bool, error) {
|
|
return r.exists(Select().Where(And{Eq{"id": id}, r.userFilter()}))
|
|
}
|
|
|
|
func (r *playlistRepository) Delete(id string) error {
|
|
err := r.delete(And{Eq{"id": id}, r.userFilter()})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
del := Delete("playlist_tracks").Where(Eq{"playlist_id": id})
|
|
_, err = r.executeSQL(del)
|
|
return err
|
|
}
|
|
|
|
func (r *playlistRepository) Put(p *model.Playlist) error {
|
|
if p.ID == "" {
|
|
p.CreatedAt = time.Now()
|
|
} else {
|
|
ok, err := r.Exists(p.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !ok {
|
|
return model.ErrNotAuthorized
|
|
}
|
|
}
|
|
p.UpdatedAt = time.Now()
|
|
|
|
// Save tracks for later and set it to nil, to avoid trying to save it to the DB
|
|
tracks := p.Tracks
|
|
p.Tracks = nil
|
|
|
|
id, err := r.put(p.ID, p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
p.ID = id
|
|
err = r.updateTracks(id, tracks)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return r.loadTracks(p)
|
|
}
|
|
|
|
func (r *playlistRepository) Get(id string) (*model.Playlist, error) {
|
|
sel := r.newSelect().Columns("*").Where(And{Eq{"id": id}, r.userFilter()})
|
|
var pls model.Playlist
|
|
err := r.queryOne(sel, &pls)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = r.loadTracks(&pls)
|
|
return &pls, err
|
|
}
|
|
|
|
func (r *playlistRepository) GetAll(options ...model.QueryOptions) (model.Playlists, error) {
|
|
sel := r.newSelect(options...).Columns("*").Where(r.userFilter())
|
|
res := model.Playlists{}
|
|
err := r.queryAll(sel, &res)
|
|
return res, err
|
|
}
|
|
|
|
func (r *playlistRepository) updateTracks(id string, tracks model.MediaFiles) error {
|
|
ids := make([]string, len(tracks))
|
|
for i := range tracks {
|
|
ids[i] = tracks[i].ID
|
|
}
|
|
return r.Tracks(id).Update(ids)
|
|
}
|
|
|
|
func (r *playlistRepository) loadTracks(pls *model.Playlist) error {
|
|
tracksQuery := Select().From("playlist_tracks").
|
|
LeftJoin("annotation on ("+
|
|
"annotation.item_id = media_file_id"+
|
|
" AND annotation.item_type = 'media_file'"+
|
|
" AND annotation.user_id = '"+userId(r.ctx)+"')").
|
|
Columns("starred", "starred_at", "play_count", "play_date", "rating", "f.*").
|
|
Join("media_file f on f.id = media_file_id").
|
|
Where(Eq{"playlist_id": pls.ID}).OrderBy("playlist_tracks.id")
|
|
err := r.queryAll(tracksQuery, &pls.Tracks)
|
|
if err != nil {
|
|
log.Error("Error loading playlist tracks", "playlist", pls.Name, "id", pls.ID)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (r *playlistRepository) Count(options ...rest.QueryOptions) (int64, error) {
|
|
return r.CountAll(r.parseRestOptions(options...))
|
|
}
|
|
|
|
func (r *playlistRepository) Read(id string) (interface{}, error) {
|
|
return r.Get(id)
|
|
}
|
|
|
|
func (r *playlistRepository) ReadAll(options ...rest.QueryOptions) (interface{}, error) {
|
|
return r.GetAll(r.parseRestOptions(options...))
|
|
}
|
|
|
|
func (r *playlistRepository) EntityName() string {
|
|
return "playlist"
|
|
}
|
|
|
|
func (r *playlistRepository) NewInstance() interface{} {
|
|
return &model.Playlist{}
|
|
}
|
|
|
|
func (r *playlistRepository) Save(entity interface{}) (string, error) {
|
|
pls := entity.(*model.Playlist)
|
|
pls.Owner = loggedUser(r.ctx).UserName
|
|
err := r.Put(pls)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return pls.ID, err
|
|
}
|
|
|
|
func (r *playlistRepository) Update(entity interface{}, cols ...string) error {
|
|
pls := entity.(*model.Playlist)
|
|
err := r.Put(pls)
|
|
if err == model.ErrNotFound {
|
|
return rest.ErrNotFound
|
|
}
|
|
return err
|
|
}
|
|
|
|
var _ model.PlaylistRepository = (*playlistRepository)(nil)
|
|
var _ rest.Repository = (*playlistRepository)(nil)
|
|
var _ rest.Persistable = (*playlistRepository)(nil)
|