2016-03-02 15:07:24 +01:00
|
|
|
package persistence
|
2016-02-28 19:50:05 +01:00
|
|
|
|
|
|
|
import (
|
2016-03-04 22:42:09 +01:00
|
|
|
"errors"
|
2016-03-08 20:18:17 +01:00
|
|
|
|
2016-03-02 15:07:24 +01:00
|
|
|
"github.com/deluan/gosonic/domain"
|
2016-02-28 19:50:05 +01:00
|
|
|
)
|
|
|
|
|
2016-03-02 15:33:49 +01:00
|
|
|
type albumRepository struct {
|
2016-03-04 02:16:09 +01:00
|
|
|
ledisRepository
|
2016-02-28 19:50:05 +01:00
|
|
|
}
|
|
|
|
|
2016-03-02 15:33:49 +01:00
|
|
|
func NewAlbumRepository() domain.AlbumRepository {
|
|
|
|
r := &albumRepository{}
|
2016-03-02 15:07:24 +01:00
|
|
|
r.init("album", &domain.Album{})
|
2016-02-28 19:50:05 +01:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2016-03-02 15:33:49 +01:00
|
|
|
func (r *albumRepository) Put(m *domain.Album) error {
|
2016-02-28 19:50:05 +01:00
|
|
|
if m.Id == "" {
|
2016-03-04 22:42:09 +01:00
|
|
|
return errors.New("Album Id is not set")
|
2016-02-28 19:50:05 +01:00
|
|
|
}
|
2016-02-29 04:56:24 +01:00
|
|
|
return r.saveOrUpdate(m.Id, m)
|
2016-02-28 19:50:05 +01:00
|
|
|
}
|
|
|
|
|
2016-03-02 15:33:49 +01:00
|
|
|
func (r *albumRepository) Get(id string) (*domain.Album, error) {
|
2016-03-01 04:17:54 +01:00
|
|
|
var rec interface{}
|
|
|
|
rec, err := r.readEntity(id)
|
2016-03-02 15:07:24 +01:00
|
|
|
return rec.(*domain.Album), err
|
2016-03-02 19:18:39 +01:00
|
|
|
}
|
2016-03-03 04:22:31 +01:00
|
|
|
|
2016-03-04 03:01:55 +01:00
|
|
|
func (r *albumRepository) FindByArtist(artistId string) (domain.Albums, error) {
|
|
|
|
var as = make(domain.Albums, 0)
|
2016-03-04 15:09:16 +01:00
|
|
|
err := r.loadChildren("artist", artistId, &as, domain.QueryOptions{SortBy: "Year"})
|
2016-03-03 04:22:31 +01:00
|
|
|
return as, err
|
|
|
|
}
|
2016-03-03 07:24:28 +01:00
|
|
|
|
2016-03-04 15:09:16 +01:00
|
|
|
func (r *albumRepository) GetAll(options domain.QueryOptions) (domain.Albums, error) {
|
|
|
|
var as = make(domain.Albums, 0)
|
|
|
|
err := r.loadAll(&as, options)
|
|
|
|
return as, err
|
|
|
|
}
|
|
|
|
|
2016-03-08 20:18:17 +01:00
|
|
|
func (r *albumRepository) PurgeInactive(active *domain.Albums) error {
|
|
|
|
currentIds, err := r.GetAllIds()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, a := range *active {
|
|
|
|
currentIds[a.Id] = false
|
|
|
|
}
|
|
|
|
inactiveIds := make(map[string]bool)
|
|
|
|
for id, inactive := range currentIds {
|
|
|
|
if inactive {
|
|
|
|
inactiveIds[id] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r.DeleteAll(inactiveIds)
|
|
|
|
}
|
|
|
|
|
2016-03-04 15:09:16 +01:00
|
|
|
var _ domain.AlbumRepository = (*albumRepository)(nil)
|