diff --git a/internal/dao/jinzhu/comments.go b/internal/dao/jinzhu/comments.go index c30fda69..262c3007 100644 --- a/internal/dao/jinzhu/comments.go +++ b/internal/dao/jinzhu/comments.go @@ -423,20 +423,5 @@ func (s *commentManageSrv) ThumbsDownReply(userId int64, tweetId, commentId, rep } func (s *commentManageSrv) updateCommentThumbsUpCount(obj any, id int64, thumbsUpCount, thumbsDownCount int32) error { - updateColumns := make(map[string]any, 2) - if thumbsUpCount == 1 { - updateColumns["thumbs_up_count"] = gorm.Expr("thumbs_up_count + 1") - } else if thumbsUpCount == -1 { - updateColumns["thumbs_up_count"] = gorm.Expr("thumbs_up_count - 1") - } - if thumbsDownCount == 1 { - updateColumns["thumbs_down_count"] = gorm.Expr("thumbs_down_count + 1") - } else if thumbsDownCount == -1 { - updateColumns["thumbs_down_count"] = gorm.Expr("thumbs_down_count - 1") - } - if len(updateColumns) > 0 { - updateColumns["modified_on"] = time.Now().Unix() - return s.db.Model(obj).Where("id=?", id).UpdateColumns(updateColumns).Error - } - return nil + return updateCommentThumbsUpCount(s.db, obj, id, thumbsUpCount, thumbsDownCount) } diff --git a/internal/dao/jinzhu/utils.go b/internal/dao/jinzhu/utils.go index 9d97b4b8..6d7ef850 100644 --- a/internal/dao/jinzhu/utils.go +++ b/internal/dao/jinzhu/utils.go @@ -5,6 +5,8 @@ package jinzhu import ( + "time" + "github.com/rocboss/paopao-ce/internal/core/cs" "github.com/rocboss/paopao-ce/internal/dao/jinzhu/dbr" "gorm.io/gorm" @@ -78,3 +80,22 @@ func getUsersByIDs(db *gorm.DB, ids []int64) ([]*dbr.User, error) { "id IN ?": ids, }, 0, 0) } + +func updateCommentThumbsUpCount(db *gorm.DB, obj any, id int64, thumbsUpCount, thumbsDownCount int32) error { + updateColumns := make(map[string]any, 2) + if thumbsUpCount == 1 { + updateColumns["thumbs_up_count"] = gorm.Expr("thumbs_up_count + 1") + } else if thumbsUpCount == -1 { + updateColumns["thumbs_up_count"] = gorm.Expr("thumbs_up_count - 1") + } + if thumbsDownCount == 1 { + updateColumns["thumbs_down_count"] = gorm.Expr("thumbs_down_count + 1") + } else if thumbsDownCount == -1 { + updateColumns["thumbs_down_count"] = gorm.Expr("thumbs_down_count - 1") + } + if len(updateColumns) > 0 { + updateColumns["modified_on"] = time.Now().Unix() + return db.Model(obj).Where("id=?", id).UpdateColumns(updateColumns).Error + } + return nil +}