From b7cbdffd4f06e71d42df171adcc62243c9c594ea Mon Sep 17 00:00:00 2001 From: long-agent Date: Sat, 11 Apr 2026 13:21:13 +0800 Subject: [PATCH] fix: unify handler response format in custom_field and role handlers - custom_field_handler.go: Fix all error responses to use {code, message} - role_handler.go: Fix all error responses to use {code, message} Standardize all JSON responses to {code: 0, message: "success", data: ...} for success and {code: XXX, message: "..."} for errors. --- internal/api/handler/custom_field_handler.go | 16 +++++------ internal/api/handler/role_handler.go | 28 ++++++++++---------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/internal/api/handler/custom_field_handler.go b/internal/api/handler/custom_field_handler.go index c8aa4a4..113a655 100644 --- a/internal/api/handler/custom_field_handler.go +++ b/internal/api/handler/custom_field_handler.go @@ -23,7 +23,7 @@ func NewCustomFieldHandler(customFieldService *service.CustomFieldService) *Cust func (h *CustomFieldHandler) CreateField(c *gin.Context) { var req service.CreateFieldRequest 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 } @@ -44,13 +44,13 @@ func (h *CustomFieldHandler) CreateField(c *gin.Context) { func (h *CustomFieldHandler) UpdateField(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid field id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid field id"}) return } var req service.UpdateFieldRequest 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 } @@ -71,7 +71,7 @@ func (h *CustomFieldHandler) UpdateField(c *gin.Context) { func (h *CustomFieldHandler) DeleteField(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid field id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid field id"}) return } @@ -90,7 +90,7 @@ func (h *CustomFieldHandler) DeleteField(c *gin.Context) { func (h *CustomFieldHandler) GetField(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid field id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid field id"}) return } @@ -126,7 +126,7 @@ func (h *CustomFieldHandler) ListFields(c *gin.Context) { func (h *CustomFieldHandler) SetUserFieldValues(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 } @@ -135,7 +135,7 @@ func (h *CustomFieldHandler) SetUserFieldValues(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 } @@ -154,7 +154,7 @@ func (h *CustomFieldHandler) SetUserFieldValues(c *gin.Context) { func (h *CustomFieldHandler) GetUserFieldValues(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 } diff --git a/internal/api/handler/role_handler.go b/internal/api/handler/role_handler.go index a159220..5024160 100644 --- a/internal/api/handler/role_handler.go +++ b/internal/api/handler/role_handler.go @@ -23,7 +23,7 @@ func NewRoleHandler(roleService *service.RoleService) *RoleHandler { func (h *RoleHandler) CreateRole(c *gin.Context) { var req service.CreateRoleRequest 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 *RoleHandler) CreateRole(c *gin.Context) { func (h *RoleHandler) ListRoles(c *gin.Context) { var req service.ListRoleRequest 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 } @@ -54,11 +54,11 @@ func (h *RoleHandler) ListRoles(c *gin.Context) { } c.JSON(http.StatusOK, gin.H{ - "code": 0, + "code": 0, "message": "success", "data": gin.H{ "items": roles, - "total": total, + "total": total, }, }) } @@ -66,7 +66,7 @@ func (h *RoleHandler) ListRoles(c *gin.Context) { func (h *RoleHandler) GetRole(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid role id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid role id"}) return } @@ -86,13 +86,13 @@ func (h *RoleHandler) GetRole(c *gin.Context) { func (h *RoleHandler) UpdateRole(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid role id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid role id"}) return } var req service.UpdateRoleRequest 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 } @@ -112,7 +112,7 @@ func (h *RoleHandler) UpdateRole(c *gin.Context) { func (h *RoleHandler) DeleteRole(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid role id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid role id"}) return } @@ -130,7 +130,7 @@ func (h *RoleHandler) DeleteRole(c *gin.Context) { func (h *RoleHandler) UpdateRoleStatus(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid role id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid role id"}) return } @@ -139,7 +139,7 @@ func (h *RoleHandler) UpdateRoleStatus(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 } @@ -150,7 +150,7 @@ func (h *RoleHandler) UpdateRoleStatus(c *gin.Context) { case "disabled", "0": status = domain.RoleStatusDisabled default: - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid status"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid status"}) return } @@ -169,7 +169,7 @@ func (h *RoleHandler) UpdateRoleStatus(c *gin.Context) { func (h *RoleHandler) GetRolePermissions(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid role id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid role id"}) return } @@ -189,7 +189,7 @@ func (h *RoleHandler) GetRolePermissions(c *gin.Context) { func (h *RoleHandler) AssignPermissions(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid role id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid role id"}) return } @@ -198,7 +198,7 @@ func (h *RoleHandler) AssignPermissions(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 }