32 lines
1.1 KiB
C#
32 lines
1.1 KiB
C#
using MediatR;
|
|
using ExpenseTracker.Application.Common.Exceptions;
|
|
using ExpenseTracker.Application.Accounts.Commands.Delete;
|
|
using ExpenseTracker.Application.Common.Interfaces;
|
|
|
|
namespace ExpenseTracker.Application.Accountes.Commands.Delete;
|
|
|
|
public class DeleteAccountCommandHandler : IRequestHandler<DeleteAccountCommand>
|
|
{
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
|
|
public DeleteAccountCommandHandler(IUnitOfWork unitOfWork)
|
|
{
|
|
_unitOfWork = unitOfWork;
|
|
}
|
|
|
|
public async Task Handle(DeleteAccountCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var isEntityPresentInDatabase = _unitOfWork.AccountRepository.Queryable.Any(e => e.Id == request.Id);
|
|
|
|
if (!isEntityPresentInDatabase)
|
|
{
|
|
throw new NotFoundException();
|
|
}
|
|
|
|
await _unitOfWork.AccountRepository.DeleteOneAsync(request.Id, cancellationToken);
|
|
await _unitOfWork.TransactionRepository.DeleteManyAsync(e => e.AccountId.Equals(request.Id), cancellationToken);
|
|
|
|
await _unitOfWork.SaveAsync(cancellationToken);
|
|
}
|
|
}
|