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-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 artistRepository 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 NewArtistRepository() domain.ArtistRepository {
|
|
|
|
|
r := &artistRepository{}
|
2016-03-02 05:07:24 -09:00
|
|
|
r.init("artist", &domain.Artist{})
|
2016-02-28 09:50:05 -09:00
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-02 05:33:49 -09:00
|
|
|
func (r *artistRepository) Put(m *domain.Artist) error {
|
2016-02-28 09:50:05 -09:00
|
|
|
if m.Id == "" {
|
2016-03-04 12:42:09 -09:00
|
|
|
return errors.New("Artist 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 *artistRepository) Get(id string) (*domain.Artist, 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.Artist), err
|
2016-02-28 09:50:05 -09:00
|
|
|
}
|
|
|
|
|
|
2016-03-02 05:33:49 -09:00
|
|
|
func (r *artistRepository) GetByName(name string) (*domain.Artist, error) {
|
2016-02-28 09:50:05 -09:00
|
|
|
id := r.NewId(name)
|
|
|
|
|
return r.Get(id)
|
|
|
|
|
}
|
2016-03-02 21:24:28 -09:00
|
|
|
|
2016-03-08 10:18:17 -09:00
|
|
|
func (r *artistRepository) PurgeInactive(active *domain.Artists) error {
|
2016-03-08 17:54:32 -09:00
|
|
|
currentIds, err := r.getAllIds()
|
2016-03-08 10:18:17 -09:00
|
|
|
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 12:42:09 -09:00
|
|
|
var _ domain.ArtistRepository = (*artistRepository)(nil)
|