mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
Add comments count to Resource model and update logic for comment creation and deletion
This commit is contained in:
@@ -35,6 +35,12 @@ func CreateComment(content string, userID uint, resourceID uint, imageIDs []uint
|
||||
if err := tx.Model(&model.User{}).Where("id = ?", userID).Update("comments_count", gorm.Expr("comments_count + 1")).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Update resource comments count
|
||||
if err := tx.Model(&model.Resource{}).Where("id = ?", resourceID).Update("comments", gorm.Expr("comments + 1")).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
@@ -166,6 +172,9 @@ func DeleteCommentByID(commentID uint) error {
|
||||
if err := tx.Model(&model.User{}).Where("id = ?", comment.UserID).Update("comments_count", gorm.Expr("comments_count - 1")).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Model(&model.Resource{}).Where("id = ?", comment.RefID).Update("comments", gorm.Expr("comments - 1")).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.
|
||||
Where("type = ? and ref_id = ?", model.ActivityTypeNewComment, commentID).
|
||||
Delete(&model.Activity{}).
|
||||
|
Reference in New Issue
Block a user