public async Task <IActionResult> DeletePermissionRole([FromRoute] Guid id, [FromRoute] Guid permissionId) { var command = new RemovePermissionToRoleCommand { RoleId = id, PermissionId = permissionId }; await _mediator.Send(command); return(Ok()); }
public async Task <IActionResult> DeletePermissionRole(RemovePermissionToRoleCommand command) { try { await _mediator.Send(command); return(Ok()); } catch (KeyNotFoundException ex) { return(NotFound()); } catch (ArgumentException argumentException) { return(BadRequest(argumentException.Message)); } }
public async Task <bool> Handle(RemovePermissionToRoleCommand message, CancellationToken cancellationToken) { var role = await _roleManager.FindByIdAsync(message.RoleId.ToString()) ?? throw new KeyNotFoundException(); var permission = await _permissionRepository.GetById(message.PermissionId) ?? throw new KeyNotFoundException(); if (role.Permissions.Any(pname => pname == permission.Name)) { role.Permissions.Remove(permission.Name); } else { throw new KeyNotFoundException("Permission not found in this role"); } await _roleManager.UpdateAsync(role); return(true); }