feat: add total pages

This commit is contained in:
Damillora 2022-04-14 21:27:27 +07:00
parent ba86dd3c01
commit fdb4250652
2 changed files with 11 additions and 0 deletions

View File

@ -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,
})

View File

@ -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"`
}