Scanning artists and albums too

This commit is contained in:
Deluan
2016-02-28 13:50:05 -05:00
parent bccfeec2d3
commit 14e52576a7
11 changed files with 209 additions and 51 deletions
+34
View File
@@ -0,0 +1,34 @@
package repositories
import (
"github.com/deluan/gosonic/models"
)
type Album struct {
BaseRepository
}
func NewAlbumRepository() *Album {
r := &Album{}
r.key = "album"
return r
}
func (r *Album) Put(m *models.Album) (*models.Album, error) {
if m.Id == "" {
m.Id = r.NewId(m.Name)
}
return m, r.saveOrUpdate(m.Id, m)
}
func (r *Album) Get(id string) (*models.Album, error) {
rec := &models.Album{}
err := readStruct(r.key, id, rec)
return rec, err
}
func (r *Album) GetByName(name string) (*models.Album, error) {
id := r.NewId(name)
return r.Get(id)
}
+34
View File
@@ -0,0 +1,34 @@
package repositories
import (
"github.com/deluan/gosonic/models"
)
type Artist struct {
BaseRepository
}
func NewArtistRepository() *Artist {
r := &Artist{}
r.key = "artist"
return r
}
func (r *Artist) Put(m *models.Artist) (*models.Artist, error) {
if m.Id == "" {
m.Id = r.NewId(m.Name)
}
return m, r.saveOrUpdate(m.Id, m)
}
func (r *Artist) Get(id string) (*models.Artist, error) {
rec := &models.Artist{}
err := readStruct(r.key, id, rec)
return rec, err
}
func (r *Artist) GetByName(name string) (*models.Artist, error) {
id := r.NewId(name)
return r.Get(id)
}
+14 -4
View File
@@ -1,18 +1,28 @@
package repositories
import (
"fmt"
"crypto/md5"
"strings"
)
type BaseRepository struct {
key string
key string // TODO Rename to 'table'
}
func (r *BaseRepository) saveOrUpdate(id string, rec interface{}) error {
return saveStruct(r.key, id, rec)
func (r *BaseRepository) NewId(fields ...string) string {
s := fmt.Sprintf("%s\\%s", strings.ToUpper(r.key), strings.Join(fields, ""))
return fmt.Sprintf("%x", md5.Sum([]byte(s)))
}
func (r *BaseRepository) CountAll() (int, error) {
return count(r.key)
}
func (r *BaseRepository) saveOrUpdate(id string, rec interface{}) error {
return saveStruct(r.key, id, rec)
}
func (r *BaseRepository) Dump() {
}
@@ -46,8 +46,9 @@ func saveStruct(key, id string, data interface{}) error {
return db().HMset([]byte(kh), fvList...)
}
func readStruct(key string) (interface{}, error) {
fvs, _ := db().HGetAll([]byte(key))
func readStruct(key, id string, rec interface{}) error {
kh := key + "_id_" + id
fvs, _ := db().HGetAll([]byte(kh))
var m = make(map[string]interface{}, len(fvs))
for _, fv := range fvs {
var v interface{}
@@ -55,15 +56,10 @@ func readStruct(key string) (interface{}, error) {
m[string(fv.Field)] = v
}
return utils.ToStruct(m)
return utils.ToStruct(m, rec)
}
func count(key string) (int, error) {
ids, err := db().SMembers([]byte(key + "_ids"))
return len(ids), err
}
func hset(key, field, value string) error {
_, err := db().HSet([]byte(key), []byte(field), []byte(value))
return err
}
+1 -6
View File
@@ -2,8 +2,6 @@ package repositories
import (
"github.com/deluan/gosonic/models"
"fmt"
"crypto/md5"
)
type MediaFile struct {
@@ -16,9 +14,6 @@ func NewMediaFileRepository() *MediaFile {
return r
}
func (r *MediaFile) Add(m *models.MediaFile) error {
if m.Id == "" {
m.Id = fmt.Sprintf("%x", md5.Sum([]byte(m.Path)))
}
func (r *MediaFile) Put(m *models.MediaFile) error {
return r.saveOrUpdate(m.Id, m)
}