MyBookmark/Modules.User.Application/Commands/Access/RoleDeleteCommand.cs

26 lines
893 B
C#

using MediatR;
using Modules.User.Application.Gateways;
namespace Modules.User.Application.Commands.Access;
public class DeleteRoleCommand : IRequest<bool>
{
public int RoleId { get; init; }
}
public class DeleteRoleCommandHandler(IRoleRepository roleRepository, IAccountRepository accountRepository,
IUnitOfWork uow) : IRequestHandler<DeleteRoleCommand, bool>
{
public async Task<bool> 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;
}
}