public RoleCreatedConfirmation CreateRole(Role role) { //TODO: Unique if (!_roleManager.RoleExistsAsync(role.RoleName).Result) { RoleCreatedConfirmation createdRole = _roleRepository.CreateRole(role); _roleRepository.SaveChanges(); AccountRole accRole = new AccountRole(createdRole.RoleId, role.RoleName, role.RoleName); _roleManager.CreateAsync(accRole).Wait(); return(createdRole); } throw new UniqueValueViolationException("Role name should be unique"); }
public ActionResult <RoleCreatedConfirmationDto> CreateRole([FromBody] RoleCreationDto role) { try { Role newRole = _mapper.Map <Role>(role); RoleCreatedConfirmation createdRole = _rolesService.CreateRole(newRole); var location = _linkGenerator.GetPathByAction("GetRoleById", "Role", new { roleId = createdRole.RoleId }); return(Created(location, _mapper.Map <RoleCreatedConfirmationDto>(createdRole))); } catch (Exception ex) { if (ex.GetBaseException().GetType() == typeof(UniqueValueViolationException)) { return(StatusCode(StatusCodes.Status409Conflict, ex.Message)); } return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message)); } }