init: 初始化 web_hgdjb_server 项目
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package commentlikemod
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
"videoweb/pub/common/log"
|
||||
"videoweb/pub/common/srvdef"
|
||||
"videoweb/pub/mongos"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
var coll *mongos.Collection
|
||||
|
||||
// Init 初始化 comment_like 集合和常用查询索引
|
||||
func Init(ctx context.Context) {
|
||||
coll = mongos.Shop.Coll(TN.CommentLike)
|
||||
initIndex(ctx)
|
||||
}
|
||||
|
||||
func initIndex(ctx context.Context) {
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.CommentLike.UserId, Value: 1},
|
||||
{Key: FN.CommentLike.CommentId, Value: 1},
|
||||
},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.CommentLike.CommentId, Value: 1},
|
||||
{Key: FN.CommentLike.CreatedAt, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.CommentLike.ObjectType, Value: 1},
|
||||
{Key: FN.CommentLike.ObjectId, Value: 1},
|
||||
{Key: FN.CommentLike.CreatedAt, Value: -1},
|
||||
},
|
||||
},
|
||||
}
|
||||
if _, err := coll.CreateIndex(ctx, indexes); err != nil {
|
||||
panic(fmt.Sprintf("(commentlikemod:initIndex) 创建索引失败 err:%+v:", err))
|
||||
}
|
||||
}
|
||||
|
||||
// InsertOneCommentLike 插入一条评论点赞关系
|
||||
func InsertOneCommentLike(ctx context.Context, doc *CommentLike) (bool, *srvdef.SrvErr) {
|
||||
if doc.Id == 0 {
|
||||
doc.Id = mongos.Lid.CommentLike.Get()
|
||||
}
|
||||
now := time.Now()
|
||||
if doc.CreatedAt.IsZero() {
|
||||
doc.CreatedAt = now
|
||||
}
|
||||
doc.UpdatedAt = now
|
||||
if _, err := coll.InsertOne(ctx, doc); err != nil {
|
||||
if mongo.IsDuplicateKeyError(err) {
|
||||
return false, nil
|
||||
}
|
||||
log.Warn("插入评论点赞关系失败", log.Any("doc", doc), log.E(err))
|
||||
return false, srvdef.New(srvdef.ErrDbInsertError, "", "")
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// DeleteOneCommentLike 删除一条评论点赞关系
|
||||
func DeleteOneCommentLike(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
|
||||
count, err := coll.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("删除评论点赞关系失败", log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbDeleteError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// FindManyCommentLike 按条件查询评论点赞关系
|
||||
func FindManyCommentLike(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (CommentLikeList, *srvdef.SrvErr) {
|
||||
data := make(CommentLikeList, 0)
|
||||
if err := coll.Find(ctx, &data, filter, opts...); err != nil {
|
||||
log.Warn("查询评论点赞关系列表失败", log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package commentlikemod
|
||||
|
||||
// TagCommentLike 保存 comment_like 集合字段名
|
||||
type TagCommentLike struct {
|
||||
Id string
|
||||
UserId string
|
||||
CommentId string
|
||||
ObjectId string
|
||||
ObjectType string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
var FN struct {
|
||||
CommentLike TagCommentLike
|
||||
}
|
||||
|
||||
var TN struct {
|
||||
CommentLike string
|
||||
}
|
||||
|
||||
func init() {
|
||||
FN.CommentLike.Id = "_id"
|
||||
FN.CommentLike.UserId = "userId"
|
||||
FN.CommentLike.CommentId = "commentId"
|
||||
FN.CommentLike.ObjectId = "objectId"
|
||||
FN.CommentLike.ObjectType = "objectType"
|
||||
FN.CommentLike.CreatedAt = "createdAt"
|
||||
FN.CommentLike.UpdatedAt = "updatedAt"
|
||||
|
||||
TN.CommentLike = "comment_like"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package commentlikemod
|
||||
|
||||
import "time"
|
||||
|
||||
// CommentLike @table: comment_like
|
||||
//
|
||||
// CommentLike 评论点赞关系模型
|
||||
type CommentLike struct {
|
||||
Id int64 `json:"id" bson:"_id,omitempty"` // 点赞记录ID
|
||||
UserId int64 `json:"userId" bson:"userId"` // 用户ID
|
||||
CommentId int64 `json:"commentId" bson:"commentId"` // 评论ID
|
||||
ObjectId int64 `json:"objectId" bson:"objectId"` // 评论对象ID
|
||||
ObjectType int64 `json:"objectType" bson:"objectType"` // 评论对象类型 10-短剧 11-短剧剧集 20-视频
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` // 修改时间
|
||||
}
|
||||
|
||||
// CommentLikeList 评论点赞关系列表
|
||||
type CommentLikeList []*CommentLike
|
||||
Reference in New Issue
Block a user