From 3462fd0101fd30ff36160dc51979431a04409c34 Mon Sep 17 00:00:00 2001 From: XFFFCCCC Date: Fri, 25 Aug 2023 02:28:05 -0700 Subject: [PATCH 1/2] fix favorite cancel --- src/constant/strings/err.go | 32 ++++++------ src/services/favorite/handler.go | 84 ++++++++++++++++++++------------ 2 files changed, 71 insertions(+), 45 deletions(-) diff --git a/src/constant/strings/err.go b/src/constant/strings/err.go index 189ed93..8a02ccd 100644 --- a/src/constant/strings/err.go +++ b/src/constant/strings/err.go @@ -56,18 +56,22 @@ const ( // Expected Error const ( - AuthUserExistedCode = 10001 - AuthUserExisted = "用户已存在,请更换用户名或尝试登录!" - UserNotExistedCode = 10002 - UserNotExisted = "用户不存在,请先注册或检查你的用户名是否正确!" - AuthUserLoginFailedCode = 10003 - AuthUserLoginFailed = "用户信息错误,请检查账号密码是否正确" - AuthUserNeededCode = 10004 - AuthUserNeeded = "用户无权限操作,请登陆后重试!" - ActionCommentTypeInvalidCode = 10005 - ActionCommentTypeInvalid = "不合法的评论类型" - ActionCommentLimitedCode = 10006 - ActionCommentLimited = "评论频繁,请稍后再试!" - InvalidContentTypeCode = 10007 - InvalidContentType = "不合法的内容类型" + AuthUserExistedCode = 10001 + AuthUserExisted = "用户已存在,请更换用户名或尝试登录!" + UserNotExistedCode = 10002 + UserNotExisted = "用户不存在,请先注册或检查你的用户名是否正确!" + AuthUserLoginFailedCode = 10003 + AuthUserLoginFailed = "用户信息错误,请检查账号密码是否正确" + AuthUserNeededCode = 10004 + AuthUserNeeded = "用户无权限操作,请登陆后重试!" + ActionCommentTypeInvalidCode = 10005 + ActionCommentTypeInvalid = "不合法的评论类型" + ActionCommentLimitedCode = 10006 + ActionCommentLimited = "评论频繁,请稍后再试!" + InvalidContentTypeCode = 10007 + InvalidContentType = "不合法的内容类型" + FavorivateServiceDuplicateCode = 10008 + FavorivateServiceDuplicateError = "不能重复点赞" + FavorivateServiceCancelCode = 10009 + FavorivateServiceCancelError = "没有点赞,不能取消点赞" ) diff --git a/src/services/favorite/handler.go b/src/services/favorite/handler.go index 14e7c61..f6015cd 100644 --- a/src/services/favorite/handler.go +++ b/src/services/favorite/handler.go @@ -69,19 +69,7 @@ func (c FavoriteServiceServerImpl) FavoriteAction(ctx context.Context, req *favo videoId := fmt.Sprintf("%d", req.VideoId) value, err := redis2.Client.ZScore(ctx, userId, videoId).Result() //判断是否重复点赞 - if value > 0 { - resp = &favorite.FavoriteResponse{ - StatusCode: strings.FavorivateServiceErrorCode, - StatusMsg: strings.FavorivateServiceError, - } - logger.WithFields(logrus.Fields{ - "response": resp, - }).Debugf("Process done.") - - return - } - - if err != redis.Nil { + if err != redis.Nil && err != nil { logger.WithFields(logrus.Fields{ "ActorId": req.ActorId, "video_id": req.VideoId, @@ -92,26 +80,59 @@ func (c FavoriteServiceServerImpl) FavoriteAction(ctx context.Context, req *favo return } - _, err = redis2.Client.TxPipelined(ctx, func(pipe redis.Pipeliner) error { - var val int64 - if req.ActionType == 1 { - val = 1 - } else { - val = -1 + if req.ActionType == 1 { + //重复点赞 + if value > 0 { + resp = &favorite.FavoriteResponse{ + StatusCode: strings.FavorivateServiceDuplicateCode, + StatusMsg: strings.FavorivateServiceDuplicateError, + } + logger.WithFields(logrus.Fields{ + "ActorId": req.ActorId, + "video_id": req.VideoId, + }).Info("user duplicate like") + logging.SetSpanError(span, err) + return + } else { //正常点赞 + _, err = redis2.Client.TxPipelined(ctx, func(pipe redis.Pipeliner) error { + videoId := fmt.Sprintf("%svideo_like_%d", config.EnvCfg.RedisPrefix, req.VideoId) // 该视频的点赞数量 + user_like_Id := fmt.Sprintf("%suser_like_%d", config.EnvCfg.RedisPrefix, req.ActorId) // 用户的点赞 + user_liked_id := fmt.Sprintf("%suser_liked_%d", config.EnvCfg.RedisPrefix, user_liked) // 被赞用户的获赞数量 + pipe.IncrBy(ctx, videoId, 1) + pipe.IncrBy(ctx, user_liked_id, 1) + pipe.ZAdd(ctx, user_like_Id, redis.Z{Score: float64(time.Now().Unix()), Member: req.VideoId}) + return nil + }) } - videoId := fmt.Sprintf("%svideo_like_%d", config.EnvCfg.RedisPrefix, req.VideoId) // 该视频的点赞数量 - user_like_Id := fmt.Sprintf("%suser_like_%d", config.EnvCfg.RedisPrefix, req.ActorId) // 用户的点赞 - user_liked_id := fmt.Sprintf("%suser_liked_%d", config.EnvCfg.RedisPrefix, user_liked) // 被赞用户的获赞数量 - pipe.IncrBy(ctx, videoId, val) - pipe.IncrBy(ctx, user_liked_id, val) - if req.ActionType == 2 { - pipe.ZRem(ctx, user_like_Id, req.VideoId) - } else { - pipe.ZAdd(ctx, user_like_Id, redis.Z{Score: float64(time.Now().Unix()), Member: req.VideoId}) + } else { + //没有的点过赞 + if value == 0 { + resp = &favorite.FavoriteResponse{ + StatusCode: strings.FavorivateServiceCancelCode, + StatusMsg: strings.FavorivateServiceCancelError, + } + + logger.WithFields(logrus.Fields{ + "ActorId": req.ActorId, + "video_id": req.VideoId, + }).Info("User did not like, cancel liking") + return + } else { //正常取消点赞 + _, err = redis2.Client.TxPipelined(ctx, func(pipe redis.Pipeliner) error { + videoId := fmt.Sprintf("%svideo_like_%d", config.EnvCfg.RedisPrefix, req.VideoId) // 该视频的点赞数量 + user_like_Id := fmt.Sprintf("%suser_like_%d", config.EnvCfg.RedisPrefix, req.ActorId) // 用户的点赞 + user_liked_id := fmt.Sprintf("%suser_liked_%d", config.EnvCfg.RedisPrefix, user_liked) // 被赞用户的获赞数量 + pipe.IncrBy(ctx, videoId, -1) + pipe.IncrBy(ctx, user_liked_id, -1) + _, err := pipe.ZRem(ctx, user_like_Id, req.VideoId).Result() + if err == redis.Nil { + err = nil + } + return nil + }) } - return nil - }) + } if err != nil { logger.WithFields(logrus.Fields{ "ActorId": req.ActorId, @@ -157,6 +178,7 @@ func (c FavoriteServiceServerImpl) FavoriteList(ctx context.Context, req *favori logger.WithFields(logrus.Fields{ "err": err, "ActorId": req.ActorId, + "user_id": req.UserId, }).Errorf("User service error") logging.SetSpanError(span, err) @@ -256,7 +278,7 @@ func (c FavoriteServiceServerImpl) IsFavorite(ctx context.Context, req *favorite ok, err := redis2.Client.ZScore(ctx, userId, videoId).Result() if err == redis.Nil { - + err = nil } else if err != nil { logger.WithFields(logrus.Fields{ "ActorId": req.ActorId, From a01459c43510842b1f44af42846d6117bf68dc1e Mon Sep 17 00:00:00 2001 From: XFFFCCCC Date: Fri, 25 Aug 2023 02:50:00 -0700 Subject: [PATCH 2/2] fix handle err --- src/services/favorite/handler.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/services/favorite/handler.go b/src/services/favorite/handler.go index f6015cd..ff0c686 100644 --- a/src/services/favorite/handler.go +++ b/src/services/favorite/handler.go @@ -124,12 +124,13 @@ func (c FavoriteServiceServerImpl) FavoriteAction(ctx context.Context, req *favo user_liked_id := fmt.Sprintf("%suser_liked_%d", config.EnvCfg.RedisPrefix, user_liked) // 被赞用户的获赞数量 pipe.IncrBy(ctx, videoId, -1) pipe.IncrBy(ctx, user_liked_id, -1) - _, err := pipe.ZRem(ctx, user_like_Id, req.VideoId).Result() - if err == redis.Nil { - err = nil - } + pipe.ZRem(ctx, user_like_Id, req.VideoId) + return nil }) + if err == redis.Nil { + err = nil + } } }