123 lines
2.8 KiB
Go
123 lines
2.8 KiB
Go
package storm
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/asdine/storm/q"
|
|
"github.com/cloudsonic/sonic-server/domain"
|
|
)
|
|
|
|
type _Album struct {
|
|
ID string ``
|
|
Name string `storm:"index"`
|
|
ArtistID string `storm:"index"`
|
|
CoverArtPath string ``
|
|
CoverArtId string ``
|
|
Artist string `storm:"index"`
|
|
AlbumArtist string ``
|
|
Year int `storm:"index"`
|
|
Compilation bool ``
|
|
Starred bool `storm:"index"`
|
|
PlayCount int `storm:"index"`
|
|
PlayDate time.Time `storm:"index"`
|
|
SongCount int ``
|
|
Duration int ``
|
|
Rating int `storm:"index"`
|
|
Genre string ``
|
|
StarredAt time.Time `storm:"index"`
|
|
CreatedAt time.Time `storm:"index"`
|
|
UpdatedAt time.Time ``
|
|
}
|
|
|
|
type albumRepository struct {
|
|
stormRepository
|
|
}
|
|
|
|
func NewAlbumRepository() domain.AlbumRepository {
|
|
r := &albumRepository{}
|
|
r.init(&_Album{})
|
|
return r
|
|
}
|
|
|
|
func (r *albumRepository) Put(a *domain.Album) error {
|
|
ta := _Album(*a)
|
|
return Db().Save(&ta)
|
|
}
|
|
|
|
func (r *albumRepository) Get(id string) (*domain.Album, error) {
|
|
ta := &_Album{}
|
|
err := r.getByID(id, ta)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
a := domain.Album(*ta)
|
|
return &a, err
|
|
}
|
|
|
|
func (r *albumRepository) FindByArtist(artistId string) (domain.Albums, error) {
|
|
var albums []_Album
|
|
err := r.execute(q.Eq("ArtistID", artistId), &albums)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return r.toDomainList(albums)
|
|
}
|
|
|
|
func (r *albumRepository) GetAll(options domain.QueryOptions) (domain.Albums, error) {
|
|
all, err := r.getAll(&options)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return r.toDomainList(all)
|
|
}
|
|
|
|
func (r *albumRepository) toDomainList(all []_Album) (domain.Albums, error) {
|
|
result := make(domain.Albums, len(all))
|
|
for i, a := range all {
|
|
result[i] = domain.Album(a)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (r *albumRepository) GetAllIds() ([]string, error) {
|
|
all, err := r.getAll(&domain.QueryOptions{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result := make([]string, len(all))
|
|
for i, a := range all {
|
|
result[i] = domain.Album(a).ID
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (r *albumRepository) getAll(options *domain.QueryOptions) (all []_Album, err error) {
|
|
if options.SortBy != "" {
|
|
err = Db().AllByIndex(options.SortBy, &all, stormOptions(options))
|
|
} else {
|
|
err = Db().All(&all, stormOptions(options))
|
|
}
|
|
return
|
|
}
|
|
|
|
func (r *albumRepository) PurgeInactive(active domain.Albums) ([]string, error) {
|
|
activeIDs := make([]string, len(active))
|
|
for i, album := range active {
|
|
activeIDs[i] = album.ID
|
|
}
|
|
|
|
return r.purgeInactive(activeIDs)
|
|
}
|
|
|
|
func (r *albumRepository) GetStarred(options domain.QueryOptions) (domain.Albums, error) {
|
|
var starred []_Album
|
|
err := r.execute(q.Eq("Starred", true), &starred, &options)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return r.toDomainList(starred)
|
|
}
|
|
|
|
var _ domain.AlbumRepository = (*albumRepository)(nil)
|
|
var _ = domain.Album(_Album{})
|