quintodrome/persistence/album_repository.go

221 lines
5.7 KiB
Go
Raw Normal View History

package persistence
2020-01-12 14:55:55 -09:00
import (
2020-01-16 12:53:48 -09:00
"fmt"
"strings"
2020-01-12 14:55:55 -09:00
"time"
2020-01-21 19:01:43 -09:00
"github.com/Masterminds/squirrel"
2020-01-12 14:55:55 -09:00
"github.com/astaxie/beego/orm"
2020-01-16 12:53:48 -09:00
"github.com/cloudsonic/sonic-server/log"
2020-01-14 18:22:34 -09:00
"github.com/cloudsonic/sonic-server/model"
2020-01-12 14:55:55 -09:00
)
2020-01-17 17:03:54 -09:00
type album struct {
ID string `json:"id" orm:"pk;column(id)"`
Name string `json:"name" orm:"index"`
ArtistID string `json:"artistId" orm:"column(artist_id);index"`
CoverArtPath string `json:"-"`
CoverArtId string `json:"-"`
Artist string `json:"artist" orm:"index"`
AlbumArtist string `json:"albumArtist"`
Year int `json:"year" orm:"index"`
Compilation bool `json:"compilation"`
SongCount int `json:"songCount"`
Duration int `json:"duration"`
Genre string `json:"genre" orm:"index"`
CreatedAt time.Time `json:"createdAt" orm:"null"`
UpdatedAt time.Time `json:"updatedAt" orm:"null"`
2020-01-12 14:55:55 -09:00
}
type albumRepository struct {
searchableRepository
2020-01-12 14:55:55 -09:00
}
func NewAlbumRepository(o orm.Ormer) model.AlbumRepository {
2020-01-12 14:55:55 -09:00
r := &albumRepository{}
r.ormer = o
2020-01-12 20:04:11 -09:00
r.tableName = "album"
2020-01-12 14:55:55 -09:00
return r
}
2020-01-14 18:22:34 -09:00
func (r *albumRepository) Put(a *model.Album) error {
2020-01-17 17:03:54 -09:00
ta := album(*a)
return r.put(a.ID, a.Name, &ta)
2020-01-12 14:55:55 -09:00
}
2020-01-14 18:22:34 -09:00
func (r *albumRepository) Get(id string) (*model.Album, error) {
2020-01-17 17:03:54 -09:00
ta := album{ID: id}
err := r.ormer.Read(&ta)
2020-01-12 14:55:55 -09:00
if err == orm.ErrNoRows {
2020-01-14 18:22:34 -09:00
return nil, model.ErrNotFound
2020-01-12 14:55:55 -09:00
}
if err != nil {
return nil, err
}
2020-01-14 18:22:34 -09:00
a := model.Album(ta)
2020-01-12 14:55:55 -09:00
return &a, err
}
2020-01-14 18:22:34 -09:00
func (r *albumRepository) FindByArtist(artistId string) (model.Albums, error) {
2020-01-17 17:03:54 -09:00
var albums []album
_, err := r.newQuery().Filter("artist_id", artistId).OrderBy("year", "name").All(&albums)
2020-01-12 14:55:55 -09:00
if err != nil {
return nil, err
}
2020-01-13 11:41:14 -09:00
return r.toAlbums(albums), nil
2020-01-12 14:55:55 -09:00
}
2020-01-14 18:22:34 -09:00
func (r *albumRepository) GetAll(options ...model.QueryOptions) (model.Albums, error) {
2020-01-17 17:03:54 -09:00
var all []album
_, err := r.newQuery(options...).All(&all)
2020-01-12 14:55:55 -09:00
if err != nil {
return nil, err
}
2020-01-13 11:41:14 -09:00
return r.toAlbums(all), nil
2020-01-12 14:55:55 -09:00
}
2020-01-21 21:00:00 -09:00
func (r *albumRepository) GetMap(ids []string) (map[string]model.Album, error) {
var all []album
if len(ids) == 0 {
return nil, nil
}
2020-01-21 21:00:00 -09:00
_, err := r.newQuery().Filter("id__in", ids).All(&all)
if err != nil {
return nil, err
}
res := make(map[string]model.Album)
for _, a := range all {
res[a.ID] = model.Album(a)
}
return res, nil
}
// TODO Keep order when paginating
func (r *albumRepository) GetRandom(options ...model.QueryOptions) (model.Albums, error) {
sq := r.newRawQuery(options...)
switch r.ormer.Driver().Type() {
case orm.DRMySQL:
sq = sq.OrderBy("RAND()")
default:
sq = sq.OrderBy("RANDOM()")
}
sql, args, err := sq.ToSql()
if err != nil {
return nil, err
}
var results []album
_, err = r.ormer.Raw(sql, args...).QueryRows(&results)
return r.toAlbums(results), err
}
2020-01-17 17:03:54 -09:00
func (r *albumRepository) toAlbums(all []album) model.Albums {
2020-01-14 18:22:34 -09:00
result := make(model.Albums, len(all))
2020-01-12 14:55:55 -09:00
for i, a := range all {
2020-01-14 18:22:34 -09:00
result[i] = model.Album(a)
2020-01-12 14:55:55 -09:00
}
2020-01-13 11:41:14 -09:00
return result
2020-01-12 14:55:55 -09:00
}
2020-01-16 12:53:48 -09:00
func (r *albumRepository) Refresh(ids ...string) error {
type refreshAlbum struct {
2020-01-17 17:03:54 -09:00
album
2020-01-16 12:53:48 -09:00
CurrentId string
HasCoverArt bool
}
var albums []refreshAlbum
o := r.ormer
2020-01-16 12:53:48 -09:00
sql := fmt.Sprintf(`
select album_id as id, album as name, f.artist, f.album_artist, f.artist_id, f.compilation, f.genre,
2020-01-21 19:01:43 -09:00
max(f.year) as year, 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
2020-01-16 12:53:48 -09:00
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-17 17:03:54 -09:00
var toInsert []album
var toUpdate []album
2020-01-16 12:53:48 -09: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 12:53:48 -09:00
if al.CurrentId != "" {
2020-01-17 17:03:54 -09:00
toUpdate = append(toUpdate, al.album)
2020-01-16 12:53:48 -09:00
} else {
2020-01-17 17:03:54 -09:00
toInsert = append(toInsert, al.album)
2020-01-16 12:53:48 -09:00
}
err := r.addToIndex(r.tableName, al.ID, al.Name)
if err != nil {
return err
}
2020-01-16 12:53:48 -09:00
}
if len(toInsert) > 0 {
n, err := o.InsertMulti(10, toInsert)
2020-01-16 12:53:48 -09:00
if err != nil {
return err
}
log.Debug("Inserted new albums", "num", n)
}
if len(toUpdate) > 0 {
for _, al := range toUpdate {
_, err := o.Update(&al, "name", "artist_id", "cover_art_path", "cover_art_id", "artist", "album_artist",
2020-01-21 19:01:43 -09:00
"year", "compilation", "song_count", "duration", "updated_at", "created_at")
2020-01-16 12:53:48 -09:00
if err != nil {
return err
}
}
log.Debug("Updated albums", "num", len(toUpdate))
}
return err
}
2020-01-17 19:28:11 -09:00
func (r *albumRepository) PurgeEmpty() error {
_, err := r.ormer.Raw("delete from album where id not in (select distinct(album_id) from media_file)").Exec()
2020-01-17 19:28:11 -09:00
return err
}
2020-01-21 19:01:43 -09:00
func (r *albumRepository) GetStarred(userId string, options ...model.QueryOptions) (model.Albums, error) {
2020-01-17 17:03:54 -09:00
var starred []album
2020-01-21 19:01:43 -09:00
sq := r.newRawQuery(options...).Join("annotation").Where("annotation.item_id = " + r.tableName + ".id")
2020-01-22 11:19:37 -09:00
sq = sq.Where(squirrel.And{
squirrel.Eq{"annotation.user_id": userId},
squirrel.Eq{"annotation.starred": true},
})
2020-01-21 19:01:43 -09:00
sql, args, err := sq.ToSql()
2020-01-12 14:55:55 -09:00
if err != nil {
return nil, err
}
2020-01-21 19:01:43 -09:00
_, err = r.ormer.Raw(sql, args...).QueryRows(&starred)
if err != nil {
return nil, err
}
2020-01-21 19:01:43 -09:00
return r.toAlbums(starred), nil
}
2020-01-14 18:22:34 -09:00
func (r *albumRepository) Search(q string, offset int, size int) (model.Albums, error) {
2020-01-13 11:41:14 -09:00
if len(q) <= 2 {
return nil, nil
}
2020-01-17 17:03:54 -09:00
var results []album
2020-01-21 19:01:43 -09:00
err := r.doSearch(r.tableName, q, offset, size, &results, "name")
2020-01-13 11:41:14 -09:00
if err != nil {
return nil, err
}
return r.toAlbums(results), nil
2020-01-12 14:55:55 -09:00
}
2020-01-14 18:22:34 -09:00
var _ model.AlbumRepository = (*albumRepository)(nil)
2020-01-17 17:03:54 -09:00
var _ = model.Album(album{})