Пример #1
0
        public async Task <JsonResult> AddNew([FromBody] InputRoleModel inputRoleModel)
        {
            if (inputRoleModel.Name.IsNullOrWhiteSpace())
            {
                return(AjaxHelper.JsonResult(HttpStatusCode.BadRequest, "角色名不能为空"));
            }
            if (inputRoleModel.Permissions.Count <= 0)
            {
                return(AjaxHelper.JsonResult(HttpStatusCode.BadRequest, "角色必须具有至少一种权限"));
            }
            string token   = HttpContext.Request.Headers["token"];
            var    userId  = (await LoginManager.GetUserIdAsync(token)).GetValueOrDefault();
            var    roleDto = new RoleDto
            {
                Name          = inputRoleModel.Name,
                Description   = inputRoleModel.Description,
                CreatorUserId = userId,
                Permissions   = inputRoleModel.Permissions.Select(i => new PermissionDto
                {
                    Name          = i.Name,
                    Description   = i.Description,
                    CreatorUserId = userId
                }).ToList()
            };
            var id = await _roleService.InsertAndGetIdAsync(roleDto);

            return(AjaxHelper.JsonResult(HttpStatusCode.OK, "成功", id));
        }
Пример #2
0
        public async Task <JsonResult> UpdateRoleInfo([FromBody] InputRoleModel inputRoleModel)
        {
            if (inputRoleModel.RoleId == null || inputRoleModel.RoleId.Value <= 0)
            {
                return(AjaxHelper.JsonResult(HttpStatusCode.BadRequest, "角色Id不能为空"));
            }
            if (inputRoleModel.Name.IsNullOrWhiteSpace())
            {
                return(AjaxHelper.JsonResult(HttpStatusCode.BadRequest, "角色名不能为空"));
            }
            var roleDto = new RoleDto
            {
                Id          = inputRoleModel.RoleId.Value,
                Name        = inputRoleModel.Name,
                Description = inputRoleModel.Description,
            };
            await _roleService.UpdateAsync(roleDto);

            return(AjaxHelper.JsonResult(HttpStatusCode.OK, "成功"));
        }