⚡ optimize the list of comments
This commit is contained in:
@@ -56,3 +56,35 @@ func (CommentReplyService) UpdateCommentReplyCount(commentID int64) error {
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateCommentLikesCount 更新评论 likes 数量
|
||||
func (CommentReplyService) UpdateCommentLikesCount(commentID int64, topicID string) error {
|
||||
// 使用事务处理错误
|
||||
err := global.DB.Transaction(func(tx *gorm.DB) error {
|
||||
result := tx.Model(&model.ScaCommentReply{}).Where("id = ? and topic_id = ? and deleted = 0", commentID, topicID).Update("likes", gorm.Expr("likes + ?", 1))
|
||||
if result.Error != nil {
|
||||
return result.Error // 返回更新错误
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("comment not found") // 处理评论不存在的情况
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// DecrementCommentLikesCount 减少评论 likes 数量
|
||||
func (CommentReplyService) DecrementCommentLikesCount(commentID int64, topicID string) error {
|
||||
// 使用事务处理错误
|
||||
err := global.DB.Transaction(func(tx *gorm.DB) error {
|
||||
result := tx.Model(&model.ScaCommentReply{}).Where("id = ? and topic_id = ? and deleted = 0", commentID, topicID).Update("likes", gorm.Expr("likes - ?", 1))
|
||||
if result.Error != nil {
|
||||
return result.Error // 返回更新错误
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("comment not found") // 处理评论不存在的情况
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user