00:02
- So we've already done a part of what we need here. We've wrapped our filters in an if check. What we want to also do is make the filters
00:12
as a whole within our parameters optional. And you'll notice that we do indeed actually still need our page and per page within this action in order for us to be able to reuse this elsewhere
00:22
beyond just searching. So if we wanted to reuse this action for our index method, then we really don't want to filter on anything here. We could, but we don't have that natively right now
00:32
within this index method. So if we didn't want to, then we want our filters to be optional. So to support that, let's mutate our types here a little bit.
00:40
Let's switch this to data or validated data since we're inferring the entire set of our course search validator here, which includes that pagination information.
00:49
And then let's put our filters in a type that omits from our data, the page and our per page from that type.
00:58
And now our filters just comprises of our name filter, status ID, difficulty and access level IDs, making our filters property here actually our filters.
01:07
Then we can add in addition to that, our per page, which again, we can make optional of type number as well as our actual page there,
01:15
which we can make optional and type number as well. Then from our params, we want to grab our page and per page from there. And we can set our defaults directly in here.
01:25
So we can set our default to five if it doesn't have a value and one for our page. Then within our paginate call, let's get rid of our filters dot, as well as the null fallback there
01:35
for both our page and per page, which should make that happy. The only thing that's left to do is assert that we do actually have filters since we already wrapped that in an if check with a callback.
01:44
That should make everything nice and happy within our action. Now, if we jump back into our controllers, our search endpoint is still happy because we're providing our filters in, but now we need to switch this back
01:54
to also providing our page and per page separately. So we can pluck the page and per page out of there and then spread our filters in. And I think this is how we had it originally
02:04
whenever we copied this over from our index method. Then we just need to apply those in. So page and per page, and we'll just let the defaults be managed by the action there.
02:13
Then we can scroll back up to our index method where we still have our page and per page here and replace everything with this query to instead run off of our search courses action
02:23
called a handle method there, pass our organization in, omit the filters, but pass our page and per page into there.
02:31
Again, since the action handles defaults, we could let that do its thing inside of the action itself. And that should be happy there too. And this also gives us the flexibility
02:40
to add filters at any point in time if we so choose to our index page endpoint. Perfect. So let's jump back over into Hopscotch real quick,
02:48
over into our get courses paginated. Let's just make sure that that still works A-okay. So we send it off, we get back 14 total, five per page, current page is one.
02:57
If we switch this to page two, we get back page two as the current page, still 14 total. So everything there is working okay.
03:05
And let's do and per page equals 10, send that off. And now we get back 10 per page, current page is two, total 14, last page is now two. Perfect. So that's working A-okay.