From e00af0bce412938585a47192ececd401e4caef4a Mon Sep 17 00:00:00 2001 From: long-agent Date: Sat, 11 Apr 2026 13:12:27 +0800 Subject: [PATCH] fix: unify handler response format in log, permission, webhook handlers - log_handler.go: Fix GetMyLoginLogs/GetMyOperationLogs/GetLoginLogs/GetOperationLogs to use {code, message, data} - permission_handler.go: Fix all error responses to use {code, message} - webhook_handler.go: Add missing "message" field in success responses, wrap data in data object with list/total/page/page_size - webhook_handler_test.go: Update test to match new response format Standardize all JSON responses to {code: 0, message: "success", data: ...} for success and {code: XXX, message: "..."} for errors. --- internal/api/handler/log_handler.go | 58 +++++++++++++------- internal/api/handler/permission_handler.go | 18 +++--- internal/api/handler/webhook_handler.go | 17 +++--- internal/api/handler/webhook_handler_test.go | 21 ++++--- 4 files changed, 66 insertions(+), 48 deletions(-) diff --git a/internal/api/handler/log_handler.go b/internal/api/handler/log_handler.go index dcacb42..1eeaf62 100644 --- a/internal/api/handler/log_handler.go +++ b/internal/api/handler/log_handler.go @@ -27,7 +27,7 @@ func NewLogHandler(loginLogService *service.LoginLogService, operationLogService func (h *LogHandler) GetMyLoginLogs(c *gin.Context) { userID, ok := getUserIDFromContext(c) if !ok { - c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"}) + c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"}) return } @@ -41,17 +41,21 @@ func (h *LogHandler) GetMyLoginLogs(c *gin.Context) { } c.JSON(http.StatusOK, gin.H{ - "list": logs, - "total": total, - "page": page, - "page_size": pageSize, + "code": 0, + "message": "success", + "data": gin.H{ + "list": logs, + "total": total, + "page": page, + "page_size": pageSize, + }, }) } func (h *LogHandler) GetMyOperationLogs(c *gin.Context) { userID, ok := getUserIDFromContext(c) if !ok { - c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"}) + c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"}) return } @@ -65,17 +69,21 @@ func (h *LogHandler) GetMyOperationLogs(c *gin.Context) { } c.JSON(http.StatusOK, gin.H{ - "list": logs, - "total": total, - "page": page, - "page_size": pageSize, + "code": 0, + "message": "success", + "data": gin.H{ + "list": logs, + "total": total, + "page": page, + "page_size": pageSize, + }, }) } func (h *LogHandler) GetLoginLogs(c *gin.Context) { var req service.ListLoginLogRequest if err := c.ShouldBindQuery(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()}) return } @@ -102,17 +110,21 @@ func (h *LogHandler) GetLoginLogs(c *gin.Context) { } c.JSON(http.StatusOK, gin.H{ - "list": logs, - "total": total, - "page": req.Page, - "page_size": req.PageSize, + "code": 0, + "message": "success", + "data": gin.H{ + "list": logs, + "total": total, + "page": req.Page, + "page_size": req.PageSize, + }, }) } func (h *LogHandler) GetOperationLogs(c *gin.Context) { var req service.ListOperationLogRequest if err := c.ShouldBindQuery(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()}) return } @@ -139,17 +151,21 @@ func (h *LogHandler) GetOperationLogs(c *gin.Context) { } c.JSON(http.StatusOK, gin.H{ - "list": logs, - "total": total, - "page": req.Page, - "page_size": req.PageSize, + "code": 0, + "message": "success", + "data": gin.H{ + "list": logs, + "total": total, + "page": req.Page, + "page_size": req.PageSize, + }, }) } func (h *LogHandler) ExportLoginLogs(c *gin.Context) { var req service.ExportLoginLogRequest if err := c.ShouldBindQuery(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()}) return } diff --git a/internal/api/handler/permission_handler.go b/internal/api/handler/permission_handler.go index e89c6f2..f696f88 100644 --- a/internal/api/handler/permission_handler.go +++ b/internal/api/handler/permission_handler.go @@ -23,7 +23,7 @@ func NewPermissionHandler(permissionService *service.PermissionService) *Permiss func (h *PermissionHandler) CreatePermission(c *gin.Context) { var req service.CreatePermissionRequest if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()}) return } @@ -43,7 +43,7 @@ func (h *PermissionHandler) CreatePermission(c *gin.Context) { func (h *PermissionHandler) ListPermissions(c *gin.Context) { var req service.ListPermissionRequest if err := c.ShouldBindQuery(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()}) return } @@ -63,7 +63,7 @@ func (h *PermissionHandler) ListPermissions(c *gin.Context) { func (h *PermissionHandler) GetPermission(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid permission id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid permission id"}) return } @@ -83,13 +83,13 @@ func (h *PermissionHandler) GetPermission(c *gin.Context) { func (h *PermissionHandler) UpdatePermission(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid permission id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid permission id"}) return } var req service.UpdatePermissionRequest if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()}) return } @@ -109,7 +109,7 @@ func (h *PermissionHandler) UpdatePermission(c *gin.Context) { func (h *PermissionHandler) DeletePermission(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid permission id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid permission id"}) return } @@ -127,7 +127,7 @@ func (h *PermissionHandler) DeletePermission(c *gin.Context) { func (h *PermissionHandler) UpdatePermissionStatus(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid permission id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid permission id"}) return } @@ -136,7 +136,7 @@ func (h *PermissionHandler) UpdatePermissionStatus(c *gin.Context) { } if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()}) return } @@ -147,7 +147,7 @@ func (h *PermissionHandler) UpdatePermissionStatus(c *gin.Context) { case "disabled", "0": status = domain.PermissionStatusDisabled default: - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid status"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid status"}) return } diff --git a/internal/api/handler/webhook_handler.go b/internal/api/handler/webhook_handler.go index ad6381e..9a282c1 100644 --- a/internal/api/handler/webhook_handler.go +++ b/internal/api/handler/webhook_handler.go @@ -35,7 +35,7 @@ func (h *WebhookHandler) CreateWebhook(c *gin.Context) { return } - c.JSON(http.StatusCreated, gin.H{"code": 0, "data": webhook}) + c.JSON(http.StatusCreated, gin.H{"code": 0, "message": "success", "data": webhook}) } func (h *WebhookHandler) ListWebhooks(c *gin.Context) { @@ -59,11 +59,14 @@ func (h *WebhookHandler) ListWebhooks(c *gin.Context) { } c.JSON(http.StatusOK, gin.H{ - "code": 0, - "data": webhooks, - "total": total, - "page": page, - "page_size": pageSize, + "code": 0, + "message": "success", + "data": gin.H{ + "list": webhooks, + "total": total, + "page": page, + "page_size": pageSize, + }, }) } @@ -121,5 +124,5 @@ func (h *WebhookHandler) GetWebhookDeliveries(c *gin.Context) { return } - c.JSON(http.StatusOK, gin.H{"code": 0, "data": deliveries}) + c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success", "data": gin.H{"deliveries": deliveries}}) } diff --git a/internal/api/handler/webhook_handler_test.go b/internal/api/handler/webhook_handler_test.go index 773acf6..4ebffa7 100644 --- a/internal/api/handler/webhook_handler_test.go +++ b/internal/api/handler/webhook_handler_test.go @@ -255,10 +255,8 @@ func TestWebhookHandler_ListWebhooks_Success(t *testing.T) { if result["code"].(float64) != 0 { t.Fatalf("expected code 0, got %v", result["code"]) } - if result["data"] == nil { - t.Fatal("expected data in response") - } - if result["total"] == nil { + data := result["data"].(map[string]interface{}) + if data["total"] == nil { t.Fatal("expected total in response") } } @@ -436,15 +434,16 @@ func TestWebhookHandler_ListWebhooks_Pagination(t *testing.T) { var result map[string]interface{} json.NewDecoder(resp.Body).Decode(&result) - data := result["data"].([]interface{}) - if len(data) != 2 { - t.Fatalf("expected 2 webhooks per page, got %d", len(data)) + data := result["data"].(map[string]interface{}) + list := data["list"].([]interface{}) + if len(list) != 2 { + t.Fatalf("expected 2 webhooks per page, got %d", len(list)) } - if result["page"].(float64) != 1 { - t.Fatalf("expected page 1, got %v", result["page"]) + if data["page"].(float64) != 1 { + t.Fatalf("expected page 1, got %v", data["page"]) } - if result["page_size"].(float64) != 2 { - t.Fatalf("expected page_size 2, got %v", result["page_size"]) + if data["page_size"].(float64) != 2 { + t.Fatalf("expected page_size 2, got %v", data["page_size"]) } }