using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.UserWalletCQ.Commands.DeleteUserWallet;
public class DeleteUserWalletCommandHandler : IRequestHandler<DeleteUserWalletCommand, Unit>
{
    private readonly IApplicationDbContext _context;

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

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