示例#1
0
        public async Task <IActionResult> AddNewRole([FromBody] CreateRoleRequestModel roleCreateRequestModel)
        {
            try
            {
                System.Threading.Thread.Sleep(1000);
                var roleExists = await _employeeRoleRespository.RoleExists(roleCreateRequestModel.RoleName);

                if (roleExists)
                {
                    return(BadRequest(new ApiResponseBadRequestResult()
                    {
                        ErrorMessage = $"Role {roleCreateRequestModel.RoleName} already exists"
                    }));
                }

                var newRole = await _employeeRoleRespository.AddRole(_mapper.Map <EmployeeRole>(roleCreateRequestModel));

                return(Ok(new ApiResponseOKResult()
                {
                    Data = newRole
                }));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Failed to create new role");
                return(StatusCode((int)HttpStatusCode.InternalServerError, new ApiResponseFailure()
                {
                    ErrorMessage = "Failed to create new role"
                }));
            }
        }
示例#2
0
        public async Task <BaseResponse> CreateRoleAsync(CreateRoleRequestModel model)
        {
            var roleExists = await _roleRepository.ExistsAsync(u => u.Name == model.Name);

            if (roleExists)
            {
                throw new BadRequestException($"Role with name '{model.Name}' already exists.");
            }

            var role = new Role
            {
                Id          = Guid.NewGuid(),
                Name        = model.Name,
                Description = model.Description
            };

            await _roleRepository.AddAsync(role);

            await _roleRepository.SaveChangesAsync();

            return(new BaseResponse
            {
                Status = true,
                Message = "Role created successfully"
            });
        }
示例#3
0
        public async Task <IHttpActionResult> Create(CreateRoleRequestModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            CustomRole role = new CustomRole {
                Id = GuidUtility.GetNewSequentialGuid(), Name = model.Name
            };

            var result = await _applicationRoleManager.CreateAsync(role);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }

            return(CreatedAtRoute(UriName.Identity.Roles.GET_ROLE, new { id = role.Id }, _identityRoleResponseFactory.Create(role)));
        }
示例#4
0
        public async Task <IActionResult> Create([FromBody] CreateRoleRequestModel model)
        {
            var response = await _roleService.CreateRoleAsync(model);

            return(Ok(response));
        }