refactor: 提取公共分页解析函数 parsePageAndSize
- 新增 internal/api/handler/common.go 存放 handler 层公共辅助函数 - parsePageAndSize: 统一提取 page/page_size 参数解析、默认值设置、ClampPageSize 调用 - device/log/webhook handler: 替换重复的分页解析代码为 parsePageAndSize 调用 - 清理不再需要的 strconv/pagination 导入
This commit is contained in:
21
internal/api/handler/common.go
Normal file
21
internal/api/handler/common.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/user-management-system/internal/pagination"
|
||||
)
|
||||
|
||||
// parsePageAndSize extracts and validates page & page_size from query parameters.
|
||||
// Returns page (>=1) and pageSize (clamped to [1, MaxPageSize]).
|
||||
func parsePageAndSize(c *gin.Context) (page, pageSize int) {
|
||||
page, _ = strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
pageSize, _ = strconv.Atoi(c.DefaultQuery("page_size", strconv.Itoa(pagination.DefaultPageSize)))
|
||||
pageSize = pagination.ClampPageSize(pageSize)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user