diff --git a/attribute/service.go b/attribute/service.go new file mode 100644 index 0000000..42d3eb7 --- /dev/null +++ b/attribute/service.go @@ -0,0 +1,78 @@ +package attribute + +import ( + "github.com/gin-gonic/gin" + "kumoly.io/kumoly/app/auth" + "kumoly.io/kumoly/app/server" + "kumoly.io/kumoly/app/store" +) + +type Service struct{} + +func (srv Service) GetName() string { return "attribute.Service" } +func (srv Service) GetDependencies() []string { return []string{} } +func (srv Service) IsService() bool { return false } +func (srv Service) Main() error { return nil } +func (srv Service) Del() {} +func (srv Service) Health() error { return nil } + +func (srv Service) Init() error { + Init() + return nil +} + +func (srv Service) Load() error { + api := server.API.Group("attr", auth.ACAdmin()) + + api.GET("", func(c *gin.Context) { + key := c.Query("key") + if key == "" { + var attrs []Attribute + store.DB.Find(&attrs) + server.OK(c, attrs) + } else { + var attr Attribute + store.DB.Find(&attr, "key = ?", key) + server.OK(c, attr) + } + }) + + api.PUT("", func(c *gin.Context) { + var data struct { + Key string `json:"key" binding:"required"` + Value string `json:"value" binding:"required"` + } + if err := c.ShouldBindJSON(&data); err != nil { + panic(err) + } + if err := store.DB.Model(&Attribute{}).Where("key = ?", data.Key). + Update("value", data.Value).Error; err != nil { + panic(err) + } + server.OK(c, "ok") + }) + + api.POST("restore", func(c *gin.Context) { + var data struct { + Key string `json:"key" binding:"required"` + } + if err := c.ShouldBindJSON(&data); err != nil { + panic(err) + } + result := store.DB.Exec(`update "attributes" set value = "default" where "key" = ?`, data.Key) + if result.Error != nil { + panic(result.Error) + } + server.OK(c, result.RowsAffected) + }) + + api.POST("restore_all", func(c *gin.Context) { + result := store.DB.Exec(`update "attributes" set value = "default"`) + if result.Error != nil { + panic(result.Error) + } + server.OK(c, result.RowsAffected) + }) + + return nil +} diff --git a/helper.go b/helper.go index f58bb72..31ce611 100644 --- a/helper.go +++ b/helper.go @@ -20,13 +20,12 @@ func Default() *system.System { sys := system.New() server := server.New(viper.GetString("name")) - attribute.Init() - auth.Setup() auth.SetDB(store.DB) sys.Inject(auth.Injector(server.API)) sys.Append(server, auth.New(), + &attribute.Service{}, &task.Service{}, &history.Service{}, &email.Service{},