refactor: 提取分页魔法数字为 pagination 常量
- handler 层: device/log/webhook/user handler 使用 pagination.DefaultPageSize/MaxPageSize - service 层: device/login_log/operation_log service 使用 pagination.DefaultPageSize - repository 层: user repository 使用 pagination.DefaultPageSize/MaxPageSize - 消除 8 处硬编码的 20/100 分页魔法数字
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/user-management-system/internal/pagination"
|
||||
"github.com/user-management-system/internal/service"
|
||||
)
|
||||
|
||||
@@ -66,12 +67,12 @@ func (h *WebhookHandler) CreateWebhook(c *gin.Context) {
|
||||
// @Router /api/v1/webhooks [get]
|
||||
func (h *WebhookHandler) ListWebhooks(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", strconv.Itoa(pagination.DefaultPageSize)))
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize < 1 || pageSize > 100 {
|
||||
pageSize = 20
|
||||
if pageSize < 1 || pageSize > pagination.MaxPageSize {
|
||||
pageSize = pagination.DefaultPageSize
|
||||
}
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
@@ -178,9 +179,9 @@ func (h *WebhookHandler) GetWebhookDeliveries(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "20"))
|
||||
if limit < 1 || limit > 100 {
|
||||
limit = 20
|
||||
limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(pagination.DefaultPageSize)))
|
||||
if limit < 1 || limit > pagination.MaxPageSize {
|
||||
limit = pagination.DefaultPageSize
|
||||
}
|
||||
|
||||
deliveries, err := h.webhookService.GetWebhookDeliveries(c.Request.Context(), id, limit)
|
||||
|
||||
Reference in New Issue
Block a user