quintodrome/repositories/index_repository.go

55 lines
1.3 KiB
Go
Raw Normal View History

package repositories
import (
"github.com/deluan/gosonic/models"
"errors"
2016-03-01 15:50:20 -09:00
"sort"
"github.com/deluan/gosonic/utils"
)
type ArtistIndex interface {
Put(m *models.ArtistIndex) error
Get(id string) (*models.ArtistIndex, error)
GetAll() ([]models.ArtistIndex, error)
}
2016-03-01 15:17:30 -09:00
type artistIndex struct {
BaseRepository
}
func NewArtistIndexRepository() ArtistIndex {
2016-03-01 15:17:30 -09:00
r := &artistIndex{}
r.init("index", &models.ArtistIndex{})
return r
}
2016-03-01 15:17:30 -09:00
func (r *artistIndex) Put(m *models.ArtistIndex) error {
if m.Id == "" {
return errors.New("Id is not set")
}
return r.saveOrUpdate(m.Id, m)
}
2016-03-01 15:17:30 -09:00
func (r *artistIndex) Get(id string) (*models.ArtistIndex, error) {
var rec interface{}
rec, err := r.readEntity(id)
return rec.(*models.ArtistIndex), err
2016-02-29 04:34:57 -09:00
}
2016-03-01 15:17:30 -09:00
func (r *artistIndex) GetAll() ([]models.ArtistIndex, error) {
2016-02-29 18:39:36 -09:00
var indices = make([]models.ArtistIndex, 0)
2016-03-01 15:50:20 -09:00
err := r.loadAll(&indices, "")
if err == nil {
for _, idx := range indices {
sort.Sort(byArtistName(idx.Artists))
}
}
return indices, err
2016-02-29 04:34:57 -09:00
}
2016-03-01 15:50:20 -09:00
type byArtistName []models.ArtistInfo
2016-03-01 15:50:20 -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) }