Skip to content

update review count #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions danke/api/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,35 @@ func DeleteReviewV1(c *fiber.Ctx) (err error) {
return Forbidden("没有权限")
}

// 删除评论
err = DB.Delete(review).Error
// 在事务中删除评论并更新计数
err = DB.Transaction(func(tx *gorm.DB) error {
// 删除评论
if err := tx.Delete(review).Error; err != nil {
return err
}

// 更新课程评教数量
if err := tx.Model(&Course{ID: review.CourseID}).
Update("review_count", gorm.Expr("review_count - 1")).Error; err != nil {
return err
}

// 获取课程组ID
var courseGroupID int
if err := tx.Model(&Course{}).Select("course_group_id").
Where("id = ?", review.CourseID).Scan(&courseGroupID).Error; err != nil {
return err
}

// 更新课程组评教数量
if err := tx.Model(&CourseGroup{ID: courseGroupID}).
Update("review_count", gorm.Expr("review_count - 1")).Error; err != nil {
return err
}

return nil
})

if err != nil {
return
}
Expand Down