From fab63a2743f36e936fc67e96fe29e3fd7bc9af9f Mon Sep 17 00:00:00 2001 From: Sizhe Zhao Date: Tue, 2 Jul 2024 00:28:25 +0800 Subject: [PATCH] fix(controllers/comments.RecentCommentByPage): use separate queries --- internal/controllers/comments/recent_by_page.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/internal/controllers/comments/recent_by_page.go b/internal/controllers/comments/recent_by_page.go index f07d1a0..e0d0b42 100644 --- a/internal/controllers/comments/recent_by_page.go +++ b/internal/controllers/comments/recent_by_page.go @@ -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) }