Skip to content

Commit

Permalink
feat: gin 分页处理
Browse files Browse the repository at this point in the history
  • Loading branch information
minibear2333 committed Jun 13, 2021
1 parent 597741e commit 572c950
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pkg/app/pagination.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package app

import (
"github.com/gin-gonic/gin"
"github.com/golang-minibear2333/gin-blog/global"
"github.com/golang-minibear2333/gin-blog/pkg/convert"
)

func GetPage(c *gin.Context) int {
page := convert.StrTo(c.Query("page")).MustInt()
if page <= 0 {
return 1
}

return page
}

func GetPageSize(c *gin.Context) int {
pageSize := convert.StrTo(c.Query("page_size")).MustInt()
if pageSize <= 0 {
return global.AppSetting.DefaultPageSize
}
if pageSize > global.AppSetting.MaxPageSize {
return global.AppSetting.MaxPageSize
}

return pageSize
}

func GetPageOffset(page, pageSize int) int {
result := 0
if page > 0 {
result = (page - 1) * pageSize
}

return result
}

0 comments on commit 572c950

Please sign in to comment.