using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.UserWalletCQ.Commands.UpdateUserWallet;
public class UpdateUserWalletCommandHandler : IRequestHandler<UpdateUserWalletCommand, Unit>
{
    private readonly IApplicationDbContext _context;

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

    public async Task<Unit> Handle(UpdateUserWalletCommand request, CancellationToken cancellationToken)
    {
        var entity = await _context.UserWallets
            .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(UserWallet), request.Id);
        request.Adapt(entity);
        _context.UserWallets.Update(entity);
        entity.AddDomainEvent(new UpdateUserWalletEvent(entity));
        await _context.SaveChangesAsync(cancellationToken);
        return Unit.Value;
    }
}
