using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.UserWalletCQ.Commands.CreateNewUserWallet;
public class CreateNewUserWalletCommandHandler : IRequestHandler<CreateNewUserWalletCommand, CreateNewUserWalletResponseDto>
{
    private readonly IApplicationDbContext _context;

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

    public async Task<CreateNewUserWalletResponseDto> Handle(CreateNewUserWalletCommand request,
        CancellationToken cancellationToken)
    {
        var entity = request.Adapt<UserWallet>();
        await _context.UserWallets.AddAsync(entity, cancellationToken);
        entity.AddDomainEvent(new CreateNewUserWalletEvent(entity));
        await _context.SaveChangesAsync(cancellationToken);
        return entity.Adapt<CreateNewUserWalletResponseDto>();
    }
}
