@@ -0,0 +1,27 @@
|
||||
package userwatchrecordmod
|
||||
|
||||
import (
|
||||
"91porn-server/models/commod"
|
||||
"91porn-server/models/v/vidmod"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
type ListRequest struct {
|
||||
commod.Page
|
||||
}
|
||||
|
||||
func (receiver *ListRequest) Filter(uid uint64) primitive.M {
|
||||
return bson.M{"uid": uid, "type": bson.M{"$ne": "drama"}}
|
||||
}
|
||||
|
||||
func (receiver *ListRequest) Options() *options.FindOptions {
|
||||
return options.Find().SetSkip(int64((receiver.PageNumber - 1) * receiver.PageSize)).SetLimit(int64(receiver.PageSize + 1)).SetSort(bson.M{"recordTime": -1})
|
||||
}
|
||||
|
||||
type ListResponse struct {
|
||||
WorkList []*vidmod.VideoInfoResp `json:"workList"` // 作品列表
|
||||
HasNext bool `json:"hasNext"` // 是否有下一页
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package userwatchrecordmod
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
func TestListRequestFilterExcludesDramaHistory(t *testing.T) {
|
||||
filter := (&ListRequest{}).Filter(42)
|
||||
if filter["uid"] != uint64(42) {
|
||||
t.Fatalf("uid filter = %v, want 42", filter["uid"])
|
||||
}
|
||||
typeFilter, ok := filter["type"].(bson.M)
|
||||
if !ok || typeFilter["$ne"] != "drama" {
|
||||
t.Fatalf("type filter = %#v, want drama exclusion", filter["type"])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package userwatchrecordmod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type UserWatchRecord struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
|
||||
UID uint64 `json:"uid" bson:"uid"` // 用户ID
|
||||
VideoID primitive.ObjectID `json:"videoId" bson:"videoId"` // 视频ID
|
||||
RecordTime time.Time `json:"recordTime" bson:"recordTime"` // 记录时间
|
||||
CreateTime time.Time `json:"createTime" bson:"createTime"` // 创建时间
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package userwatchrecordmod
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"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.UserWatchRecord
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
// Index 索引设置
|
||||
func Index() {
|
||||
mdb = db.Init(table)
|
||||
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
||||
{
|
||||
Keys: bson.D{{Key: "uid", Value: 1}, {Key: "recordTime", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "videoId", Value: 1}, {Key: "uid", Value: 1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("UserWatchRecord model set index err ==>[%+v]", err))
|
||||
}
|
||||
}
|
||||
|
||||
func Insert(uid uint64, videoId primitive.ObjectID) error {
|
||||
now := time.Now()
|
||||
result, err := coll(nil).InsertOne(&UserWatchRecord{
|
||||
UID: uid,
|
||||
VideoID: videoId,
|
||||
RecordTime: now,
|
||||
CreateTime: now,
|
||||
})
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "InsertOne", err),
|
||||
log.Any("videoId", videoId),
|
||||
)
|
||||
return err
|
||||
}
|
||||
if result.InsertedID == nil {
|
||||
return errors.New("UserWatchRecord Insert is null")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Refresh(uid uint64, videoId primitive.ObjectID) error {
|
||||
result, err := coll(nil).UpdateOne(bson.M{"uid": uid, "videoId": videoId}, bson.M{"$set": bson.M{"recordTime": time.Now()}})
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Refresh", table, "UpdateOne", err),
|
||||
log.Any("uid", uid),
|
||||
log.Any("videoId", videoId),
|
||||
)
|
||||
return err
|
||||
}
|
||||
if result.ModifiedCount == 0 {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Refresh", table, "UpdateOne", "refresh Fail"),
|
||||
log.Any("uid", uid),
|
||||
log.Any("videoId", videoId),
|
||||
)
|
||||
return errors.New("refresh Fail")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func IsExist(uid uint64, videoId primitive.ObjectID) (bool, error) {
|
||||
count, err := coll(nil).Count(bson.M{"uid": uid, "videoId": videoId})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return count != 0, nil
|
||||
}
|
||||
|
||||
func List(filter primitive.M, opts ...*options.FindOptions) (out []*UserWatchRecord, err error) {
|
||||
if err = coll(nil).Find(&out, filter, opts...); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "List", table, "InsertOne", err),
|
||||
log.Any("filter", filter),
|
||||
log.Any("opts", opts),
|
||||
)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user