quintodrome/persistence/index_repository.go

109 lines
2.4 KiB
Go
Raw Normal View History

package persistence
2020-01-12 17:28:47 -09:00
import (
"sort"
"github.com/astaxie/beego/orm"
2020-01-14 18:22:34 -09:00
"github.com/cloudsonic/sonic-server/model"
2020-01-12 17:28:47 -09:00
)
type ArtistInfo struct {
ID int `orm:"pk;auto;column(id)"`
Idx string
ArtistID string `orm:"column(artist_id)"`
Artist string
AlbumCount int
}
type artistIndexRepository struct {
sqlRepository
}
2020-01-14 18:22:34 -09:00
func NewArtistIndexRepository() model.ArtistIndexRepository {
2020-01-12 17:28:47 -09:00
r := &artistIndexRepository{}
2020-01-12 20:04:11 -09:00
r.tableName = "artist_info"
2020-01-12 17:28:47 -09:00
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
}
2020-01-14 18:22:34 -09:00
func (r *artistIndexRepository) Put(idx *model.ArtistIndex) error {
return withTx(func(o orm.Ormer) error {
2020-01-12 17:28:47 -09:00
_, err := r.newQuery(o).Filter("idx", idx.ID).Delete()
if err != nil {
return err
}
for _, artist := range idx.Artists {
a := ArtistInfo{
Idx: idx.ID,
ArtistID: artist.ArtistID,
Artist: artist.Artist,
AlbumCount: artist.AlbumCount,
}
2020-01-14 15:20:47 -09:00
err := r.insert(o, &a)
2020-01-12 17:28:47 -09:00
if err != nil {
return err
}
}
return nil
})
}
2020-01-14 18:22:34 -09:00
func (r *artistIndexRepository) Get(id string) (*model.ArtistIndex, error) {
2020-01-12 17:28:47 -09:00
var ais []ArtistInfo
_, err := r.newQuery(Db()).Filter("idx", id).All(&ais)
if err != nil {
return nil, err
}
2020-01-14 18:22:34 -09:00
idx := &model.ArtistIndex{ID: id}
idx.Artists = make([]model.ArtistInfo, len(ais))
2020-01-12 17:28:47 -09:00
for i, a := range ais {
2020-01-14 18:22:34 -09:00
idx.Artists[i] = model.ArtistInfo{
2020-01-12 17:28:47 -09:00
ArtistID: a.ArtistID,
Artist: a.Artist,
AlbumCount: a.AlbumCount,
}
}
return idx, err
}
2020-01-14 18:22:34 -09:00
func (r *artistIndexRepository) GetAll() (model.ArtistIndexes, error) {
2020-01-12 17:28:47 -09:00
var all []ArtistInfo
_, err := r.newQuery(Db()).OrderBy("idx", "artist").All(&all)
if err != nil {
return nil, err
}
2020-01-14 18:22:34 -09:00
fullIdx := make(map[string]*model.ArtistIndex)
2020-01-12 17:28:47 -09:00
for _, a := range all {
idx, ok := fullIdx[a.Idx]
if !ok {
2020-01-14 18:22:34 -09:00
idx = &model.ArtistIndex{ID: a.Idx}
2020-01-12 17:28:47 -09:00
fullIdx[a.Idx] = idx
}
2020-01-14 18:22:34 -09:00
idx.Artists = append(idx.Artists, model.ArtistInfo{
2020-01-12 17:28:47 -09:00
ArtistID: a.ArtistID,
Artist: a.Artist,
AlbumCount: a.AlbumCount,
})
}
2020-01-14 18:22:34 -09:00
var result model.ArtistIndexes
2020-01-12 17:28:47 -09:00
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
}
2020-01-14 18:22:34 -09:00
var _ model.ArtistIndexRepository = (*artistIndexRepository)(nil)