Fixed AlbumId generation. Some code cleanup

This commit is contained in:
Deluan
2016-03-01 09:17:28 -05:00
parent 20650ed3fd
commit 375fd30045
8 changed files with 20 additions and 47 deletions
+2 -8
View File
@@ -16,7 +16,7 @@ func NewAlbumRepository() *Album {
func (r *Album) Put(m *models.Album) error {
if m.Id == "" {
m.Id = r.NewId(m.Name)
m.Id = r.NewId(m.ArtistId, m.Name)
}
return r.saveOrUpdate(m.Id, m)
}
@@ -25,10 +25,4 @@ func (r *Album) Get(id string) (*models.Album, error) {
var rec interface{}
rec, err := r.readEntity(id)
return rec.(*models.Album), err
}
func (r *Album) GetByName(name string) (*models.Album, error) {
id := r.NewId(name)
return r.Get(id)
}
}
+12 -6
View File
@@ -5,30 +5,36 @@ import (
"errors"
)
type ArtistIndex struct {
type ArtistIndex interface {
Put(m *models.ArtistIndex) error
Get(id string) (*models.ArtistIndex, error)
GetAll() ([]models.ArtistIndex, error)
}
type artistIndex struct {
BaseRepository
}
func NewArtistIndexRepository() *ArtistIndex {
r := &ArtistIndex{}
func NewArtistIndexRepository() ArtistIndex {
r := &artistIndex{}
r.init("index", &models.ArtistIndex{})
return r
}
func (r *ArtistIndex) Put(m *models.ArtistIndex) error {
func (r *artistIndex) Put(m *models.ArtistIndex) error {
if m.Id == "" {
return errors.New("Id is not set")
}
return r.saveOrUpdate(m.Id, m)
}
func (r *ArtistIndex) Get(id string) (*models.ArtistIndex, error) {
func (r *artistIndex) Get(id string) (*models.ArtistIndex, error) {
var rec interface{}
rec, err := r.readEntity(id)
return rec.(*models.ArtistIndex), err
}
func (r *ArtistIndex) GetAll() ([]models.ArtistIndex, error) {
func (r *artistIndex) GetAll() ([]models.ArtistIndex, error) {
var indices = make([]models.ArtistIndex, 0)
err := r.loadAll(&indices)
return indices, err
+1 -1
View File
@@ -23,7 +23,7 @@ func TestIndexRepository(t *testing.T) {
So(s, shouldBeEqual, i)
})
Convey("Put() should return error if Id is not set", func() {
Convey("Method Put() should return error if Id is not set", func() {
i := &models.ArtistIndex{}
err := repo.Put(i)