navidrome/persistence/index_repository.go

51 lines
1.1 KiB
Go
Raw Normal View History

2016-03-02 15:07:24 +01:00
package persistence
import (
"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-03-02 01:17:30 +01:00
type artistIndex struct {
baseRepository
}
2016-03-02 15:07:24 +01:00
func NewArtistIndexRepository() domain.ArtistIndexRepository {
2016-03-02 01:17:30 +01:00
r := &artistIndex{}
2016-03-02 15:07:24 +01:00
r.init("index", &domain.ArtistIndex{})
return r
}
2016-03-02 15:07:24 +01:00
func (r *artistIndex) Put(m *domain.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 15:07:24 +01:00
func (r *artistIndex) Get(id string) (*domain.ArtistIndex, error) {
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-02 15:07:24 +01:00
func (r *artistIndex) GetAll() ([]domain.ArtistIndex, error) {
var indices = make([]domain.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 15:07:24 +01:00
type byArtistName []domain.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)
}