swirl/api/chart.go

103 lines
2.2 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"
2021-12-23 11:28:31 +00:00
"github.com/cuigh/swirl/dao"
2022-01-06 08:54:14 +00:00
"github.com/cuigh/swirl/misc"
2021-12-06 12:24:22 +00:00
)
// ChartHandler encapsulates chart related handlers.
type ChartHandler struct {
Search web.HandlerFunc `path:"/search" auth:"chart.view" desc:"search charts"`
Find web.HandlerFunc `path:"/find" auth:"chart.view" desc:"find chart by id"`
Save web.HandlerFunc `path:"/save" method:"post" auth:"chart.edit" desc:"create or update chart"`
Delete web.HandlerFunc `path:"/delete" method:"post" auth:"chart.delete" desc:"delete chart"`
2021-12-06 12:24:22 +00:00
}
// NewChart creates an instance of ChartHandler
func NewChart(b biz.ChartBiz) *ChartHandler {
return &ChartHandler{
Search: chartSearch(b),
Find: chartFind(b),
Delete: chartDelete(b),
Save: chartSave(b),
2021-12-06 12:24:22 +00:00
}
}
func chartSearch(b biz.ChartBiz) web.HandlerFunc {
2022-01-06 08:54:14 +00:00
return func(c web.Context) (err error) {
2021-12-06 12:24:22 +00:00
var (
2021-12-23 11:28:31 +00:00
args = &dao.ChartSearchArgs{}
charts []*dao.Chart
2021-12-06 12:24:22 +00:00
total int
)
2022-01-06 08:54:14 +00:00
if err = c.Bind(args); err == nil {
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
charts, total, err = b.Search(ctx, args)
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": charts,
"total": total,
})
}
}
func chartFind(b biz.ChartBiz) 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")
chart, 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, chart)
2021-12-06 12:24:22 +00:00
}
}
func chartDelete(b biz.ChartBiz) web.HandlerFunc {
type Args struct {
ID string `json:"id"`
Title string `json:"title"`
}
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.Title, 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 chartSave(b biz.ChartBiz) web.HandlerFunc {
2022-01-06 08:54:14 +00:00
return func(c web.Context) error {
2021-12-23 11:28:31 +00:00
r := &dao.Chart{}
2022-01-06 08:54:14 +00:00
err := c.Bind(r, 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()
2021-12-06 12:24:22 +00:00
if r.ID == "" {
2022-01-06 08:54:14 +00:00
err = b.Create(ctx, r, c.User())
2021-12-06 12:24:22 +00:00
} else {
2022-01-06 08:54:14 +00:00
err = b.Update(ctx, r, 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
}
}