2020-01-15 00:23:29 +01:00
|
|
|
package persistence
|
2020-01-13 03:46:40 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/astaxie/beego/orm"
|
2020-01-15 04:22:34 +01:00
|
|
|
"github.com/cloudsonic/sonic-server/model"
|
2020-01-13 03:46:40 +01:00
|
|
|
)
|
|
|
|
|
2020-01-18 03:03:54 +01:00
|
|
|
type property struct {
|
2020-01-13 03:46:40 +01:00
|
|
|
ID string `orm:"pk;column(id)"`
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
type propertyRepository struct {
|
|
|
|
sqlRepository
|
|
|
|
}
|
|
|
|
|
2020-01-19 21:37:41 +01:00
|
|
|
func NewPropertyRepository(o orm.Ormer) model.PropertyRepository {
|
2020-01-13 03:46:40 +01:00
|
|
|
r := &propertyRepository{}
|
2020-01-19 21:37:41 +01:00
|
|
|
r.ormer = o
|
2020-01-13 06:04:11 +01:00
|
|
|
r.tableName = "property"
|
2020-01-13 03:46:40 +01:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *propertyRepository) Put(id string, value string) error {
|
2020-01-18 03:03:54 +01:00
|
|
|
p := &property{ID: id, Value: value}
|
2020-01-19 21:37:41 +01:00
|
|
|
num, err := r.ormer.Update(p)
|
2020-01-13 03:46:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if num == 0 {
|
2020-01-19 21:37:41 +01:00
|
|
|
_, err = r.ormer.Insert(p)
|
2020-01-13 03:46:40 +01:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *propertyRepository) Get(id string) (string, error) {
|
2020-01-18 03:03:54 +01:00
|
|
|
p := &property{ID: id}
|
2020-01-19 21:37:41 +01:00
|
|
|
err := r.ormer.Read(p)
|
2020-01-13 03:46:40 +01:00
|
|
|
if err == orm.ErrNoRows {
|
2020-01-15 04:22:34 +01:00
|
|
|
return "", model.ErrNotFound
|
2020-01-13 03:46:40 +01:00
|
|
|
}
|
|
|
|
return p.Value, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *propertyRepository) DefaultGet(id string, defaultValue string) (string, error) {
|
|
|
|
value, err := r.Get(id)
|
2020-01-15 04:22:34 +01:00
|
|
|
if err == model.ErrNotFound {
|
2020-01-13 03:46:40 +01:00
|
|
|
return defaultValue, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return defaultValue, err
|
|
|
|
}
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 04:22:34 +01:00
|
|
|
var _ model.PropertyRepository = (*propertyRepository)(nil)
|