optimize the list of comments

This commit is contained in:
landaiqing
2024-09-25 11:46:49 +08:00
parent bb2f49fe74
commit 97175c3d67
9 changed files with 333 additions and 99 deletions

View File

@@ -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
}