public async Task <IActionResult> CreateRole(CreateRoleDTO model) { if (ModelState.IsValid) { // We just need to specify a unique role name to create a new role IdentityRole identityRole = new IdentityRole { Name = model.RoleName }; // Saves the role in the underlying AspNetRoles table IdentityResult result = await _roleManager.CreateAsync(identityRole); if (result.Succeeded) { return(RedirectToRoute("Roles")); } foreach (IdentityError error in result.Errors) { ModelState.AddModelError("", error.Description); } } return(Ok(model)); }
/// <summary> /// Creates the role. /// </summary> /// <param name="roleDetails">The role details.</param> /// <returns></returns> public async Task <CSSResponse> CreateRole(CreateRoleDTO roleDetails) { var roles = await _repository.Roles.GetRolesByName(new RoleNameDetails { Name = roleDetails.Name }); if (roles?.Count > 0) { return(new CSSResponse($"role with Role Name '{roleDetails.Name}' already exists.", HttpStatusCode.Conflict)); } var roleRequest = _mapper.Map <Role>(roleDetails); _repository.Roles.CreateRole(roleRequest); await _repository.SaveAsync(); //_bus.SendCommand<CreateroleCommand>(MassTransitConstants.roleCreateCommandRouteKey, // new // { // Id = roleRequest.Id, // Sso = roleRequest.Sso, // EmployeeId = roleRequest.EmployeeId, // Firstname = roleRequest.Firstname, // Lastname = roleRequest.Lastname, // ModifiedDate = roleRequest.ModifiedDate // }); return(new CSSResponse(new RoleNameDetails { Name = roleRequest.Name }, HttpStatusCode.Created)); //return new CSSResponse(roles, HttpStatusCode.Created); }
public static Roles AsModel(this CreateRoleDTO role) { return(new Roles { RoleType = role.RoleType }); }
public async Task <ActionResult <RoleDTO> > UpdateRole(int id, [FromBody] CreateRoleDTO saveRoleResource) { var validator = new CreateRoleResourceValidator(); var validationResult = await validator.ValidateAsync(saveRoleResource); if (!validationResult.IsValid) { return(BadRequest(validationResult.Errors)); // this needs refining, but for demo it is ok } var roleToBeUpdated = await _roleService.GetRoleById(id); if (roleToBeUpdated == null) { return(NotFound()); } var role = _mapper.Map <CreateRoleDTO, Role>(saveRoleResource); await _roleService.UpdateRole(roleToBeUpdated, role); var updatedRole = await _roleService.GetRoleById(id); var updatedRoleResource = _mapper.Map <Role, RoleDTO>(updatedRole); return(Ok(updatedRoleResource)); }
public async Task <bool> CreateAsync([FromBody] CreateRoleDTO dto) { var entity = new Role(dto.Name, dto.Type, dto.Description); entity.SetCreationAudited(Session.UserId, Session.UserName); entity.SetModificationAudited(null, null, DateTimeOffset.Now); await _dbContext.Roles.AddAsync(entity); await _dbContext.SaveChangesAsync(); return(true); }
public async Task <RoleDTO> CreateRole(CreateRoleDTO role) { if (role.RoleType is null) { throw new Exception("RoleType cannot be empty"); } Roles roles = role.AsModel(); await _context.Roles.AddAsync(roles); await _context.SaveChangesAsync(); return(roles.AsDTO()); }
public async Task <ActionResult> AddRole([FromBody] CreateRoleDTO roleDto) { try { await _adminServices.CreateRole(roleDto); return(Ok()); } catch (ApplicationException ex) { return(BadRequest(new { message = ex.Message })); } }
public async Task CreateRole(CreateRoleDTO roleDto) { IdentityRole identityRole = new IdentityRole { Name = roleDto.RoleName }; IdentityResult result = await _roleManager.CreateAsync(identityRole); if (result.Succeeded) { _logger.LogInformation("Role added successfully"); } }
public async Task <ActionResult <RoleDTO> > CreateRole([FromBody] CreateRoleDTO createRoleResource) { var validator = new CreateRoleResourceValidator(); var validationResult = await validator.ValidateAsync(createRoleResource); if (!validationResult.IsValid) { return(BadRequest(validationResult.Errors)); // this needs refining, but for demo it is ok } var roleToCreate = _mapper.Map <CreateRoleDTO, Role>(createRoleResource); var newRole = await _roleService.CreateRole(roleToCreate); var role = await _roleService.GetRoleById(newRole.Id); var roleResource = _mapper.Map <Role, RoleDTO>(role); return(Ok(roleResource)); }
public async Task <IActionResult> CreateRole(CreateRoleDTO role) { try { var createrole = await _adminservice.CreateRole(role); return(CreatedAtAction(nameof(GetRole), new { id = createrole.Id }, createrole)); } catch (Exception e) { if (e.Message.Length > 0) { return(BadRequest(e.Message)); } else { throw; } } }
public async Task <IActionResult> CreateRole(CreateRoleDTO model) { if (ModelState.IsValid) { IdentityRole identityRole = new IdentityRole { Name = model.RoleName }; IdentityResult result = await roleManager.CreateAsync(identityRole); if (result.Succeeded) { return(RedirectToAction("ListRoles", "Empleados")); } else { foreach (IdentityError error in result.Errors) { ModelState.AddModelError("", error.Description); } } } return(View(model)); }
public async Task <IActionResult> CreateRole([FromBody] CreateRoleDTO roleDetails) { var result = await _roleService.CreateRole(roleDetails); return(StatusCode((int)result.Code, result.Value)); }