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.
This commit is contained in:
2026-04-11 13:12:27 +08:00
parent b6aff65975
commit e00af0bce4
4 changed files with 66 additions and 48 deletions

View File

@@ -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{
"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{
"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{
"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{
"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
}

View File

@@ -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
}

View File

@@ -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) {
@@ -60,10 +60,13 @@ func (h *WebhookHandler) ListWebhooks(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": webhooks,
"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}})
}

View File

@@ -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"])
}
}