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.
This commit is contained in:
@@ -23,7 +23,7 @@ func NewCustomFieldHandler(customFieldService *service.CustomFieldService) *Cust
|
|||||||
func (h *CustomFieldHandler) CreateField(c *gin.Context) {
|
func (h *CustomFieldHandler) CreateField(c *gin.Context) {
|
||||||
var req service.CreateFieldRequest
|
var req service.CreateFieldRequest
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,13 +44,13 @@ func (h *CustomFieldHandler) CreateField(c *gin.Context) {
|
|||||||
func (h *CustomFieldHandler) UpdateField(c *gin.Context) {
|
func (h *CustomFieldHandler) UpdateField(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 field id"})
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid field id"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var req service.UpdateFieldRequest
|
var req service.UpdateFieldRequest
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,7 +71,7 @@ func (h *CustomFieldHandler) UpdateField(c *gin.Context) {
|
|||||||
func (h *CustomFieldHandler) DeleteField(c *gin.Context) {
|
func (h *CustomFieldHandler) DeleteField(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 field id"})
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid field id"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +90,7 @@ func (h *CustomFieldHandler) DeleteField(c *gin.Context) {
|
|||||||
func (h *CustomFieldHandler) GetField(c *gin.Context) {
|
func (h *CustomFieldHandler) GetField(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 field id"})
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid field id"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,7 +126,7 @@ func (h *CustomFieldHandler) ListFields(c *gin.Context) {
|
|||||||
func (h *CustomFieldHandler) SetUserFieldValues(c *gin.Context) {
|
func (h *CustomFieldHandler) SetUserFieldValues(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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,7 +135,7 @@ func (h *CustomFieldHandler) SetUserFieldValues(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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,7 +154,7 @@ func (h *CustomFieldHandler) SetUserFieldValues(c *gin.Context) {
|
|||||||
func (h *CustomFieldHandler) GetUserFieldValues(c *gin.Context) {
|
func (h *CustomFieldHandler) GetUserFieldValues(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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ func NewRoleHandler(roleService *service.RoleService) *RoleHandler {
|
|||||||
func (h *RoleHandler) CreateRole(c *gin.Context) {
|
func (h *RoleHandler) CreateRole(c *gin.Context) {
|
||||||
var req service.CreateRoleRequest
|
var req service.CreateRoleRequest
|
||||||
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 *RoleHandler) CreateRole(c *gin.Context) {
|
|||||||
func (h *RoleHandler) ListRoles(c *gin.Context) {
|
func (h *RoleHandler) ListRoles(c *gin.Context) {
|
||||||
var req service.ListRoleRequest
|
var req service.ListRoleRequest
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,11 +54,11 @@ func (h *RoleHandler) ListRoles(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"code": 0,
|
"code": 0,
|
||||||
"message": "success",
|
"message": "success",
|
||||||
"data": gin.H{
|
"data": gin.H{
|
||||||
"items": roles,
|
"items": roles,
|
||||||
"total": total,
|
"total": total,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -66,7 +66,7 @@ func (h *RoleHandler) ListRoles(c *gin.Context) {
|
|||||||
func (h *RoleHandler) GetRole(c *gin.Context) {
|
func (h *RoleHandler) GetRole(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 role id"})
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid role id"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,13 +86,13 @@ func (h *RoleHandler) GetRole(c *gin.Context) {
|
|||||||
func (h *RoleHandler) UpdateRole(c *gin.Context) {
|
func (h *RoleHandler) UpdateRole(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 role id"})
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid role id"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var req service.UpdateRoleRequest
|
var req service.UpdateRoleRequest
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,7 +112,7 @@ func (h *RoleHandler) UpdateRole(c *gin.Context) {
|
|||||||
func (h *RoleHandler) DeleteRole(c *gin.Context) {
|
func (h *RoleHandler) DeleteRole(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 role id"})
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid role id"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +130,7 @@ func (h *RoleHandler) DeleteRole(c *gin.Context) {
|
|||||||
func (h *RoleHandler) UpdateRoleStatus(c *gin.Context) {
|
func (h *RoleHandler) UpdateRoleStatus(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 role id"})
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid role id"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,7 +139,7 @@ func (h *RoleHandler) UpdateRoleStatus(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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,7 +150,7 @@ func (h *RoleHandler) UpdateRoleStatus(c *gin.Context) {
|
|||||||
case "disabled", "0":
|
case "disabled", "0":
|
||||||
status = domain.RoleStatusDisabled
|
status = domain.RoleStatusDisabled
|
||||||
default:
|
default:
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid status"})
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid status"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,7 +169,7 @@ func (h *RoleHandler) UpdateRoleStatus(c *gin.Context) {
|
|||||||
func (h *RoleHandler) GetRolePermissions(c *gin.Context) {
|
func (h *RoleHandler) GetRolePermissions(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 role id"})
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid role id"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,7 +189,7 @@ func (h *RoleHandler) GetRolePermissions(c *gin.Context) {
|
|||||||
func (h *RoleHandler) AssignPermissions(c *gin.Context) {
|
func (h *RoleHandler) AssignPermissions(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 role id"})
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid role id"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -198,7 +198,7 @@ func (h *RoleHandler) AssignPermissions(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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user