From fdb4250652eb0191fd45255f1940bc1151ab48ea Mon Sep 17 00:00:00 2001 From: Damillora Date: Thu, 14 Apr 2022 21:27:27 +0700 Subject: [PATCH] feat: add total pages --- pkg/app/post_routes.go | 10 ++++++++++ pkg/models/responses.go | 1 + 2 files changed, 11 insertions(+) diff --git a/pkg/app/post_routes.go b/pkg/app/post_routes.go index 38e0392..112be82 100644 --- a/pkg/app/post_routes.go +++ b/pkg/app/post_routes.go @@ -32,12 +32,16 @@ func postGet(c *gin.Context) { pageParam := c.Query("page") page, _ := strconv.Atoi(pageParam) + if page < 1 { + page = 1 + } tag := c.Query("tags") tags := strings.Split(tag, " ") var posts []database.Post var postPages int + var perPage = 20 if tag != "" { posts = services.GetPostTags(page, tags) @@ -47,6 +51,11 @@ func postGet(c *gin.Context) { postPages = services.CountPostPages() } + var totalPage = postPages / perPage + if postPages%perPage > 0 { + totalPage++ + } + var postResult []models.PostListItem for _, post := range posts { var tagStrings []string @@ -63,6 +72,7 @@ func postGet(c *gin.Context) { } c.JSON(http.StatusOK, models.PostPaginationResponse{ CurrentPage: page, + TotalPage: totalPage, PostCount: postPages, Posts: postResult, }) diff --git a/pkg/models/responses.go b/pkg/models/responses.go index cab45ca..268af3c 100644 --- a/pkg/models/responses.go +++ b/pkg/models/responses.go @@ -28,6 +28,7 @@ type BlobSimilarResponse struct { } type PostPaginationResponse struct { CurrentPage int `json:"currentPage"` + TotalPage int `json:"totalPage"` PostCount int `json:"postCount"` Posts []PostListItem `json:"posts"` }