@@ -0,0 +1,55 @@
|
||||
package actmod
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func GetActivitiesCount() (int64, error) {
|
||||
return coll(nil).Count(bson.M{})
|
||||
}
|
||||
|
||||
func GetActivities(skip, limit uint64) ([]Activity, error) {
|
||||
opts := options.Find().SetSort(bson.D{{Key: "sort", Value: 1}, {Key: "createdAt", Value: -1}})
|
||||
if skip != 0 {
|
||||
opts.SetSkip(int64(skip))
|
||||
}
|
||||
if limit != 0 {
|
||||
opts.SetLimit(int64(limit))
|
||||
}
|
||||
var activities []Activity
|
||||
if err := coll(nil).Find(&activities, bson.M{}, opts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return activities, nil
|
||||
}
|
||||
|
||||
func GetActivitiesValid(skip, limit uint64) ([]Activity, error) {
|
||||
opts := options.Find().SetSort(bson.D{{Key: "sort", Value: 1}, {Key: "createdAt", Value: -1}})
|
||||
if skip != 0 {
|
||||
opts.SetSkip(int64(skip))
|
||||
}
|
||||
if limit != 0 {
|
||||
opts.SetLimit(int64(limit))
|
||||
}
|
||||
var activities []Activity
|
||||
if err := coll(nil).Find(&activities, bson.M{"status": 1, "expiredIn": bson.M{"$gt": time.Now()}}, opts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return activities, nil
|
||||
}
|
||||
|
||||
func GetActivityByActivityID(ActivityID primitive.ObjectID) (*Activity, error) {
|
||||
var act Activity
|
||||
if err := coll(nil).FindOne(&act, bson.M{"_id": ActivityID}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if act.ID.IsZero() {
|
||||
return nil, errors.New("active not found")
|
||||
}
|
||||
return &act, nil
|
||||
}
|
||||
Reference in New Issue
Block a user