2022-01-08 21:26:56 +00:00
|
|
|
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) 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
|
2022-01-09 09:15:23 +00:00
|
|
|
store.DB.Order("key").Find(&attrs)
|
2022-01-08 21:26:56 +00:00
|
|
|
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)
|
|
|
|
}
|
2022-01-09 08:31:12 +00:00
|
|
|
if err := RestoreDefault(data.Key); err != nil {
|
|
|
|
panic(err)
|
2022-01-08 21:26:56 +00:00
|
|
|
}
|
2022-01-09 08:31:12 +00:00
|
|
|
server.OK(c, "ok")
|
2022-01-08 21:26:56 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2022-01-09 08:31:12 +00:00
|
|
|
api.POST("reload", func(c *gin.Context) {
|
|
|
|
Load()
|
|
|
|
server.OK(c, "ok")
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv Service) Main() error {
|
|
|
|
Load()
|
2022-01-08 21:26:56 +00:00
|
|
|
return nil
|
|
|
|
}
|