2021-12-06 12:24:22 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
// SettingHandler encapsulates setting related handlers.
|
|
|
|
type SettingHandler struct {
|
|
|
|
Load web.HandlerFunc `path:"/load" auth:"setting.view" desc:"load setting"`
|
|
|
|
Save web.HandlerFunc `path:"/save" method:"post" auth:"setting.edit" desc:"save setting"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSetting creates an instance of SettingHandler
|
|
|
|
func NewSetting(b biz.SettingBiz) *SettingHandler {
|
|
|
|
return &SettingHandler{
|
2021-12-23 10:00:14 +00:00
|
|
|
Load: settingLoad(b),
|
2021-12-06 12:24:22 +00:00
|
|
|
Save: settingSave(b),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func settingLoad(b biz.SettingBiz) web.HandlerFunc {
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) error {
|
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
options, err := b.Load(ctx)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return success(c, options)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func settingSave(b biz.SettingBiz) web.HandlerFunc {
|
|
|
|
type Args struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Options json.RawMessage `json:"options"`
|
|
|
|
}
|
|
|
|
|
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
|
|
|
err = c.Bind(args)
|
2021-12-23 10:00:14 +00:00
|
|
|
if err == nil {
|
2022-01-06 08:54:14 +00:00
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
err = b.Save(ctx, args.ID, args.Options, 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
|
|
|
}
|
|
|
|
}
|