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:
@@ -27,7 +27,7 @@ func NewLogHandler(loginLogService *service.LoginLogService, operationLogService
|
|||||||
func (h *LogHandler) GetMyLoginLogs(c *gin.Context) {
|
func (h *LogHandler) GetMyLoginLogs(c *gin.Context) {
|
||||||
userID, ok := getUserIDFromContext(c)
|
userID, ok := getUserIDFromContext(c)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,17 +41,21 @@ func (h *LogHandler) GetMyLoginLogs(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"list": logs,
|
"code": 0,
|
||||||
"total": total,
|
"message": "success",
|
||||||
"page": page,
|
"data": gin.H{
|
||||||
"page_size": pageSize,
|
"list": logs,
|
||||||
|
"total": total,
|
||||||
|
"page": page,
|
||||||
|
"page_size": pageSize,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *LogHandler) GetMyOperationLogs(c *gin.Context) {
|
func (h *LogHandler) GetMyOperationLogs(c *gin.Context) {
|
||||||
userID, ok := getUserIDFromContext(c)
|
userID, ok := getUserIDFromContext(c)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,17 +69,21 @@ func (h *LogHandler) GetMyOperationLogs(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"list": logs,
|
"code": 0,
|
||||||
"total": total,
|
"message": "success",
|
||||||
"page": page,
|
"data": gin.H{
|
||||||
"page_size": pageSize,
|
"list": logs,
|
||||||
|
"total": total,
|
||||||
|
"page": page,
|
||||||
|
"page_size": pageSize,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *LogHandler) GetLoginLogs(c *gin.Context) {
|
func (h *LogHandler) GetLoginLogs(c *gin.Context) {
|
||||||
var req service.ListLoginLogRequest
|
var req service.ListLoginLogRequest
|
||||||
if err := c.ShouldBindQuery(&req); err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,17 +110,21 @@ func (h *LogHandler) GetLoginLogs(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"list": logs,
|
"code": 0,
|
||||||
"total": total,
|
"message": "success",
|
||||||
"page": req.Page,
|
"data": gin.H{
|
||||||
"page_size": req.PageSize,
|
"list": logs,
|
||||||
|
"total": total,
|
||||||
|
"page": req.Page,
|
||||||
|
"page_size": req.PageSize,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *LogHandler) GetOperationLogs(c *gin.Context) {
|
func (h *LogHandler) GetOperationLogs(c *gin.Context) {
|
||||||
var req service.ListOperationLogRequest
|
var req service.ListOperationLogRequest
|
||||||
if err := c.ShouldBindQuery(&req); err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,17 +151,21 @@ func (h *LogHandler) GetOperationLogs(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"list": logs,
|
"code": 0,
|
||||||
"total": total,
|
"message": "success",
|
||||||
"page": req.Page,
|
"data": gin.H{
|
||||||
"page_size": req.PageSize,
|
"list": logs,
|
||||||
|
"total": total,
|
||||||
|
"page": req.Page,
|
||||||
|
"page_size": req.PageSize,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *LogHandler) ExportLoginLogs(c *gin.Context) {
|
func (h *LogHandler) ExportLoginLogs(c *gin.Context) {
|
||||||
var req service.ExportLoginLogRequest
|
var req service.ExportLoginLogRequest
|
||||||
if err := c.ShouldBindQuery(&req); err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ func NewPermissionHandler(permissionService *service.PermissionService) *Permiss
|
|||||||
func (h *PermissionHandler) CreatePermission(c *gin.Context) {
|
func (h *PermissionHandler) CreatePermission(c *gin.Context) {
|
||||||
var req service.CreatePermissionRequest
|
var req service.CreatePermissionRequest
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ func (h *PermissionHandler) CreatePermission(c *gin.Context) {
|
|||||||
func (h *PermissionHandler) ListPermissions(c *gin.Context) {
|
func (h *PermissionHandler) ListPermissions(c *gin.Context) {
|
||||||
var req service.ListPermissionRequest
|
var req service.ListPermissionRequest
|
||||||
if err := c.ShouldBindQuery(&req); err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +63,7 @@ func (h *PermissionHandler) ListPermissions(c *gin.Context) {
|
|||||||
func (h *PermissionHandler) GetPermission(c *gin.Context) {
|
func (h *PermissionHandler) GetPermission(c *gin.Context) {
|
||||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,13 +83,13 @@ func (h *PermissionHandler) GetPermission(c *gin.Context) {
|
|||||||
func (h *PermissionHandler) UpdatePermission(c *gin.Context) {
|
func (h *PermissionHandler) UpdatePermission(c *gin.Context) {
|
||||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var req service.UpdatePermissionRequest
|
var req service.UpdatePermissionRequest
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,7 +109,7 @@ func (h *PermissionHandler) UpdatePermission(c *gin.Context) {
|
|||||||
func (h *PermissionHandler) DeletePermission(c *gin.Context) {
|
func (h *PermissionHandler) DeletePermission(c *gin.Context) {
|
||||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,7 +127,7 @@ func (h *PermissionHandler) DeletePermission(c *gin.Context) {
|
|||||||
func (h *PermissionHandler) UpdatePermissionStatus(c *gin.Context) {
|
func (h *PermissionHandler) UpdatePermissionStatus(c *gin.Context) {
|
||||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,7 +136,7 @@ func (h *PermissionHandler) UpdatePermissionStatus(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,7 +147,7 @@ func (h *PermissionHandler) UpdatePermissionStatus(c *gin.Context) {
|
|||||||
case "disabled", "0":
|
case "disabled", "0":
|
||||||
status = domain.PermissionStatusDisabled
|
status = domain.PermissionStatusDisabled
|
||||||
default:
|
default:
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid status"})
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid status"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ func (h *WebhookHandler) CreateWebhook(c *gin.Context) {
|
|||||||
return
|
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) {
|
func (h *WebhookHandler) ListWebhooks(c *gin.Context) {
|
||||||
@@ -59,11 +59,14 @@ func (h *WebhookHandler) ListWebhooks(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"code": 0,
|
"code": 0,
|
||||||
"data": webhooks,
|
"message": "success",
|
||||||
"total": total,
|
"data": gin.H{
|
||||||
"page": page,
|
"list": webhooks,
|
||||||
"page_size": pageSize,
|
"total": total,
|
||||||
|
"page": page,
|
||||||
|
"page_size": pageSize,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,5 +124,5 @@ func (h *WebhookHandler) GetWebhookDeliveries(c *gin.Context) {
|
|||||||
return
|
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}})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -255,10 +255,8 @@ func TestWebhookHandler_ListWebhooks_Success(t *testing.T) {
|
|||||||
if result["code"].(float64) != 0 {
|
if result["code"].(float64) != 0 {
|
||||||
t.Fatalf("expected code 0, got %v", result["code"])
|
t.Fatalf("expected code 0, got %v", result["code"])
|
||||||
}
|
}
|
||||||
if result["data"] == nil {
|
data := result["data"].(map[string]interface{})
|
||||||
t.Fatal("expected data in response")
|
if data["total"] == nil {
|
||||||
}
|
|
||||||
if result["total"] == nil {
|
|
||||||
t.Fatal("expected total in response")
|
t.Fatal("expected total in response")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -436,15 +434,16 @@ func TestWebhookHandler_ListWebhooks_Pagination(t *testing.T) {
|
|||||||
var result map[string]interface{}
|
var result map[string]interface{}
|
||||||
json.NewDecoder(resp.Body).Decode(&result)
|
json.NewDecoder(resp.Body).Decode(&result)
|
||||||
|
|
||||||
data := result["data"].([]interface{})
|
data := result["data"].(map[string]interface{})
|
||||||
if len(data) != 2 {
|
list := data["list"].([]interface{})
|
||||||
t.Fatalf("expected 2 webhooks per page, got %d", len(data))
|
if len(list) != 2 {
|
||||||
|
t.Fatalf("expected 2 webhooks per page, got %d", len(list))
|
||||||
}
|
}
|
||||||
|
|
||||||
if result["page"].(float64) != 1 {
|
if data["page"].(float64) != 1 {
|
||||||
t.Fatalf("expected page 1, got %v", result["page"])
|
t.Fatalf("expected page 1, got %v", data["page"])
|
||||||
}
|
}
|
||||||
if result["page_size"].(float64) != 2 {
|
if data["page_size"].(float64) != 2 {
|
||||||
t.Fatalf("expected page_size 2, got %v", result["page_size"])
|
t.Fatalf("expected page_size 2, got %v", data["page_size"])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user