Integrate ArtistIndex logic into Artist

This commit is contained in:
Deluan
2020-01-17 20:46:19 -05:00
parent 035fdaeb96
commit c0beaa6377
17 changed files with 110 additions and 427 deletions
+53 -9
View File
@@ -2,11 +2,14 @@ package persistence
import (
"fmt"
"sort"
"strings"
"github.com/astaxie/beego/orm"
"github.com/cloudsonic/sonic-server/conf"
"github.com/cloudsonic/sonic-server/log"
"github.com/cloudsonic/sonic-server/model"
"github.com/cloudsonic/sonic-server/utils"
)
type Artist struct {
@@ -17,14 +20,27 @@ type Artist struct {
type artistRepository struct {
searchableRepository
indexGroups utils.IndexGroups
}
func NewArtistRepository() model.ArtistRepository {
r := &artistRepository{}
r.indexGroups = utils.ParseIndexGroups(conf.Sonic.IndexGroups)
r.tableName = "artist"
return r
}
func (r *artistRepository) getIndexKey(a *Artist) string {
name := strings.ToLower(utils.NoArticle(a.Name))
for k, v := range r.indexGroups {
key := strings.ToLower(k)
if strings.HasPrefix(name, key) {
return v
}
}
return "#"
}
func (r *artistRepository) Put(a *model.Artist) error {
ta := Artist(*a)
return withTx(func(o orm.Ormer) error {
@@ -45,6 +61,34 @@ func (r *artistRepository) Get(id string) (*model.Artist, error) {
return &a, nil
}
// TODO Cache the index (recalculate when there are changes to the DB)
func (r *artistRepository) GetIndex() (model.ArtistIndexes, error) {
var all []Artist
_, err := r.newQuery(Db()).OrderBy("name").All(&all)
if err != nil {
return nil, err
}
fullIdx := make(map[string]*model.ArtistIndex)
for _, a := range all {
ax := r.getIndexKey(&a)
idx, ok := fullIdx[ax]
if !ok {
idx = &model.ArtistIndex{ID: ax}
fullIdx[ax] = idx
}
idx.Artists = append(idx.Artists, model.Artist(a))
}
var result model.ArtistIndexes
for _, idx := range fullIdx {
result = append(result, *idx)
}
sort.Slice(result, func(i, j int) bool {
return result[i].ID < result[j].ID
})
return result, nil
}
func (r *artistRepository) Refresh(ids ...string) error {
type refreshArtist struct {
Artist
@@ -71,17 +115,17 @@ where f.artist_id in ('%s') group by f.artist_id order by f.id`, strings.Join(id
var toInsert []Artist
var toUpdate []Artist
for _, al := range artists {
if al.Compilation {
al.AlbumArtist = "Various Artists"
for _, ar := range artists {
if ar.Compilation {
ar.AlbumArtist = "Various Artists"
}
if al.AlbumArtist != "" {
al.Name = al.AlbumArtist
if ar.AlbumArtist != "" {
ar.Name = ar.AlbumArtist
}
if al.CurrentId != "" {
toUpdate = append(toUpdate, al.Artist)
if ar.CurrentId != "" {
toUpdate = append(toUpdate, ar.Artist)
} else {
toInsert = append(toInsert, al.Artist)
toInsert = append(toInsert, ar.Artist)
}
}
if len(toInsert) > 0 {
@@ -93,7 +137,7 @@ where f.artist_id in ('%s') group by f.artist_id order by f.id`, strings.Join(id
}
if len(toUpdate) > 0 {
for _, al := range toUpdate {
_, err := o.Update(&al, "name", "album_count")
_, err := o.Update(&al)
if err != nil {
return err
}