app/attribute/service.go

87 lines
1.9 KiB
Go

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
store.DB.Order("key").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)
}
if err := RestoreDefault(data.Key); err != nil {
panic(err)
}
server.OK(c, "ok")
})
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)
})
api.POST("reload", func(c *gin.Context) {
Load()
server.OK(c, "ok")
})
return nil
}
func (srv Service) Main() error {
Load()
return nil
}