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:
2026-05-08 12:48:03 +08:00
parent b3374dccf4
commit b8e9af001f
4 changed files with 27 additions and 36 deletions

View File

@@ -66,12 +66,7 @@ func (h *WebhookHandler) CreateWebhook(c *gin.Context) {
// @Failure 500 {object} Response "服务器错误"
// @Router /api/v1/webhooks [get]
func (h *WebhookHandler) ListWebhooks(c *gin.Context) {
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)
page, pageSize := parsePageAndSize(c)
offset := (page - 1) * pageSize
userID, _ := c.Get("user_id")
@@ -178,9 +173,7 @@ func (h *WebhookHandler) GetWebhookDeliveries(c *gin.Context) {
}
limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(pagination.DefaultPageSize)))
if limit < 1 || limit > pagination.MaxPageSize {
limit = pagination.DefaultPageSize
}
limit = pagination.ClampPageSize(limit)
deliveries, err := h.webhookService.GetWebhookDeliveries(c.Request.Context(), id, limit)
if err != nil {