navidrome/repositories/index_repository.go

57 lines
1.2 KiB
Go
Raw Normal View History

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