2016-03-02 15:07:24 +01:00
|
|
|
package persistence
|
2016-02-28 21:46:53 +01:00
|
|
|
|
|
|
|
import (
|
2016-03-01 04:17:54 +01:00
|
|
|
"errors"
|
2016-03-02 19:18:39 +01:00
|
|
|
"github.com/deluan/gosonic/domain"
|
2016-03-02 01:50:20 +01:00
|
|
|
"github.com/deluan/gosonic/utils"
|
2016-03-02 19:18:39 +01:00
|
|
|
"sort"
|
2016-02-28 21:46:53 +01:00
|
|
|
)
|
|
|
|
|
2016-03-03 02:00:55 +01:00
|
|
|
type artistIndexRepository struct {
|
2016-03-04 02:16:09 +01:00
|
|
|
ledisRepository
|
2016-02-28 21:46:53 +01:00
|
|
|
}
|
|
|
|
|
2016-03-02 15:07:24 +01:00
|
|
|
func NewArtistIndexRepository() domain.ArtistIndexRepository {
|
2016-03-03 02:00:55 +01:00
|
|
|
r := &artistIndexRepository{}
|
2016-03-02 15:07:24 +01:00
|
|
|
r.init("index", &domain.ArtistIndex{})
|
2016-02-28 21:46:53 +01:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2016-03-03 02:00:55 +01:00
|
|
|
func (r *artistIndexRepository) Put(m *domain.ArtistIndex) error {
|
2016-02-28 21:46:53 +01:00
|
|
|
if m.Id == "" {
|
|
|
|
return errors.New("Id is not set")
|
|
|
|
}
|
2016-03-02 02:51:30 +01:00
|
|
|
sort.Sort(byArtistName(m.Artists))
|
2016-02-28 21:46:53 +01:00
|
|
|
return r.saveOrUpdate(m.Id, m)
|
|
|
|
}
|
|
|
|
|
2016-03-03 02:00:55 +01:00
|
|
|
func (r *artistIndexRepository) Get(id string) (*domain.ArtistIndex, 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.ArtistIndex), err
|
2016-02-29 14:34:57 +01:00
|
|
|
}
|
|
|
|
|
2016-03-04 03:01:55 +01:00
|
|
|
func (r *artistIndexRepository) GetAll() (domain.ArtistIndexes, error) {
|
|
|
|
var indices = make(domain.ArtistIndexes, 0)
|
2016-03-03 05:20:17 +01:00
|
|
|
err := r.loadAll(&indices, "", true)
|
2016-03-01 04:17:54 +01:00
|
|
|
return indices, err
|
2016-02-29 14:34:57 +01:00
|
|
|
}
|
2016-02-28 21:46:53 +01:00
|
|
|
|
2016-03-02 15:07:24 +01:00
|
|
|
type byArtistName []domain.ArtistInfo
|
2016-02-28 21:46:53 +01:00
|
|
|
|
2016-03-02 02:51:30 +01:00
|
|
|
func (a byArtistName) Len() int {
|
|
|
|
return len(a)
|
|
|
|
}
|
|
|
|
func (a byArtistName) Swap(i, j int) {
|
|
|
|
a[i], a[j] = a[j], a[i]
|
|
|
|
}
|
|
|
|
func (a byArtistName) Less(i, j int) bool {
|
|
|
|
return utils.NoArticle(a[i].Artist) < utils.NoArticle(a[j].Artist)
|
|
|
|
}
|
2016-03-03 07:24:28 +01:00
|
|
|
|
|
|
|
var _ domain.ArtistIndexRepository = (*artistIndexRepository)(nil)
|