All right.
And up next, let's set up the pagination.
Now, even though there's not that much code, it's quite complex topic.
And therefore, I'll do everything step by step.
So for starters, let's see how we can get all of the jobs we're returning.
And if you're wondering where we're going to use that on the front end notice over here.
So yes, we'll display only ten jobs per page, but notice we can clearly see that total.
There are 75 jobs in this example.
And the way we do that, we go here with some kind of variable.
My case.
I'm going to call this total jobs.
And we want to go with Await, then Job and then count documents.
And in here we want to provide the query object and as a result, we'll get back total jobs.
Now, I will place this one before jobs since it's going to be easier to see in the Thunder client.
So let me go to my Json and I'll say total jobs and then comma.
Now let's navigate back over here and I think I'm going to stick with some kind of search query because
in my opinion, it's going to be easier to showcase the skip and limit something we're going to cover
in a second with less amount of jobs.
So if I go back to empty search term, I'm going to get back 19, which basically matches the job status
as well as the job type.
And this is definitely something that we can use when we set up the pagination.
But for now, when we're just discussing Skip and Limit, I'm going to provide the search as as because
then I'm going to get back three jobs and effectively.
Just like sort we can chain skip method and limit method.
Now, as far as the limit, it just limits the amount of jobs we're returning.
But again, it's not going to change the total jobs.
And this is super, super important.
So let's navigate back to job controller and where we have over here the job find basically where we're
looking for all of the jobs right after sort.
I'll also say limit and it's pretty straightforward.
Essentially we just provide some kind of number value.
So at the moment I have three jobs, correct.
So let's just provide a value of one to a limit and let's see what we're going to get back and let's
focus on the company value.
I think it's going to be easier.
So the second one over here is the flash dog.
And then I have something starting with S as my first job.
So let me go with Limit.
Let's provide one and let's make the same request.
So notice we're not changing anything in the Thunder client.
And now of course I only have one company.
But again, total jobs is three super, super important.
So we didn't change the amount of total jobs.
When it comes to total jobs, we're changing by providing different values to job status, the search,
job type and all that.
But not when we're changing the limit.
This is just a response.
So in our response, we're returning only one.
Now, if I'm going to change this and let's say set it equal to two.
You can probably already guess that we'll get back these two job values.
And there's also a skip where effectively we can just skip the jobs in our response.
So again, please focus on the company value since it's going to be.
Easier to understand if we provide, for example, skip of one.
I'm going to get back still those two jobs since my limit is two, but we'll skip the first job in our
array.
How was that going to look like?
Well, let's go over here.
Dot, Skip.
And like I said, for now, let's just skip one.
Let's navigate back.
Let's send it and check it out.
Now I have the second and the third job in my original array.
Hopefully that is clear.
So now we want to set up some functionality where of course we're going to calculate these both values
based on something.
So for now, let's just remove we're not going to set anything here.
We'll leave both of them empty.
And yes, basically in my case, I'm going to do it right after the sort key and all that.
Let me add here first a comment I'll say set up.
Pagination.
And then for starters, let's create a page variable.
And remember, when it comes to the query parameters, they are going to be strings.
But since we want to do the math operation, I want to right away turn it into a number and therefore
I'm going to go with number.
Again, this is straight up JavaScript.
Then we're going to go with rec, dot, query and page, which of course eventually will provide in
our request.
So I'm expecting that the value is going to be there.
If it's not there, I'm going to go with one.
So by default, we'll always look for the first page.
Then second one is going to be the limit.
And even though I will look for the limit in the rec dot query, please keep in mind that in this project
we're not going to utilize it.
So it's always going to default to whatever value I'm going to use.
So in my case, that is going to be ten.
Now this is a nice challenge that you can set it up.
You'll have all of the backend logic in place in a second and the only thing you need to change of course,
is the front end where you'll need to implement that limit option.
So in here, let's go with limit.
Again, we're going to go with number.
But like I just said during the project, from the front end, we're not going to pass it.
So it will always, always.
Default back to ten.
So by default, page is going to be one.
And yes, we will change these page values.
And when it comes to limit, we'll always have ten jobs per page.
And here comes the main logic.
We'll go with const.
And in this case I'm going to call this Skip and then we'll go with page minus one times the limit.
So let me kind of explain how everything works.
Let's imagine that you're requesting the first page.
What is going to be the value?
Well, it's going to be one over here.
Right.
And then as far as the limit, we're going to go with ten.
So for the first page, we're going to go with one minus one, which is zero times limit is what?
That is zero.
Correct.
So as a result, we'll skip zero jobs.
But we still have a limit of ten.
Correct.
So we will only return ten jobs.
Now for the second page, what is the value of the page?
Well, two, two minus one is what one.
Multiply that by ten our limit and we will be skipping those first ten jobs, the ones that we showcase
in the first page.
And then of course, the same is for page three, four and however many pages you have.
So hopefully that is clear.
And of course we'll test it out here in the Thunder client as well.
So now we just want to navigate where we have the jobs and basically I'll provide here, Skip, I'll
provide here a limit.
So those are the variables we want to use.
And also right after the total jobs we want to return, Well, how many pages?
So not only the jobs, but also how many pages we have.
And we'll use those page values to construct our buttons on the front end.
And the way we'll calculate that, first of all, we'll go with num of pages.
We'll set it equal to math dot ceiling because I always want to round up.
So imagine you have 89 jobs, you limit ten jobs per page.
How many pages are you going to have?
8.9 or you're going to have nine pages.
So you'll have eight pages with ten jobs.
And then the last one.
Well, that one is going to have only nine jobs and therefore we're going to go with math dot ceiling.
So we always round up and we'll divide total jobs by the limit.
So how many jobs per page?
So now let's save this.
Or you know what?
Actually, before we save, we want to send back this as well.
So we're going to go with num of pages.
That's the value we want to send back as well as the current page, and we'll set it equal to our page
value.
So whatever it is, again in a second we'll pass it from the Thunder client.
But if it's not present, it's always going to be equal to one.
Okay, good.
Now let's navigate back to all jobs.
Now, since I have a limit of ten, since I'm displaying ten jobs per page, of course I want to come
up with some total jobs value, which is bigger than ten, and I believe this one was the case.
So let's go over here.
Let's send it.
And it says match here is not defined because I.
Yeah, it's not match.
Sorry, it's math.
Let me navigate back.
Let me send it.
Okay.
Awesome.
So I have total jobs.
Notice how we nicely calculate number of pages.
It is two because 19 divided by what the limit is.
Ten correct.
Again, we're rounding it up.
So essentially we have two pages in the first page we'll have ten jobs and the second one will have
nine.
Now let me look for something that's less.
Basically, I want to get, I don't know, in a second page, just few jobs.
I don't want to count nine jobs because I want to showcase that.
Of course, in the second page we'll have the remaining jobs.
So in here in a search value, I'm going to go with E!
Let's send it.
Now, notice we have 16 over here.
And then if I go to parameter and if I'm going to go with page number two, guess what?
Now the value actually is going to be six as far as jobs in my page.
So again, total jobs is 16.
In the first page, I'm not going to count, but I can guarantee you that there's going to be ten jobs
since my limit is equal to ten.
And then when it comes to the second page, notice one, two, three, four, five and six.
So everything is correct.
Our pagination is in place.
And of course, when it comes to the current page, this is something we'll use on the front end.
So this concludes the pagination functionality.
And up next, we'll start working on the front end.