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