quintodrome/persistence/artist_repository.go

78 lines
1.7 KiB
Go
Raw Normal View History

package persistence
2020-01-12 13:32:06 -09:00
import (
"github.com/astaxie/beego/orm"
2020-01-14 18:22:34 -09:00
"github.com/cloudsonic/sonic-server/model"
2020-01-12 13:32:06 -09:00
)
// This is used to isolate Storm's struct tags from the domain, to keep it agnostic of persistence details
type Artist struct {
ID string `orm:"pk;column(id)"`
Name string `orm:"index"`
AlbumCount int `orm:"column(album_count)"`
}
type artistRepository struct {
searchableRepository
2020-01-12 13:32:06 -09:00
}
2020-01-14 18:22:34 -09:00
func NewArtistRepository() model.ArtistRepository {
2020-01-12 13:32:06 -09:00
r := &artistRepository{}
2020-01-12 20:04:11 -09:00
r.tableName = "artist"
2020-01-12 13:32:06 -09:00
return r
}
2020-01-14 18:22:34 -09:00
func (r *artistRepository) Put(a *model.Artist) error {
2020-01-12 13:32:06 -09:00
ta := Artist(*a)
return withTx(func(o orm.Ormer) error {
return r.put(o, a.ID, a.Name, &ta)
})
2020-01-12 13:32:06 -09:00
}
2020-01-14 18:22:34 -09:00
func (r *artistRepository) Get(id string) (*model.Artist, error) {
2020-01-12 13:32:06 -09:00
ta := Artist{ID: id}
err := Db().Read(&ta)
if err == orm.ErrNoRows {
2020-01-14 18:22:34 -09:00
return nil, model.ErrNotFound
2020-01-12 13:32:06 -09:00
}
if err != nil {
return nil, err
}
2020-01-14 18:22:34 -09:00
a := model.Artist(ta)
2020-01-12 13:32:06 -09:00
return &a, nil
}
2020-01-14 18:22:34 -09:00
func (r *artistRepository) PurgeInactive(activeList model.Artists) error {
return withTx(func(o orm.Ormer) error {
_, err := r.purgeInactive(o, activeList, func(item interface{}) string {
2020-01-14 18:22:34 -09:00
return item.(model.Artist).ID
})
return err
2020-01-12 13:32:06 -09:00
})
}
2020-01-14 18:22:34 -09:00
func (r *artistRepository) Search(q string, offset int, size int) (model.Artists, error) {
if len(q) <= 2 {
return nil, nil
}
var results []Artist
err := r.doSearch(r.tableName, q, offset, size, &results, "name")
if err != nil {
return nil, err
}
return r.toArtists(results), nil
}
2020-01-14 18:22:34 -09:00
func (r *artistRepository) toArtists(all []Artist) model.Artists {
result := make(model.Artists, len(all))
for i, a := range all {
2020-01-14 18:22:34 -09:00
result[i] = model.Artist(a)
}
return result
}
2020-01-14 18:22:34 -09:00
var _ model.ArtistRepository = (*artistRepository)(nil)
var _ = model.Artist(Artist{})