SQL/Orm PropertyRepository complete

This commit is contained in:
Deluan
2020-01-12 21:46:40 -05:00
committed by Deluan Quintão
parent 3d706e3e98
commit 5d519dcecf
5 changed files with 88 additions and 2 deletions
+55
View File
@@ -0,0 +1,55 @@
package db_sql
import (
"github.com/astaxie/beego/orm"
"github.com/cloudsonic/sonic-server/domain"
)
type Property struct {
ID string `orm:"pk;column(id)"`
Value string
}
type propertyRepository struct {
sqlRepository
}
func NewPropertyRepository() domain.PropertyRepository {
r := &propertyRepository{}
r.entityName = "property"
return r
}
func (r *propertyRepository) Put(id string, value string) error {
p := &Property{ID: id, Value: value}
num, err := Db().Update(p)
if err != nil {
return nil
}
if num == 0 {
_, err = Db().Insert(p)
}
return err
}
func (r *propertyRepository) Get(id string) (string, error) {
p := &Property{ID: id}
err := Db().Read(p)
if err == orm.ErrNoRows {
return "", domain.ErrNotFound
}
return p.Value, err
}
func (r *propertyRepository) DefaultGet(id string, defaultValue string) (string, error) {
value, err := r.Get(id)
if err == domain.ErrNotFound {
return defaultValue, nil
}
if err != nil {
return defaultValue, err
}
return value, nil
}
var _ domain.PropertyRepository = (*propertyRepository)(nil)