64 lines
1.8 KiB
Go
64 lines
1.8 KiB
Go
package commentser
|
|
|
|
import (
|
|
"context"
|
|
"videoweb/app/api/proto"
|
|
"videoweb/pub/common/enum/redisconst"
|
|
"videoweb/pub/common/log"
|
|
"videoweb/pub/common/srvdef"
|
|
"videoweb/pub/models/commentlikemod"
|
|
"videoweb/pub/models/commentmod"
|
|
"videoweb/pub/redis"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
)
|
|
|
|
// SetCommentLike 设置评论点赞状态
|
|
func SetCommentLike(ctx context.Context, userId int64, req *proto.CommentLikeSetReq) (*proto.CommentLikeSetResp, *srvdef.SrvErr) {
|
|
if userId <= 0 || req == nil || req.CommentId <= 0 || req.IsLike == nil {
|
|
return nil, srvdef.New(srvdef.ErrParamError, "", "")
|
|
}
|
|
comment, err := commentmod.FindOneComment(ctx, bson.M{
|
|
commentmod.FN.Comment.Id: req.CommentId,
|
|
commentmod.FN.Comment.ReviewStatus: 1,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var changed bool
|
|
if *req.IsLike {
|
|
changed, err = commentlikemod.Add(ctx, userId, req.CommentId)
|
|
} else {
|
|
changed, err = commentlikemod.Remove(ctx, userId, req.CommentId)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
count := comment.CountLike
|
|
if changed {
|
|
delta := int64(1)
|
|
if !*req.IsLike {
|
|
delta = -1
|
|
}
|
|
count, err = commentmod.ChangeLikeCount(ctx, req.CommentId, delta)
|
|
if err != nil {
|
|
if *req.IsLike {
|
|
_, _ = commentlikemod.Remove(ctx, userId, req.CommentId)
|
|
} else {
|
|
_, _ = commentlikemod.Add(ctx, userId, req.CommentId)
|
|
}
|
|
return nil, err
|
|
}
|
|
} else {
|
|
current, findErr := commentmod.FindOneComment(ctx, bson.M{commentmod.FN.Comment.Id: req.CommentId})
|
|
if findErr != nil {
|
|
return nil, findErr
|
|
}
|
|
count = current.CountLike
|
|
}
|
|
if cacheErr := redis.Del(redisconst.CommentListCacheKey(comment.ObjectId, comment.ObjectType)); cacheErr != nil {
|
|
log.Warn("SetCommentLike clear cache", log.E(cacheErr))
|
|
}
|
|
return &proto.CommentLikeSetResp{CommentId: req.CommentId, IsLike: *req.IsLike, CountLike: count}, nil
|
|
}
|