Skip to content

Commit

Permalink
fix(controllers/comments.RecentCommentByPage): use separate queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Prince213 committed Jul 1, 2024
1 parent e1bfe33 commit fab63a2
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.Preload("User").Preload("CourseGroup").Preload("CourseGroup.Course").Preload("CourseGroup.Teachers").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 fab63a2

Please sign in to comment.