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
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
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.EventSearchArgs{}
|
|
|
|
events []*dao.Event
|
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()
|
|
|
|
|
|
|
|
events, 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": 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
|
|
|
}
|
|
|
|
|
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{}
|
2022-01-06 08:54:14 +00:00
|
|
|
if err = c.Bind(args); err == nil {
|
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
err = b.Prune(ctx, args.Days)
|
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
|
|
|
}
|
|
|
|
}
|