public Task <long> DeletePerfil(DeletePerfilCommand command) { var requestUrl = CreateRequestUri(string.Format(System.Globalization.CultureInfo.InvariantCulture, $"{ResourcePerfil}/DeletePerfil")); return(Post(requestUrl, command)); }
public async Task <ActionResult <bool> > DeletePerfil(DeletePerfilCommand command) { try { var obj = _db.Roles.Where(x => x.Id == command.CodPerfil).FirstOrDefault(); var usersInRole = _db.UserRoles.Where(u => u.RoleId == command.CodPerfil).Select(s => s.UserId); foreach (var id in usersInRole) { var user = await _userManager.FindByIdAsync(id); if (user != null) { var userRoles = await _userManager.GetRolesAsync(new WebProfileUser { Id = user.Id }); var rsRemove = await _userManager.RemoveFromRoleAsync(user, userRoles.FirstOrDefault()); if (!rsRemove.Succeeded) { return(null); } var userRole = _db.Roles.FirstOrDefault(x => x.Name.ToUpper() == command.NomPerfilUsuarioPublico.ToUpper()); await _userManager.AddToRoleAsync(user, userRole.Name); var usuario = await Mediator.Send(new GetUsuarioByIdQuery { Id = user.Id }); var commandUsu = new UpdateUsuarioCommand { NomUsuario = user.Nome, NumTelefone = user.Telefone, CodEmpresa = usuario.CodEmpresa, CodUnidadeInfraestrutura = usuario.CodUnidadeInfraestrutura, CodPerfil = userRole.Id, NomPerfil = userRole.Name, }; var resultUsu = await Mediator.Send(commandUsu); } } var result = await _roleManager.DeleteAsync(obj); return(result.Succeeded); } catch (Exception e) { Console.WriteLine(e); throw; } }