Initial commit: web_hghl_server
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package videolikemod
|
||||
|
||||
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 初始化 video_like 集合和常用查询索引
|
||||
func Init(ctx context.Context) {
|
||||
coll = mongos.Shop.Coll(TN.VideoLike)
|
||||
initIndex(ctx)
|
||||
}
|
||||
|
||||
func initIndex(ctx context.Context) {
|
||||
indexes := []mongo.IndexModel{
|
||||
{Keys: bson.D{{Key: FN.VideoLike.UserID, Value: 1}, {Key: FN.VideoLike.VideoID, Value: 1}}, Options: options.Index().SetUnique(true)},
|
||||
{Keys: bson.D{{Key: FN.VideoLike.VideoID, Value: 1}}},
|
||||
{Keys: bson.D{{Key: FN.VideoLike.CreatedAt, Value: -1}, {Key: FN.VideoLike.VideoID, Value: 1}}},
|
||||
{Keys: bson.D{{Key: FN.VideoLike.UserID, Value: 1}, {Key: FN.VideoLike.CreatedAt, Value: -1}, {Key: FN.VideoLike.Id, Value: -1}}},
|
||||
}
|
||||
if _, err := coll.CreateIndex(ctx, indexes); err != nil {
|
||||
panic(fmt.Sprintf("(videolikemod:initIndex) Create Index failed! err:%+v:", err))
|
||||
}
|
||||
}
|
||||
|
||||
// UpsertOneVideoLike 按用户ID和视频ID写入点赞关系,返回是否新增点赞
|
||||
func UpsertOneVideoLike(ctx context.Context, userId, videoId int64, now time.Time) (bool, *srvdef.SrvErr) {
|
||||
filter := bson.M{FN.VideoLike.UserID: userId, FN.VideoLike.VideoID: videoId}
|
||||
update := bson.M{
|
||||
"$set": bson.M{FN.VideoLike.UpdatedAt: now},
|
||||
"$setOnInsert": bson.M{
|
||||
FN.VideoLike.Id: mongos.Lid.VideoLike.Get(),
|
||||
FN.VideoLike.UserID: userId,
|
||||
FN.VideoLike.VideoID: videoId,
|
||||
FN.VideoLike.CreatedAt: now,
|
||||
},
|
||||
}
|
||||
result, err := coll.UpdateOne(ctx, filter, update, options.Update().SetUpsert(true))
|
||||
if err != nil {
|
||||
log.Warn("videolikemod:UpsertOneVideoLike db.UpdateOne failed", log.Any("filter", filter), log.E(err))
|
||||
return false, srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
return result.UpsertedCount > 0, nil
|
||||
}
|
||||
|
||||
// DeleteOneVideoLike 删除一条视频点赞关系,返回是否实际删除
|
||||
func DeleteOneVideoLike(ctx context.Context, filter bson.M) (bool, *srvdef.SrvErr) {
|
||||
result, err := coll.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("videolikemod:DeleteOneVideoLike db.DeleteOne failed", log.Any("filter", filter), log.E(err))
|
||||
return false, srvdef.New(srvdef.ErrDbDeleteError, "", "")
|
||||
}
|
||||
return result > 0, nil
|
||||
}
|
||||
|
||||
// CountVideoLike 统计视频点赞关系数量
|
||||
func CountVideoLike(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
|
||||
count, err := coll.Count(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("videolikemod:CountVideoLike db.Count failed", log.Any("filter", filter), log.E(err))
|
||||
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// AggregateVideoLike 聚合查询视频点赞关系
|
||||
func AggregateVideoLike(ctx context.Context, bind interface{}, pipeline []bson.M) *srvdef.SrvErr {
|
||||
if err := coll.Aggregate(ctx, bind, pipeline); err != nil {
|
||||
log.Warn("videolikemod:AggregateVideoLike db.Aggregate failed", log.Any("pipeline", pipeline), log.E(err))
|
||||
return srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user