2021-12-18 17:37:44 +00:00
|
|
|
package attribute
|
|
|
|
|
|
|
|
import (
|
2022-01-09 08:31:12 +00:00
|
|
|
"sync"
|
2021-12-18 17:37:44 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"kumoly.io/kumoly/app/store"
|
2021-12-20 02:23:08 +00:00
|
|
|
"kumoly.io/kumoly/app/util"
|
2021-12-18 17:37:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Attribute struct {
|
2021-12-19 06:17:10 +00:00
|
|
|
ID uint `gorm:"primaryKey"`
|
2021-12-18 17:37:44 +00:00
|
|
|
|
|
|
|
Key string `gorm:"unique;not null"`
|
2022-01-08 21:04:24 +00:00
|
|
|
Name string
|
2021-12-18 17:37:44 +00:00
|
|
|
Description string
|
|
|
|
Value string
|
2021-12-19 06:17:10 +00:00
|
|
|
Default string
|
2022-01-09 08:31:12 +00:00
|
|
|
NeedRestart bool
|
2021-12-19 06:17:10 +00:00
|
|
|
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
2021-12-18 17:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var l zerolog.Logger
|
|
|
|
|
2021-12-18 18:12:41 +00:00
|
|
|
func Init() {
|
2021-12-20 02:23:08 +00:00
|
|
|
l = util.Klog.With().Str("mod", "attribute").Logger()
|
2021-12-18 17:37:44 +00:00
|
|
|
|
|
|
|
l.Debug().Str("service", "attribute.Service").
|
|
|
|
Msg("Migrating database for attribute.Service ...")
|
|
|
|
err := store.Migrate(&Attribute{})
|
|
|
|
if err != nil {
|
2021-12-18 18:12:41 +00:00
|
|
|
panic(err)
|
2021-12-18 17:37:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-09 08:31:12 +00:00
|
|
|
func Add(Key, Name, Description, Default string, NeedRestart bool) error {
|
2021-12-18 18:44:34 +00:00
|
|
|
ctr := 0
|
|
|
|
store.DB.Raw(`select count(*) from "attributes" where "key" = ?`, Key).
|
|
|
|
Scan(&ctr)
|
|
|
|
if ctr != 0 {
|
|
|
|
return ErrorAttributeExist
|
2021-12-18 18:01:56 +00:00
|
|
|
}
|
2021-12-18 17:37:44 +00:00
|
|
|
a := &Attribute{
|
|
|
|
Key: Key,
|
2022-01-08 21:04:24 +00:00
|
|
|
Name: Name,
|
2021-12-18 17:37:44 +00:00
|
|
|
Description: Description,
|
|
|
|
Default: Default,
|
2021-12-18 17:54:17 +00:00
|
|
|
Value: Default,
|
2021-12-18 17:37:44 +00:00
|
|
|
}
|
2021-12-18 18:44:34 +00:00
|
|
|
err := store.DB.Create(a).Error
|
|
|
|
|
|
|
|
return err
|
2021-12-18 17:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func RestoreDefault(key string) error {
|
2022-01-09 08:31:12 +00:00
|
|
|
result := store.DB.Exec(`update "attributes" set value = "default" where "key" = ?`, key)
|
2021-12-18 17:37:44 +00:00
|
|
|
if result.RowsAffected == 0 {
|
|
|
|
return ErrorAttributeNotFound
|
|
|
|
}
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func Get(key string) string {
|
|
|
|
var value string
|
|
|
|
store.DB.
|
|
|
|
Raw(`select "value" from "attributes" where "key" = ?`, key).
|
|
|
|
Scan(&value)
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
func Set(key, value string) error {
|
|
|
|
result := store.DB.Exec(`update "attributes" set value = ? where "key" = ?`,
|
|
|
|
value, key)
|
|
|
|
if result.RowsAffected == 0 {
|
|
|
|
return ErrorAttributeNotFound
|
|
|
|
}
|
|
|
|
return result.Error
|
|
|
|
}
|
2022-01-09 08:31:12 +00:00
|
|
|
|
|
|
|
var load []*func()
|
|
|
|
var loadLock sync.Mutex
|
|
|
|
|
|
|
|
func AddSoftLoad(f *func()) {
|
|
|
|
loadLock.Lock()
|
|
|
|
load = append(load, f)
|
|
|
|
loadLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Load() {
|
|
|
|
for _, f := range load {
|
|
|
|
if f != nil {
|
|
|
|
(*f)()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|