So this section of the course is all about pagination, which is sometimes called chunking data, and
if you recall a while ago here in Models Don't Go, we created a function that allows us to get all
orders from the database and it works really well.
The problem, of course, is that it works really well with tiny bits of data.
What happens if we have five thousand or ten thousand or one hundred thousand orders?
Well, every time we call this function, it will cheerfully attempt to populate a slice of pointers
to orders and put one hundred thousand records in there.
And it might work, but at some point, if nothing else, our contacts will exceed three seconds and
this function will fail in the same way if we try to display a hundred thousand records all at once
on a Web page.
Well, that's not exactly an ideal user experience because it's going to take a long time to render
that table.
So what we want to do instead is to chunk our data up, to break it into manageable slices of data or
manageable sections of the overall results.
So to make that happen.
What I'm going to do, say some time, is copy the entire get all order function and I'll leave the
existing one there.
Even though we're not going to use it.
It's there for you to refer to it.
And I'll paste it right below, get our orders and go to the top and change this to get all orders patinated.
And I'll change the comment returns a slice of all orders of a subset of orders.
And to make this work, we're going to have to know two things.
First of all, how many records do you want for a given query?
And secondly, what page are we currently on?
So to do that, what we're going to do is accept a couple of parameters here.
We're going to accept page size.
In other words, how many orders per page of data do you want and what page are we currently on?
Which will be an end.
OK, so these are two parts to this function.
Now down here, before I declare my orders, let's figure out what our offset is going to be and I'll
explain what offset is in a minute.
Offset.
It's nothing more than the current page minus one times page size that would tell us how far from the
beginning of the results we should offset what we're looking for.
And of course, page size tells us how much to limit our query for and to make this work will come down
here to the bottom of our query and add another directive limit.
Questionmark.
And offset questionmark with no comment.
You don't want to call it.
And then we modify this query to pass in page size, and I said, now if I take this query right now,
for example, and go to my database and paste it in there and change this to say limit to offset zero.
OK, and hit, run, run current.
And that gives me two possible results.
OK, now if I get rid of that limit statement, limit and offset and run it again, now I have three.
So you can see that I correctly patinated this data and that's all there is to it for that part.
So there's more work to happen.
Of course, after we get all of this information down here, we get all of our orders populated.
We need to send back a little bit more information because we're going to need it on the front end.
We want to send back, for example, the total number of records so we know what page wrong.
We know what limit we're going to be.
But what is the total number of records?
What we're going to need that.
So I'm still going to modify my query variable and make it now say these back checks here.
I'll just say select Count Odon ID.
And this is pretty much the same as what we had before from orders.
OK, and then I'll join it to widgets because I need that for the where clause left.
Join widgets W on O widget ID is equal to ID and we'll say where W is recurring equals zero and that
will get us all of the orders for widgets instead of subscriptions.
So now we'll just execute that query or create a variable var total records, which will be an end and
now we'll say Kotaro is a sign the value of and we're just getting one row from the database IMDB that
query row context and the context and handed our query and then we'll see error equals Kotaro dot scan
and we want to scan it into total records
and we'll check for an error if error is not equal to nil.
And I'm not going to populate that right now because I want to actually get another return value.
And we'll fix this up in a minute.
We also need to know what is the last page.
We're going to need that in the front end.
And that's really easy.
Last page is a sign the value of our total records, which we just got back up there on line for sixty
three.
Divided by page size, which we received as a parameter to this function.
Now I'm going to return orders and I'm going to return last page and I'm going to return total records
and no error, which means I'm here.
I can now return nil zero zero and the error and I'll copy that.
And everywhere else I'm returning an error.
I pasted in there and I'll paste it in there.
And finally, I'll change this to return a slice of pointers to order an entry and an error, and now
all my errors go away.
So that is our query or our method to actually get patinated results for all orders.
So the next step is to go update the handler and the front end, of course, to use this query instead
of the one that is currently using, which is just get all orders.
So we'll change the functions that's called.
And we'll have to make some modifications to the front end code as well.
And we'll get started on that in the next lecture.