Skip to content

Commit

Permalink
Merge pull request #37 from ShanghaitechGeekPie/fix-recent-by-page
Browse files Browse the repository at this point in the history
fix(controllers/comments.RecentCommentByPage): use separate queries
  • Loading branch information
Prince213 authored Jul 3, 2024
2 parents e1bfe33 + af6b05e commit 86f79ca
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions internal/controllers/comments/recent_by_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ func RecentCommentByPage(c *fiber.Ctx) (err error) {
}

db := database.GetDB()
var comments []models.Comment

var count int64
result := db.Preload("User").Preload("CourseGroup").Preload("CourseGroup.Course").Preload("CourseGroup.Teachers").Count(&count).
Order("update_time DESC").Offset((id - 1) * 30).Limit(30).Find(&comments)
if err := result.Error; err != nil {
err = db.Model(&models.Comment{}).Count(&count).Error
if err != nil {
return errors.Wrap(err, errors.DatabaseError)
}

var comments []models.Comment
err = db.Preload("User").Preload("CourseGroup").Preload("CourseGroup.Course").Preload("CourseGroup.Teachers").
Order("update_time DESC").Offset((id - 1) * 30).Limit(30).Find(&comments).Error
if err != nil {
return errors.Wrap(err, errors.DatabaseError)
}

Expand Down

0 comments on commit 86f79ca

Please sign in to comment.