swirl/api/config.go

108 lines
2.5 KiB
Go
Raw Normal View History

2021-12-06 12:24:22 +00:00
package api
import (
"github.com/cuigh/auxo/data"
"github.com/cuigh/auxo/net/web"
"github.com/cuigh/swirl/biz"
2022-01-06 08:54:14 +00:00
"github.com/cuigh/swirl/misc"
2021-12-06 12:24:22 +00:00
)
// ConfigHandler encapsulates config related handlers.
type ConfigHandler struct {
Search web.HandlerFunc `path:"/search" auth:"config.view" desc:"search configs"`
Find web.HandlerFunc `path:"/find" auth:"config.view" desc:"find config by name"`
Delete web.HandlerFunc `path:"/delete" method:"post" auth:"config.delete" desc:"delete config"`
Save web.HandlerFunc `path:"/save" method:"post" auth:"config.edit" desc:"create or update config"`
}
// NewConfig creates an instance of ConfigHandler
func NewConfig(b biz.ConfigBiz) *ConfigHandler {
return &ConfigHandler{
Search: configSearch(b),
Find: configFind(b),
Delete: configDelete(b),
Save: configSave(b),
}
}
func configSearch(b biz.ConfigBiz) web.HandlerFunc {
type Args struct {
Name string `json:"name" bind:"name"`
PageIndex int `json:"pageIndex" bind:"pageIndex"`
PageSize int `json:"pageSize" bind:"pageSize"`
}
2022-01-06 08:54:14 +00:00
return func(c web.Context) (err error) {
2021-12-06 12:24:22 +00:00
var (
args = &Args{}
configs []*biz.Config
total int
)
2022-01-06 08:54:14 +00:00
if err = c.Bind(args); err == nil {
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
configs, total, err = b.Search(ctx, args.Name, args.PageIndex, args.PageSize)
2021-12-06 12:24:22 +00:00
}
if err != nil {
return
}
2022-01-06 08:54:14 +00:00
return success(c, data.Map{
2021-12-06 12:24:22 +00:00
"items": configs,
"total": total,
})
}
}
func configFind(b biz.ConfigBiz) web.HandlerFunc {
2022-01-06 08:54:14 +00:00
return func(c web.Context) error {
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
id := c.Query("id")
config, raw, err := b.Find(ctx, id)
2021-12-06 12:24:22 +00:00
if err != nil {
return err
}
2022-01-06 08:54:14 +00:00
return success(c, data.Map{"config": config, "raw": raw})
2021-12-06 12:24:22 +00:00
}
}
func configDelete(b biz.ConfigBiz) web.HandlerFunc {
type Args struct {
ID string `json:"id"`
Name string `json:"name"`
}
2022-01-06 08:54:14 +00:00
return func(c web.Context) (err error) {
2021-12-06 12:24:22 +00:00
args := &Args{}
2022-01-06 08:54:14 +00:00
if err = c.Bind(args); err == nil {
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
err = b.Delete(ctx, args.ID, args.Name, c.User())
2021-12-06 12:24:22 +00:00
}
2022-01-06 08:54:14 +00:00
return ajax(c, err)
2021-12-06 12:24:22 +00:00
}
}
func configSave(b biz.ConfigBiz) web.HandlerFunc {
2022-01-06 08:54:14 +00:00
return func(c web.Context) error {
config := &biz.Config{}
err := c.Bind(config, true)
2021-12-06 12:24:22 +00:00
if err == nil {
2022-01-06 08:54:14 +00:00
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
if config.ID == "" {
err = b.Create(ctx, config, c.User())
2021-12-06 12:24:22 +00:00
} else {
2022-01-06 08:54:14 +00:00
err = b.Update(ctx, config, c.User())
2021-12-06 12:24:22 +00:00
}
}
2022-01-06 08:54:14 +00:00
return ajax(c, err)
2021-12-06 12:24:22 +00:00
}
}