swirl/dao/mongo/event.go
2022-01-04 16:44:03 +08:00

36 lines
899 B
Go

package mongo
import (
"context"
"time"
"github.com/cuigh/swirl/dao"
"go.mongodb.org/mongo-driver/bson"
)
const Event = "event"
func (d *Dao) EventSearch(ctx context.Context, args *dao.EventSearchArgs) (events []*dao.Event, count int, err error) {
filter := bson.M{}
if args.Type != "" {
filter["type"] = args.Type
}
if args.Name != "" {
filter["args.name"] = args.Name
}
opts := searchOptions{filter: filter, sorter: bson.M{"_id": -1}, pageIndex: args.PageIndex, pageSize: args.PageSize}
events = []*dao.Event{}
count, err = d.search(ctx, Event, opts, &events)
return
}
func (d *Dao) EventCreate(ctx context.Context, event *dao.Event) (err error) {
return d.create(ctx, Event, event)
}
func (d *Dao) EventPrune(ctx context.Context, end time.Time) (err error) {
filter := bson.M{"time": bson.M{"$lt": end}}
_, err = d.db.Collection(Event).DeleteMany(ctx, filter)
return
}