swirl/dao/mongo/event.go

36 lines
899 B
Go
Raw Permalink Normal View History

2017-09-26 12:50:09 +00:00
package mongo
import (
2021-12-06 12:24:22 +00:00
"context"
2022-01-04 08:44:03 +00:00
"time"
2021-12-06 12:24:22 +00:00
2021-12-23 11:28:31 +00:00
"github.com/cuigh/swirl/dao"
2021-12-06 12:24:22 +00:00
"go.mongodb.org/mongo-driver/bson"
2017-09-26 12:50:09 +00:00
)
2021-12-06 12:24:22 +00:00
const Event = "event"
2017-09-26 12:50:09 +00:00
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-06 12:24:22 +00:00
filter := bson.M{}
if args.Type != "" {
filter["type"] = args.Type
}
if args.Name != "" {
2021-12-24 07:04:15 +00:00
filter["args.name"] = args.Name
2021-12-06 12:24:22 +00:00
}
opts := searchOptions{filter: filter, sorter: bson.M{"_id": -1}, pageIndex: args.PageIndex, pageSize: args.PageSize}
2021-12-23 11:28:31 +00:00
events = []*dao.Event{}
2021-12-06 12:24:22 +00:00
count, err = d.search(ctx, Event, opts, &events)
2017-09-26 12:50:09 +00:00
return
}
2021-12-23 11:28:31 +00:00
func (d *Dao) EventCreate(ctx context.Context, event *dao.Event) (err error) {
2021-12-06 12:24:22 +00:00
return d.create(ctx, Event, event)
2017-09-26 12:50:09 +00:00
}
2022-01-04 08:44:03 +00:00
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
}