Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix favorite cancel #172

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions src/constant/strings/err.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "没有点赞,不能取消点赞"
)
85 changes: 54 additions & 31 deletions src/services/favorite/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -92,26 +80,60 @@ 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)
pipe.ZRem(ctx, user_like_Id, req.VideoId)

return nil
})
if err == redis.Nil {
err = nil
}
}
return nil
})

}
if err != nil {
logger.WithFields(logrus.Fields{
"ActorId": req.ActorId,
Expand Down Expand Up @@ -157,6 +179,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)

Expand Down Expand Up @@ -256,7 +279,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,
Expand Down
Loading