using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.UserWalletChangeLogCQ.Commands.CreateNewUserWalletChangeLog;
public class CreateNewUserWalletChangeLogCommandHandler : IRequestHandler<CreateNewUserWalletChangeLogCommand, CreateNewUserWalletChangeLogResponseDto>
{
    private readonly IApplicationDbContext _context;

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

    public async Task<CreateNewUserWalletChangeLogResponseDto> Handle(CreateNewUserWalletChangeLogCommand request,
        CancellationToken cancellationToken)
    {
        var entity = request.Adapt<UserWalletChangeLog>();
        await _context.UserWalletChangeLogs.AddAsync(entity, cancellationToken);
        entity.AddDomainEvent(new CreateNewUserWalletChangeLogEvent(entity));
        await _context.SaveChangesAsync(cancellationToken);
        return entity.Adapt<CreateNewUserWalletChangeLogResponseDto>();
    }
}
