2016-03-02 05:07:24 -09:00
|
|
|
package persistence
|
2016-02-28 09:50:05 -09:00
|
|
|
|
|
|
|
|
import (
|
2016-03-04 12:42:09 -09:00
|
|
|
"errors"
|
2016-03-22 15:00:18 -08:00
|
|
|
"time"
|
2016-03-08 10:18:17 -09:00
|
|
|
|
2016-03-02 05:07:24 -09:00
|
|
|
"github.com/deluan/gosonic/domain"
|
2016-02-28 09:50:05 -09:00
|
|
|
)
|
|
|
|
|
|
2016-03-02 05:33:49 -09:00
|
|
|
type albumRepository struct {
|
2016-03-03 16:16:09 -09:00
|
|
|
ledisRepository
|
2016-02-28 09:50:05 -09:00
|
|
|
}
|
|
|
|
|
|
2016-03-02 05:33:49 -09:00
|
|
|
func NewAlbumRepository() domain.AlbumRepository {
|
|
|
|
|
r := &albumRepository{}
|
2016-03-02 05:07:24 -09:00
|
|
|
r.init("album", &domain.Album{})
|
2016-02-28 09:50:05 -09:00
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-02 05:33:49 -09:00
|
|
|
func (r *albumRepository) Put(m *domain.Album) error {
|
2016-02-28 09:50:05 -09:00
|
|
|
if m.Id == "" {
|
2016-03-23 12:30:38 -08:00
|
|
|
return errors.New("album Id is not set")
|
2016-02-28 09:50:05 -09:00
|
|
|
}
|
2016-02-28 18:56:24 -09:00
|
|
|
return r.saveOrUpdate(m.Id, m)
|
2016-02-28 09:50:05 -09:00
|
|
|
}
|
|
|
|
|
|
2016-03-02 05:33:49 -09:00
|
|
|
func (r *albumRepository) Get(id string) (*domain.Album, error) {
|
2016-02-29 18:17:54 -09:00
|
|
|
var rec interface{}
|
|
|
|
|
rec, err := r.readEntity(id)
|
2016-03-02 05:07:24 -09:00
|
|
|
return rec.(*domain.Album), err
|
2016-03-02 09:18:39 -09:00
|
|
|
}
|
2016-03-02 18:22:31 -09:00
|
|
|
|
2016-03-20 09:14:04 -08:00
|
|
|
func (r *albumRepository) FindByArtist(artistId string) (domain.Albums, error) {
|
2016-03-03 17:01:55 -09:00
|
|
|
var as = make(domain.Albums, 0)
|
2016-03-04 05:09:16 -09:00
|
|
|
err := r.loadChildren("artist", artistId, &as, domain.QueryOptions{SortBy: "Year"})
|
2016-03-20 09:14:04 -08:00
|
|
|
return as, err
|
2016-03-02 18:22:31 -09:00
|
|
|
}
|
2016-03-02 21:24:28 -09:00
|
|
|
|
2016-03-19 18:58:20 -08:00
|
|
|
func (r *albumRepository) GetAll(options domain.QueryOptions) (domain.Albums, error) {
|
2016-03-04 05:09:16 -09:00
|
|
|
var as = make(domain.Albums, 0)
|
|
|
|
|
err := r.loadAll(&as, options)
|
2016-03-19 18:58:20 -08:00
|
|
|
return as, err
|
2016-03-04 05:09:16 -09:00
|
|
|
}
|
|
|
|
|
|
2016-03-19 18:58:20 -08:00
|
|
|
func (r *albumRepository) GetAllIds() ([]string, error) {
|
2016-03-08 17:54:32 -09:00
|
|
|
idMap, err := r.getAllIds()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
ids := make([]string, len(idMap))
|
|
|
|
|
|
|
|
|
|
i := 0
|
2016-03-22 15:00:18 -08:00
|
|
|
for id := range idMap {
|
2016-03-08 17:54:32 -09:00
|
|
|
ids[i] = id
|
|
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-19 18:58:20 -08:00
|
|
|
return ids, nil
|
2016-03-08 17:54:32 -09:00
|
|
|
}
|
|
|
|
|
|
2016-03-18 15:50:21 -08:00
|
|
|
func (r *albumRepository) PurgeInactive(active domain.Albums) ([]string, error) {
|
2016-03-18 15:32:49 -08:00
|
|
|
return r.purgeInactive(active, func(e interface{}) string {
|
|
|
|
|
return e.(domain.Album).Id
|
|
|
|
|
})
|
2016-03-08 10:18:17 -09:00
|
|
|
}
|
|
|
|
|
|
2016-03-20 09:14:04 -08:00
|
|
|
func (r *albumRepository) GetStarred(options domain.QueryOptions) (domain.Albums, error) {
|
2016-03-14 07:35:48 -08:00
|
|
|
var as = make(domain.Albums, 0)
|
2016-04-21 06:44:27 -08:00
|
|
|
start := time.Time{}.Add(1 * time.Hour)
|
2016-03-22 15:00:18 -08:00
|
|
|
err := r.loadRange("Starred", start, time.Now(), &as, options)
|
2016-03-20 09:14:04 -08:00
|
|
|
return as, err
|
2016-03-14 07:35:48 -08:00
|
|
|
}
|
|
|
|
|
|
2016-03-04 05:09:16 -09:00
|
|
|
var _ domain.AlbumRepository = (*albumRepository)(nil)
|