master
Evan Chen 2021-12-19 02:44:34 +08:00
parent 6b53ae502d
commit a84c90f459
3 changed files with 36 additions and 4 deletions

View File

@ -33,9 +33,11 @@ func Init() {
}
func Add(Key, Description, Default string) error {
result := store.DB.Exec(`select "value" from "attributes" where "key" = ?`, Key)
if result.RowsAffected != 0 {
return nil
ctr := 0
store.DB.Raw(`select count(*) from "attributes" where "key" = ?`, Key).
Scan(&ctr)
if ctr != 0 {
return ErrorAttributeExist
}
a := &Attribute{
Key: Key,
@ -43,7 +45,9 @@ func Add(Key, Description, Default string) error {
Default: Default,
Value: Default,
}
return store.DB.Create(a).Error
err := store.DB.Create(a).Error
return err
}
func RestoreDefault(key string) error {

View File

@ -0,0 +1,22 @@
package attribute
import (
"fmt"
"testing"
"github.com/spf13/viper"
"kumoly.io/kumoly/app/store"
)
func TestAttr(t *testing.T) {
viper.Set("data", "work")
viper.Set("db.type", "sqlite")
store.Setup()
Init()
fmt.Println(Add("test", "", "1"))
fmt.Println(Add("test1", "", "2"))
fmt.Println(Add("test2", "", "2"))
fmt.Println(Add("test3", "", "2"))
fmt.Println(Set("test", "reset"))
fmt.Println(Get("test"))
}

View File

@ -6,6 +6,12 @@ import (
"kumoly.io/kumoly/app/errors"
)
var ErrorAttributeExist = errors.Error{
Code: http.StatusBadRequest,
ID: "ErrorAttributeExist",
Message: "Attribute already exist",
}
var ErrorAttributeNotFound = errors.Error{
Code: http.StatusNotFound,
ID: "ErrorTokenNotValid",