34 lines
1.1 KiB
C#
34 lines
1.1 KiB
C#
using AutoMapper;
|
|
using MediatR;
|
|
using ExpenseTracker.Application.Common.Exceptions;
|
|
using ExpenseTracker.Application.Common.Interfaces;
|
|
using ExpenseTracker.Application.Transactions.Commands.Delete;
|
|
|
|
namespace ExpenseTracker.Application.Transactiones.Commands.Delete;
|
|
|
|
public class DeleteTransactionCommandHandler : IRequestHandler<DeleteTransactionCommand>
|
|
{
|
|
private readonly IMapper _mapper;
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
|
|
public DeleteTransactionCommandHandler(IMapper mapper, IUnitOfWork unitOfWork)
|
|
{
|
|
_mapper = mapper;
|
|
_unitOfWork = unitOfWork;
|
|
}
|
|
|
|
public async Task Handle(DeleteTransactionCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var isEntityPresentInDatabase = _unitOfWork.TransactionRepository.Queryable.Any(e => e.Id == request.Id);
|
|
|
|
if (!isEntityPresentInDatabase)
|
|
{
|
|
throw new NotFoundException();
|
|
}
|
|
|
|
await _unitOfWork.TransactionRepository.DeleteOneAsync(request.Id, cancellationToken);
|
|
|
|
await _unitOfWork.SaveAsync(cancellationToken);
|
|
}
|
|
}
|