New Folder Scanner - WIP
This commit is contained in:
@@ -2,9 +2,12 @@ package persistence
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/cloudsonic/sonic-server/conf"
|
||||
"github.com/cloudsonic/sonic-server/model"
|
||||
"github.com/cloudsonic/sonic-server/utils"
|
||||
)
|
||||
|
||||
type ArtistInfo struct {
|
||||
@@ -15,6 +18,8 @@ type ArtistInfo struct {
|
||||
AlbumCount int
|
||||
}
|
||||
|
||||
type tempIndex map[string]model.ArtistInfo
|
||||
|
||||
type artistIndexRepository struct {
|
||||
sqlRepository
|
||||
}
|
||||
@@ -25,15 +30,6 @@ func NewArtistIndexRepository() model.ArtistIndexRepository {
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *artistIndexRepository) CountAll() (int64, error) {
|
||||
count := struct{ Count int64 }{}
|
||||
err := Db().Raw("select count(distinct(idx)) as count from artist_info").QueryRow(&count)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count.Count, nil
|
||||
}
|
||||
|
||||
func (r *artistIndexRepository) Put(idx *model.ArtistIndex) error {
|
||||
return withTx(func(o orm.Ormer) error {
|
||||
_, err := r.newQuery(o).Filter("idx", idx.ID).Delete()
|
||||
@@ -56,23 +52,64 @@ func (r *artistIndexRepository) Put(idx *model.ArtistIndex) error {
|
||||
})
|
||||
}
|
||||
|
||||
func (r *artistIndexRepository) Get(id string) (*model.ArtistIndex, error) {
|
||||
var ais []ArtistInfo
|
||||
_, err := r.newQuery(Db()).Filter("idx", id).All(&ais)
|
||||
func (r *artistIndexRepository) Refresh() error {
|
||||
o := Db()
|
||||
|
||||
indexGroups := utils.ParseIndexGroups(conf.Sonic.IndexGroups)
|
||||
artistIndex := make(map[string]tempIndex)
|
||||
|
||||
var artists []Artist
|
||||
_, err := o.QueryTable(&Artist{}).All(&artists)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
idx := &model.ArtistIndex{ID: id}
|
||||
idx.Artists = make([]model.ArtistInfo, len(ais))
|
||||
for i, a := range ais {
|
||||
idx.Artists[i] = model.ArtistInfo{
|
||||
ArtistID: a.ArtistID,
|
||||
Artist: a.Artist,
|
||||
AlbumCount: a.AlbumCount,
|
||||
for _, ar := range artists {
|
||||
r.collectIndex(indexGroups, &ar, artistIndex)
|
||||
}
|
||||
|
||||
return r.saveIndex(artistIndex)
|
||||
}
|
||||
|
||||
func (r *artistIndexRepository) collectIndex(ig utils.IndexGroups, a *Artist, artistIndex map[string]tempIndex) {
|
||||
name := a.Name
|
||||
indexName := strings.ToLower(utils.NoArticle(name))
|
||||
if indexName == "" {
|
||||
return
|
||||
}
|
||||
group := r.findGroup(ig, indexName)
|
||||
artists := artistIndex[group]
|
||||
if artists == nil {
|
||||
artists = make(tempIndex)
|
||||
artistIndex[group] = artists
|
||||
}
|
||||
artists[indexName] = model.ArtistInfo{ArtistID: a.ID, Artist: a.Name, AlbumCount: a.AlbumCount}
|
||||
}
|
||||
|
||||
func (r *artistIndexRepository) findGroup(ig utils.IndexGroups, name string) string {
|
||||
for k, v := range ig {
|
||||
key := strings.ToLower(k)
|
||||
if strings.HasPrefix(name, key) {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return idx, err
|
||||
return "#"
|
||||
}
|
||||
|
||||
func (r *artistIndexRepository) saveIndex(artistIndex map[string]tempIndex) error {
|
||||
r.DeleteAll()
|
||||
for k, temp := range artistIndex {
|
||||
idx := &model.ArtistIndex{ID: k}
|
||||
for _, v := range temp {
|
||||
idx.Artists = append(idx.Artists, v)
|
||||
}
|
||||
err := r.Put(idx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *artistIndexRepository) GetAll() (model.ArtistIndexes, error) {
|
||||
|
||||
Reference in New Issue
Block a user