using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.UserWalletHistoryCQ.Commands.CreateNewUserWalletHistory;
public class CreateNewUserWalletHistoryCommandHandler : IRequestHandler<CreateNewUserWalletHistoryCommand, CreateNewUserWalletHistoryResponseDto>
{
    private readonly IApplicationDbContext _context;

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

    public async Task<CreateNewUserWalletHistoryResponseDto> Handle(CreateNewUserWalletHistoryCommand request,
        CancellationToken cancellationToken)
    {
        var entity = request.Adapt<UserWalletHistory>();
        await _context.UserWalletHistories.AddAsync(entity, cancellationToken);
        entity.AddDomainEvent(new CreateNewUserWalletHistoryEvent(entity));
        await _context.SaveChangesAsync(cancellationToken);
        return entity.Adapt<CreateNewUserWalletHistoryResponseDto>();
    }
}
