swirl/dao/bolt/event.go

50 lines
1.1 KiB
Go
Raw Normal View History

2018-04-27 13:17:00 +00:00
package bolt
import (
2021-12-06 12:24:22 +00:00
"context"
2018-04-27 13:17:00 +00:00
"sort"
2021-12-16 12:23:08 +00:00
"time"
2018-04-27 13:17:00 +00:00
2021-12-24 07:04:15 +00:00
"github.com/cuigh/auxo/util/cast"
2021-12-23 11:28:31 +00:00
"github.com/cuigh/swirl/dao"
2018-04-27 13:17:00 +00:00
"github.com/cuigh/swirl/misc"
)
2021-12-16 08:11:16 +00:00
const Event = "event"
2021-12-23 11:28:31 +00:00
func (d *Dao) EventSearch(ctx context.Context, args *dao.EventSearchArgs) (events []*dao.Event, count int, err error) {
2021-12-16 08:11:16 +00:00
err = d.each(Event, func(v []byte) error {
2021-12-23 11:28:31 +00:00
event := &dao.Event{}
2021-12-16 08:11:16 +00:00
err = decode(v, event)
2018-04-27 13:17:00 +00:00
if err != nil {
return err
}
match := true
if args.Name != "" {
2021-12-24 07:04:15 +00:00
match = event.Args != nil && matchAny(args.Name, cast.ToString(event.Args["name"]))
2018-04-27 13:17:00 +00:00
}
if match && args.Type != "" {
2021-12-16 08:11:16 +00:00
match = event.Type == args.Type
2018-04-27 13:17:00 +00:00
}
if match {
events = append(events, event)
}
return nil
})
if err == nil {
count = len(events)
sort.Slice(events, func(i, j int) bool {
2021-12-16 12:23:08 +00:00
return time.Time(events[i].Time).After(time.Time(events[j].Time))
2018-04-27 13:17:00 +00:00
})
start, end := misc.Page(count, args.PageIndex, args.PageSize)
events = events[start:end]
}
return
}
2021-12-23 11:28:31 +00:00
func (d *Dao) EventCreate(ctx context.Context, event *dao.Event) (err error) {
2021-12-16 08:11:16 +00:00
return d.replace(Event, event.ID.Hex(), event)
2018-04-27 13:17:00 +00:00
}