master
Evan Chen 2022-01-09 05:26:56 +08:00
parent 810a59b3fe
commit e7c12c603b
2 changed files with 79 additions and 2 deletions

78
attribute/service.go Normal file
View File

@ -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
}

View File

@ -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{},