update_admin_role.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package admin_role
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. "sghgogs.com/micro/auth-client/response"
  6. "sghgogs.com/micro/auth-client/utils"
  7. pb "sghgogs.com/micro/auth-service/proto"
  8. "sghgogs.com/micro/common"
  9. "strconv"
  10. )
  11. type UpdateRequest struct {
  12. Description string `json:"description"` // 非必填,描述
  13. Users []int64 `json:"users"` // 管理员
  14. Permissions []int64 `json:"permissions"` // 权限
  15. }
  16. // UpdateAdminRole
  17. // @summary 更新角色
  18. // @Description 更新角色内容
  19. // @Tags Role 角色管理
  20. // @Accept json
  21. // @Produce json
  22. // @Param Authorization header string true "Bearer 用户令牌"
  23. // @Param id path int true "角色ID"
  24. // @Param body body UpdateRequest true "请求body"
  25. // @Success 200 {object} response.ApiResponse "成功"
  26. // @Failure 400 {object} response.ApiResponse "请求错误"
  27. // @Failure 500 {object} response.ApiResponse "内部错误"
  28. // @Router /v1/api/admin/role/{roleID} [put]
  29. func (svc *ApiAdminRole) UpdateAdminRole(c *gin.Context) {
  30. var req UpdateRequest
  31. if err := c.BindJSON(&req); err != nil {
  32. c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error()))
  33. return
  34. }
  35. roleID, err := strconv.ParseInt(c.Param("roleID"), 10, 64)
  36. if err != nil || roleID <= 0 {
  37. c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, common.ErrorMessage[common.InvalidRoleID]))
  38. return
  39. }
  40. if len(req.Users) > 0 {
  41. if isBool, err := common.ValidateNumericInt64(req.Users); !isBool {
  42. c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error()))
  43. return
  44. }
  45. }
  46. if len(req.Permissions) > 0 {
  47. if isBool, err := common.ValidateNumericInt64(req.Permissions); !isBool {
  48. c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error()))
  49. return
  50. }
  51. }
  52. ctx, _ := utils.CreateContextWithToken(c, "authorizationservice", "AdminRoleService.UpdateAdminRole")
  53. _, err = svc.Service.UpdateAdminRole(ctx, &pb.UpdateAdminRoleRequest{
  54. RoleId: roleID,
  55. Description: req.Description,
  56. Users: req.Users,
  57. Permissions: req.Permissions,
  58. })
  59. if err != nil {
  60. code, mgs := response.MicroErrorRequest(err)
  61. c.JSON(code, response.ErrorResponse(code, mgs))
  62. return
  63. }
  64. c.JSON(http.StatusOK, response.SuccessResponse(nil))
  65. }