navidrome/persistence/album_repository.go

217 lines
5.5 KiB
Go
Raw Normal View History

package persistence
2020-01-13 00:55:55 +01:00
import (
2020-01-16 22:53:48 +01:00
"fmt"
"strings"
2020-01-13 00:55:55 +01:00
"time"
"github.com/astaxie/beego/orm"
2020-01-16 22:53:48 +01:00
"github.com/cloudsonic/sonic-server/log"
2020-01-15 04:22:34 +01:00
"github.com/cloudsonic/sonic-server/model"
2020-01-13 00:55:55 +01:00
)
2020-01-18 03:03:54 +01:00
type album struct {
2020-01-13 00:55:55 +01:00
ID string `orm:"pk;column(id)"`
Name string `orm:"index"`
ArtistID string `orm:"column(artist_id);index"`
CoverArtPath string ``
CoverArtId string ``
Artist string `orm:"index"`
AlbumArtist string ``
Year int `orm:"index"`
Compilation bool ``
Starred bool `orm:"index"`
PlayCount int `orm:"index"`
PlayDate time.Time `orm:"null;index"`
SongCount int ``
Duration int ``
Rating int `orm:"index"`
2020-01-15 23:49:09 +01:00
Genre string `orm:"index"`
StarredAt time.Time `orm:"index;null"`
2020-01-13 00:55:55 +01:00
CreatedAt time.Time `orm:"null"`
UpdatedAt time.Time `orm:"null"`
}
type albumRepository struct {
searchableRepository
2020-01-13 00:55:55 +01:00
}
2020-01-15 04:22:34 +01:00
func NewAlbumRepository() model.AlbumRepository {
2020-01-13 00:55:55 +01:00
r := &albumRepository{}
2020-01-13 06:04:11 +01:00
r.tableName = "album"
2020-01-13 00:55:55 +01:00
return r
}
2020-01-15 04:22:34 +01:00
func (r *albumRepository) Put(a *model.Album) error {
2020-01-18 03:03:54 +01:00
ta := album(*a)
return withTx(func(o orm.Ormer) error {
return r.put(o, a.ID, a.Name, &ta)
})
2020-01-13 00:55:55 +01:00
}
2020-01-15 04:22:34 +01:00
func (r *albumRepository) Get(id string) (*model.Album, error) {
2020-01-18 03:03:54 +01:00
ta := album{ID: id}
2020-01-13 00:55:55 +01:00
err := Db().Read(&ta)
if err == orm.ErrNoRows {
2020-01-15 04:22:34 +01:00
return nil, model.ErrNotFound
2020-01-13 00:55:55 +01:00
}
if err != nil {
return nil, err
}
2020-01-15 04:22:34 +01:00
a := model.Album(ta)
2020-01-13 00:55:55 +01:00
return &a, err
}
2020-01-15 04:22:34 +01:00
func (r *albumRepository) FindByArtist(artistId string) (model.Albums, error) {
2020-01-18 03:03:54 +01:00
var albums []album
2020-01-13 00:55:55 +01:00
_, err := r.newQuery(Db()).Filter("artist_id", artistId).OrderBy("year", "name").All(&albums)
if err != nil {
return nil, err
}
2020-01-13 21:41:14 +01:00
return r.toAlbums(albums), nil
2020-01-13 00:55:55 +01:00
}
2020-01-15 04:22:34 +01:00
func (r *albumRepository) GetAll(options ...model.QueryOptions) (model.Albums, error) {
2020-01-18 03:03:54 +01:00
var all []album
2020-01-13 00:55:55 +01:00
_, err := r.newQuery(Db(), options...).All(&all)
if err != nil {
return nil, err
}
2020-01-13 21:41:14 +01:00
return r.toAlbums(all), nil
2020-01-13 00:55:55 +01:00
}
2020-01-18 03:03:54 +01:00
func (r *albumRepository) toAlbums(all []album) model.Albums {
2020-01-15 04:22:34 +01:00
result := make(model.Albums, len(all))
2020-01-13 00:55:55 +01:00
for i, a := range all {
2020-01-15 04:22:34 +01:00
result[i] = model.Album(a)
2020-01-13 00:55:55 +01:00
}
2020-01-13 21:41:14 +01:00
return result
2020-01-13 00:55:55 +01:00
}
2020-01-16 22:53:48 +01:00
func (r *albumRepository) Refresh(ids ...string) error {
type refreshAlbum struct {
2020-01-18 03:03:54 +01:00
album
2020-01-16 22:53:48 +01:00
CurrentId string
HasCoverArt bool
}
var albums []refreshAlbum
o := Db()
sql := fmt.Sprintf(`
select album_id as id, album as name, f.artist, f.album_artist, f.artist_id, f.compilation, f.genre,
max(f.year) as year, sum(f.play_count) as play_count, max(f.play_date) as play_date, sum(f.duration) as duration,
max(f.updated_at) as updated_at, min(f.created_at) as created_at, count(*) as song_count,
a.id as current_id, f.id as cover_art_id, f.path as cover_art_path, f.has_cover_art
from media_file f left outer join album a on f.album_id = a.id
where f.album_id in ('%s')
group by album_id order by f.id`, strings.Join(ids, "','"))
_, err := o.Raw(sql).QueryRows(&albums)
if err != nil {
return err
}
2020-01-18 03:03:54 +01:00
var toInsert []album
var toUpdate []album
2020-01-16 22:53:48 +01:00
for _, al := range albums {
if !al.HasCoverArt {
al.CoverArtId = ""
}
if al.Compilation {
al.AlbumArtist = "Various Artists"
}
if al.AlbumArtist == "" {
al.AlbumArtist = al.Artist
}
2020-01-16 22:53:48 +01:00
if al.CurrentId != "" {
2020-01-18 03:03:54 +01:00
toUpdate = append(toUpdate, al.album)
2020-01-16 22:53:48 +01:00
} else {
2020-01-18 03:03:54 +01:00
toInsert = append(toInsert, al.album)
2020-01-16 22:53:48 +01:00
}
err := r.addToIndex(o, r.tableName, al.ID, al.Name)
if err != nil {
return err
}
2020-01-16 22:53:48 +01:00
}
if len(toInsert) > 0 {
n, err := o.InsertMulti(10, toInsert)
2020-01-16 22:53:48 +01:00
if err != nil {
return err
}
log.Debug("Inserted new albums", "num", n)
}
if len(toUpdate) > 0 {
for _, al := range toUpdate {
// Don't update Starred/Rating
_, err := o.Update(&al, "name", "artist_id", "cover_art_path", "cover_art_id", "artist", "album_artist",
"year", "compilation", "play_count", "play_date", "song_count", "duration", "updated_at", "created_at")
2020-01-16 22:53:48 +01:00
if err != nil {
return err
}
}
log.Debug("Updated albums", "num", len(toUpdate))
}
return err
}
2020-01-15 04:22:34 +01:00
func (r *albumRepository) PurgeInactive(activeList model.Albums) error {
return withTx(func(o orm.Ormer) error {
_, err := r.purgeInactive(o, activeList, func(item interface{}) string {
2020-01-15 04:22:34 +01:00
return item.(model.Album).ID
})
return err
2020-01-13 00:55:55 +01:00
})
}
2020-01-18 05:28:11 +01:00
func (r *albumRepository) PurgeEmpty() error {
o := Db()
_, err := o.Raw("delete from album where id not in (select distinct(album_id) from media_file)").Exec()
return err
}
2020-01-15 04:22:34 +01:00
func (r *albumRepository) GetStarred(options ...model.QueryOptions) (model.Albums, error) {
2020-01-18 03:03:54 +01:00
var starred []album
2020-01-13 00:55:55 +01:00
_, err := r.newQuery(Db(), options...).Filter("starred", true).All(&starred)
if err != nil {
return nil, err
}
2020-01-13 21:41:14 +01:00
return r.toAlbums(starred), nil
}
func (r *albumRepository) SetStar(starred bool, ids ...string) error {
if len(ids) == 0 {
return model.ErrNotFound
}
var starredAt time.Time
if starred {
starredAt = time.Now()
}
_, err := r.newQuery(Db()).Filter("id__in", ids).Update(orm.Params{
"starred": starred,
"starred_at": starredAt,
})
return err
}
func (r *albumRepository) MarkAsPlayed(id string, playDate time.Time) error {
_, err := r.newQuery(Db()).Filter("id", id).Update(orm.Params{
"play_count": orm.ColValue(orm.ColAdd, 1),
"play_date": playDate,
})
return err
}
2020-01-15 04:22:34 +01:00
func (r *albumRepository) Search(q string, offset int, size int) (model.Albums, error) {
2020-01-13 21:41:14 +01:00
if len(q) <= 2 {
return nil, nil
}
2020-01-18 03:03:54 +01:00
var results []album
err := r.doSearch(r.tableName, q, offset, size, &results, "rating desc", "starred desc", "play_count desc", "name")
2020-01-13 21:41:14 +01:00
if err != nil {
return nil, err
}
return r.toAlbums(results), nil
2020-01-13 00:55:55 +01:00
}
2020-01-15 04:22:34 +01:00
var _ model.AlbumRepository = (*albumRepository)(nil)
2020-01-18 03:03:54 +01:00
var _ = model.Album(album{})