Skip to content

Commit

Permalink
feat(controllers/comments.RecentCommentByPage): return page count
Browse files Browse the repository at this point in the history
  • Loading branch information
Prince213 committed Apr 28, 2024
1 parent 4bf3a15 commit 4f6f3cb
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions internal/controllers/comments/recent_by_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ func RecentCommentByPage(c *fiber.Ctx) (err error) {

db := database.GetDB()
var comments []models.Comment
result := db.Preload("User").Preload("CourseGroup").Preload("CourseGroup.Course").Preload("CourseGroup.Teachers").
Order("update_time DESC").Offset((id - 1) * 30).Limit(31).Find(&comments)
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 {
return errors.Wrap(err, errors.DatabaseError)
}
if result.RowsAffected == 31 {
comments = comments[:30]
}

var likeResult []CommentLikeResult
if uid != 0 {
Expand All @@ -49,9 +47,10 @@ func RecentCommentByPage(c *fiber.Ctx) (err error) {
var response []CommentResponse
response = GenerateResponse(comments, uid, likeResult, true, utils.GetIP(c))
return c.Status(fiber.StatusOK).JSON(models.OKResponse{
Data: fiber.Map{
"has_more": result.RowsAffected == 31,
"comments": response,
Data: RecentCommentByPageResponse{
PageCount: (count + 29) / 30, // ceil
HasMore: count > int64(id)*30,
Comments: response,
},
Error: false,
})
Expand Down

0 comments on commit 4f6f3cb

Please sign in to comment.