quintodrome/persistence/album_repository.go

122 lines
3 KiB
Go
Raw Normal View History

package persistence
2020-01-12 14:55:55 -09:00
import (
"time"
"github.com/astaxie/beego/orm"
2020-01-14 18:22:34 -09:00
"github.com/cloudsonic/sonic-server/model"
2020-01-12 14:55:55 -09:00
)
type Album struct {
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"`
Genre string ``
StarredAt time.Time `orm:"null"`
CreatedAt time.Time `orm:"null"`
UpdatedAt time.Time `orm:"null"`
}
type albumRepository struct {
searchableRepository
2020-01-12 14:55:55 -09:00
}
2020-01-14 18:22:34 -09:00
func NewAlbumRepository() model.AlbumRepository {
2020-01-12 14:55:55 -09:00
r := &albumRepository{}
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-12 14:55:55 -09:00
ta := Album(*a)
return withTx(func(o orm.Ormer) error {
return r.put(o, 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-12 14:55:55 -09:00
ta := Album{ID: id}
err := Db().Read(&ta)
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-12 14:55:55 -09:00
var albums []Album
_, err := r.newQuery(Db()).Filter("artist_id", artistId).OrderBy("year", "name").All(&albums)
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-12 14:55:55 -09:00
var all []Album
_, err := r.newQuery(Db(), options...).All(&all)
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-14 18:22:34 -09:00
func (r *albumRepository) toAlbums(all []Album) model.Albums {
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
}
// TODO Remove []string from return
2020-01-14 18:22:34 -09: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-14 18:22:34 -09:00
return item.(model.Album).ID
})
return err
2020-01-12 14:55:55 -09:00
})
}
2020-01-14 18:22:34 -09:00
func (r *albumRepository) GetStarred(options ...model.QueryOptions) (model.Albums, error) {
2020-01-12 14:55:55 -09:00
var starred []Album
_, err := r.newQuery(Db(), options...).Filter("starred", true).All(&starred)
if err != nil {
return nil, err
}
2020-01-13 11:41:14 -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
}
var results []Album
err := r.doSearch(r.tableName, q, offset, size, &results, "rating desc", "starred desc", "play_count desc", "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)
var _ = model.Album(Album{})