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") pageParam := c.Query("page")
page, _ := strconv.Atoi(pageParam) page, _ := strconv.Atoi(pageParam)
if page < 1 {
page = 1
}
tag := c.Query("tags") tag := c.Query("tags")
tags := strings.Split(tag, " ") tags := strings.Split(tag, " ")
var posts []database.Post var posts []database.Post
var postPages int var postPages int
var perPage = 20
if tag != "" { if tag != "" {
posts = services.GetPostTags(page, tags) posts = services.GetPostTags(page, tags)
@ -47,6 +51,11 @@ func postGet(c *gin.Context) {
postPages = services.CountPostPages() postPages = services.CountPostPages()
} }
var totalPage = postPages / perPage
if postPages%perPage > 0 {
totalPage++
}
var postResult []models.PostListItem var postResult []models.PostListItem
for _, post := range posts { for _, post := range posts {
var tagStrings []string var tagStrings []string
@ -63,6 +72,7 @@ func postGet(c *gin.Context) {
} }
c.JSON(http.StatusOK, models.PostPaginationResponse{ c.JSON(http.StatusOK, models.PostPaginationResponse{
CurrentPage: page, CurrentPage: page,
TotalPage: totalPage,
PostCount: postPages, PostCount: postPages,
Posts: postResult, Posts: postResult,
}) })

View File

@ -28,6 +28,7 @@ type BlobSimilarResponse struct {
} }
type PostPaginationResponse struct { type PostPaginationResponse struct {
CurrentPage int `json:"currentPage"` CurrentPage int `json:"currentPage"`
TotalPage int `json:"totalPage"`
PostCount int `json:"postCount"` PostCount int `json:"postCount"`
Posts []PostListItem `json:"posts"` Posts []PostListItem `json:"posts"`
} }