123456789101112131415161718192021222324252627282930313233343536 |
- package middleware
- import (
- "fmt"
- "github.com/gin-gonic/gin"
- "strconv"
- )
- // Pagination sets page, pageSize and pageOffset to *gin.Context
- func Pagination() gin.HandlerFunc {
- return func(c *gin.Context) {
- page := getSetItem(c, "page", 1)
- size := getSetItem(c, "pageSize", 20)
- c.Set("pageOffset", (page-1)*size)
- c.Next()
- }
- }
- func getSetItem(c *gin.Context, k string, d int) int {
- var n int
- if v := c.Query(k); v != "" {
- if i, err := strconv.Atoi(v); err == nil {
- if i > 0 {
- n = i
- }
- }
- }
- if n == 0 {
- n = d
- }
- c.Set(k, n)
- c.Request.URL.Query().Set(k, fmt.Sprintf("%d", n))
- return n
- }
|