using MediatR; using Modules.User.Application.Gateways; namespace Modules.User.Application.Commands.Access; public class DeleteRoleCommand : IRequest { public int RoleId { get; init; } } public class DeleteRoleCommandHandler(IRoleRepository roleRepository, IAccountRepository accountRepository, IUnitOfWork uow) : IRequestHandler { public async Task Handle(DeleteRoleCommand request, CancellationToken cancellationToken) { if (await accountRepository.ExistsWithRoleAsync(request.RoleId, cancellationToken)) { //TODO throw new InvalidOperationException("Role is assigned to accounts and cannot be deleted."); return false; } await roleRepository.DeleteAsync(request.RoleId, cancellationToken); await uow.SaveChangesAsync(cancellationToken); return true; } }