1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- package admin_role
- import (
- "github.com/gin-gonic/gin"
- "net/http"
- "sghgogs.com/sghblog/authorization-client/response"
- "sghgogs.com/sghblog/authorization-client/utils"
- pb "sghgogs.com/sghblog/authorization-service/proto"
- "sghgogs.com/sghblog/common"
- )
- type CreateAdminRoleRequest struct {
- Name string `json:"name" binding:"required,max=15,min=3"` // 角色名称
- Description string `json:"description"` // 非必填,描述
- Users []int64 `json:"users"` // 管理员
- Permissions []int64 `json:"permissions"` // 权限
- }
- // CreateAdminRole
- // @summary 新增角色
- // @Description 创建角色
- // @Tags Role 角色管理
- // @Accept json
- // @Produce json
- // @Param Authorization header string true "Bearer 用户令牌"
- // @Param body body CreateAdminRoleRequest true "请求body"
- // @Success 200 {object} response.ApiResponse "成功"
- // @Failure 400 {object} response.ApiResponse "请求错误"
- // @Failure 500 {object} response.ApiResponse "内部错误"
- // @Router /v1/api/admin/role [post]
- func (svc *ApiAdminRole) CreateAdminRole(c *gin.Context) {
- var req CreateAdminRoleRequest
- if err := c.BindJSON(&req); err != nil {
- c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error()))
- return
- }
- if len(req.Permissions) > 0 {
- if isBool, err := common.ValidateNumericInt64(req.Permissions); !isBool {
- c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error()))
- return
- }
- }
- if len(req.Users) > 0 {
- if isBool, err := common.ValidateNumericInt64(req.Users); !isBool {
- c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error()))
- return
- }
- }
- ctx, _ := utils.CreateContextWithToken(c, "authorizationservice", "AdminRoleService.CreateAdminRole")
- _, err := svc.Service.CreateAdminRole(ctx, &pb.CreateAdminRoleRequest{
- Name: req.Name,
- Description: req.Description,
- Users: req.Users,
- Permissions: req.Permissions,
- })
- if err != nil {
- code, mgs := response.MicroErrorRequest(err)
- c.JSON(code, response.ErrorResponse(code, mgs))
- return
- }
- c.JSON(http.StatusOK, response.SuccessResponse(nil))
- }
|