Sorting artist index

This commit is contained in:
Deluan 2016-03-01 19:50:20 -05:00
parent 6092076fad
commit 25a9320bce
4 changed files with 20 additions and 6 deletions

View File

@ -121,16 +121,19 @@ func (r *BaseRepository) toEntity(response [][]byte, entity interface{}) error {
}
// TODO Optimize it! Probably very slow (and confusing!)
func (r *BaseRepository) loadAll(entities interface{}) error {
func (r *BaseRepository) loadAll(entities interface{}, sortBy string) error {
total, err := r.CountAll()
if (err != nil) {
return err
}
reflected := reflect.ValueOf(entities).Elem()
var sortKey []byte = nil
if sortBy != "" {
sortKey = []byte(fmt.Sprintf("%s:*:%s", r.table, sortBy))
}
setName := r.table + "s:all"
response, err := db().XSSort([]byte(setName), 0, 0, true, false, nil, r.getFieldKeys("*"))
response, err := db().XSSort([]byte(setName), 0, 0, true, false, sortKey, r.getFieldKeys("*"))
if (err != nil) {
return err
}

View File

@ -131,7 +131,7 @@ func TestBaseRepository(t *testing.T) {
Convey("When I call loadAll", func() {
var es = make([]TestEntity, 0)
err := repo.loadAll(&es)
err := repo.loadAll(&es, "")
Convey("Then It should not return any error", func() {
So(err, ShouldBeNil)
})

View File

@ -3,6 +3,8 @@ package repositories
import (
"github.com/deluan/gosonic/models"
"errors"
"sort"
"github.com/deluan/gosonic/utils"
)
type ArtistIndex interface {
@ -36,8 +38,17 @@ func (r *artistIndex) Get(id string) (*models.ArtistIndex, error) {
func (r *artistIndex) GetAll() ([]models.ArtistIndex, error) {
var indices = make([]models.ArtistIndex, 0)
err := r.loadAll(&indices)
err := r.loadAll(&indices, "")
if err == nil {
for _, idx := range indices {
sort.Sort(byArtistName(idx.Artists))
}
}
return indices, err
}
type byArtistName []models.ArtistInfo
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) }

View File

@ -16,7 +16,7 @@ func (s *ItunesScanner) LoadFolder(path string) []Track {
mediaFiles := make([]Track, len(l.Tracks))
i := 0
for id, t := range l.Tracks {
if t.Location != "" && strings.Contains(t.Kind, "audio") {
if strings.HasPrefix(t.Location, "file://") && strings.Contains(t.Kind, "audio") {
mediaFiles[i].Id = id
mediaFiles[i].Album = unescape(t.Album)
mediaFiles[i].Title = unescape(t.Name)