You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
paopao-ce/pkg/app/pagination.go

63 lines
1.4 KiB

// Copyright 2022 ROC. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
3 years ago
package app
import (
"github.com/gin-gonic/gin"
"github.com/rocboss/paopao-ce/internal/conf"
"github.com/rocboss/paopao-ce/pkg/convert"
3 years ago
)
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 conf.AppSetting.DefaultPageSize
3 years ago
}
if pageSize > conf.AppSetting.MaxPageSize {
return conf.AppSetting.MaxPageSize
3 years ago
}
return pageSize
}
func GetPageOffset(c *gin.Context) (offset, limit int) {
page := convert.StrTo(c.Query("page")).MustInt()
if page <= 0 {
page = 1
3 years ago
}
limit = convert.StrTo(c.Query("page_size")).MustInt()
if limit <= 0 {
limit = conf.AppSetting.DefaultPageSize
} else if limit > conf.AppSetting.MaxPageSize {
limit = conf.AppSetting.MaxPageSize
}
offset = (page - 1) * limit
return
3 years ago
}
func GetPageInfo(c *gin.Context) (page, pageSize int) {
page = convert.StrTo(c.Query("page")).MustInt()
if page <= 0 {
page = 1
}
pageSize = convert.StrTo(c.Query("page_size")).MustInt()
if pageSize <= 0 {
pageSize = conf.AppSetting.DefaultPageSize
} else if pageSize > conf.AppSetting.MaxPageSize {
pageSize = conf.AppSetting.MaxPageSize
}
return
}