swirl/api/event.go

60 lines
1.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"
2021-12-06 12:24:22 +00:00
)
// EventHandler encapsulates event related handlers.
type EventHandler struct {
Search web.HandlerFunc `path:"/search" auth:"event.view" desc:"search events"`
Prune web.HandlerFunc `path:"/prune" method:"post" auth:"event.delete" desc:"delete events"`
}
// NewEvent creates an instance of EventHandler
func NewEvent(b biz.EventBiz) *EventHandler {
return &EventHandler{
Search: eventSearch(b),
Prune: eventPrune(b),
}
}
func eventSearch(b biz.EventBiz) web.HandlerFunc {
return func(ctx web.Context) (err error) {
var (
2021-12-23 11:28:31 +00:00
args = &dao.EventSearchArgs{}
events []*dao.Event
2021-12-06 12:24:22 +00:00
total int
)
if err = ctx.Bind(args); err == nil {
events, total, err = b.Search(args)
}
if err != nil {
return
}
return success(ctx, data.Map{
"items": events,
"total": total,
})
}
}
func eventPrune(b biz.EventBiz) web.HandlerFunc {
type Args struct {
2022-01-04 08:44:03 +00:00
Days int32 `json:"days"`
2021-12-06 12:24:22 +00:00
}
return func(ctx web.Context) (err error) {
var args = &Args{}
if err = ctx.Bind(args); err == nil {
2022-01-04 08:44:03 +00:00
err = b.Prune(args.Days)
2021-12-06 12:24:22 +00:00
}
return ajax(ctx, err)
}
}