fix: unify thumbs transactions for comments and replies

pull/699/head
yoclo 8 months ago
parent 4fb7beb25d
commit f09846789c

@ -201,7 +201,7 @@ func (s *commentManageSrv) CreateCommentReply(reply *ms.CommentReply) (res *ms.C
func (s *commentManageSrv) DeleteCommentReply(reply *ms.CommentReply) (err error) { func (s *commentManageSrv) DeleteCommentReply(reply *ms.CommentReply) (err error) {
db := s.db.Begin() db := s.db.Begin()
defer db.Rollback() defer db.Rollback()
err = reply.Delete(s.db) err = reply.Delete(db)
if err != nil { if err != nil {
return return
} }
@ -233,7 +233,7 @@ func (s *commentManageSrv) ThumbsUpComment(userId int64, tweetId, commentId int6
) )
commentThumbs := &dbr.TweetCommentThumbs{} commentThumbs := &dbr.TweetCommentThumbs{}
// 检查thumbs状态 // 检查thumbs状态
err := s.db.Where("user_id=? AND tweet_id=? AND comment_id=? AND comment_type=0", userId, tweetId, commentId).Take(commentThumbs).Error err := db.Where("user_id=? AND tweet_id=? AND comment_id=? AND comment_type=0", userId, tweetId, commentId).Take(commentThumbs).Error
if err == nil { if err == nil {
switch { switch {
case commentThumbs.IsThumbsUp == types.Yes && commentThumbs.IsThumbsDown == types.No: case commentThumbs.IsThumbsUp == types.Yes && commentThumbs.IsThumbsDown == types.No:
@ -261,11 +261,11 @@ func (s *commentManageSrv) ThumbsUpComment(userId int64, tweetId, commentId int6
thumbsUpCount, thumbsDownCount = 1, 0 thumbsUpCount, thumbsDownCount = 1, 0
} }
// 更新thumbs状态 // 更新thumbs状态
if err = s.db.Save(commentThumbs).Error; err != nil { if err = db.Save(commentThumbs).Error; err != nil {
return err return err
} }
// 更新thumbsUpCount // 更新thumbsUpCount
if err = s.updateCommentThumbsUpCount(&dbr.Comment{}, commentId, thumbsUpCount, thumbsDownCount); err != nil { if err = updateCommentThumbsUpCount(db, &dbr.Comment{}, commentId, thumbsUpCount, thumbsDownCount); err != nil {
return err return err
} }
db.Commit() db.Commit()
@ -282,7 +282,7 @@ func (s *commentManageSrv) ThumbsDownComment(userId int64, tweetId, commentId in
) )
commentThumbs := &dbr.TweetCommentThumbs{} commentThumbs := &dbr.TweetCommentThumbs{}
// 检查thumbs状态 // 检查thumbs状态
err := s.db.Where("user_id=? AND tweet_id=? AND comment_id=? AND comment_type=0", userId, tweetId, commentId).Take(commentThumbs).Error err := db.Where("user_id=? AND tweet_id=? AND comment_id=? AND comment_type=0", userId, tweetId, commentId).Take(commentThumbs).Error
if err == nil { if err == nil {
switch { switch {
case commentThumbs.IsThumbsDown == types.Yes: case commentThumbs.IsThumbsDown == types.Yes:
@ -311,11 +311,11 @@ func (s *commentManageSrv) ThumbsDownComment(userId int64, tweetId, commentId in
thumbsUpCount, thumbsDownCount = 0, 1 thumbsUpCount, thumbsDownCount = 0, 1
} }
// 更新thumbs状态 // 更新thumbs状态
if err = s.db.Save(commentThumbs).Error; err != nil { if err = db.Save(commentThumbs).Error; err != nil {
return err return err
} }
// 更新thumbsUpCount // 更新thumbsUpCount
if err = s.updateCommentThumbsUpCount(&dbr.Comment{}, commentId, thumbsUpCount, thumbsDownCount); err != nil { if err = updateCommentThumbsUpCount(db, &dbr.Comment{}, commentId, thumbsUpCount, thumbsDownCount); err != nil {
return err return err
} }
db.Commit() db.Commit()
@ -332,7 +332,7 @@ func (s *commentManageSrv) ThumbsUpReply(userId int64, tweetId, commentId, reply
) )
commentThumbs := &dbr.TweetCommentThumbs{} commentThumbs := &dbr.TweetCommentThumbs{}
// 检查thumbs状态 // 检查thumbs状态
err := s.db.Where("user_id=? AND tweet_id=? AND comment_id=? AND reply_id=? AND comment_type=1", userId, tweetId, commentId, replyId).Take(commentThumbs).Error err := db.Where("user_id=? AND tweet_id=? AND comment_id=? AND reply_id=? AND comment_type=1", userId, tweetId, commentId, replyId).Take(commentThumbs).Error
if err == nil { if err == nil {
switch { switch {
case commentThumbs.IsThumbsUp == types.Yes: case commentThumbs.IsThumbsUp == types.Yes:
@ -361,11 +361,11 @@ func (s *commentManageSrv) ThumbsUpReply(userId int64, tweetId, commentId, reply
thumbsUpCount, thumbsDownCount = 1, 0 thumbsUpCount, thumbsDownCount = 1, 0
} }
// 更新thumbs状态 // 更新thumbs状态
if err = s.db.Save(commentThumbs).Error; err != nil { if err = db.Save(commentThumbs).Error; err != nil {
return err return err
} }
// 更新thumbsUpCount // 更新thumbsUpCount
if err = s.updateCommentThumbsUpCount(&dbr.CommentReply{}, replyId, thumbsUpCount, thumbsDownCount); err != nil { if err = updateCommentThumbsUpCount(db, &dbr.CommentReply{}, replyId, thumbsUpCount, thumbsDownCount); err != nil {
return err return err
} }
db.Commit() db.Commit()
@ -382,7 +382,7 @@ func (s *commentManageSrv) ThumbsDownReply(userId int64, tweetId, commentId, rep
) )
commentThumbs := &dbr.TweetCommentThumbs{} commentThumbs := &dbr.TweetCommentThumbs{}
// 检查thumbs状态 // 检查thumbs状态
err := s.db.Where("user_id=? AND tweet_id=? AND comment_id=? AND reply_id=? AND comment_type=1", userId, tweetId, commentId, replyId).Take(commentThumbs).Error err := db.Where("user_id=? AND tweet_id=? AND comment_id=? AND reply_id=? AND comment_type=1", userId, tweetId, commentId, replyId).Take(commentThumbs).Error
if err == nil { if err == nil {
switch { switch {
case commentThumbs.IsThumbsDown == types.Yes: case commentThumbs.IsThumbsDown == types.Yes:
@ -411,11 +411,11 @@ func (s *commentManageSrv) ThumbsDownReply(userId int64, tweetId, commentId, rep
thumbsUpCount, thumbsDownCount = 0, 1 thumbsUpCount, thumbsDownCount = 0, 1
} }
// 更新thumbs状态 // 更新thumbs状态
if err = s.db.Save(commentThumbs).Error; err != nil { if err = db.Save(commentThumbs).Error; err != nil {
return err return err
} }
// 更新thumbsUpCount // 更新thumbsUpCount
if err = s.updateCommentThumbsUpCount(&dbr.CommentReply{}, replyId, thumbsUpCount, thumbsDownCount); err != nil { if err = updateCommentThumbsUpCount(db, &dbr.CommentReply{}, replyId, thumbsUpCount, thumbsDownCount); err != nil {
return err return err
} }
db.Commit() db.Commit()

Loading…
Cancel
Save