@@ -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
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package actmod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/models"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const table = models.Act
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
// initIndex 初始化索引
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "activityName", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
|
||||
type Activity struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` // id
|
||||
ActivityName string `json:"activityName" bson:"activityName"` // 活动名
|
||||
Desc string `json:"desc" bson:"desc"` // 活动说明
|
||||
Img string `json:"img" bson:"img"` // 活动图片
|
||||
Content string `json:"content" bson:"content"` // 富文本内容
|
||||
Link string `json:"link" bson:"link"` // 链接地址
|
||||
Sort int `json:"sort" bson:"sort"` // 活动排序
|
||||
Status int `json:"status" bson:"status"` // 状态 1 有效 0 无效
|
||||
ExpiredIn time.Time `json:"expiredIn" bson:"expiredIn"` // 过期时间
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 文档创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` // 文档更新时间
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package actmod
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
func AddACtivity(act Activity) error {
|
||||
_, err := coll(nil).InsertOne(act)
|
||||
return err
|
||||
}
|
||||
|
||||
func EidtActivity(actID primitive.ObjectID, b bson.M) error {
|
||||
_, err := coll(nil).UpdateOne(bson.M{"_id": actID}, bson.M{"$set": b})
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user