using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.TransactionsCQ.Commands.CreateNewTransactions;
public class CreateNewTransactionsCommandHandler : IRequestHandler<CreateNewTransactionsCommand, CreateNewTransactionsResponseDto>
{
    private readonly IApplicationDbContext _context;

    public CreateNewTransactionsCommandHandler(IApplicationDbContext context)
    {
        _context = context;
    }

    public async Task<CreateNewTransactionsResponseDto> Handle(CreateNewTransactionsCommand request,
        CancellationToken cancellationToken)
    {
        var entity = request.Adapt<Transaction>();
        await _context.Transactions.AddAsync(entity, cancellationToken);
        entity.AddDomainEvent(new CreateNewTransactionsEvent(entity));
        await _context.SaveChangesAsync(cancellationToken);
        return entity.Adapt<CreateNewTransactionsResponseDto>();
    }
}
