-
- Before we begin implementing our get lessons endpoint, let's first stop and talk about what we actually need that to implement within our API.
-
So if we jump into our route definitions, currently we have our lessons as a top level resource, meaning that it doesn't require a module nor course ID
-
be within the route definition as a route parameter. Now we have our lesson as a top level resource, meaning that it does not have a module nor course ID
-
as a route parameter on any of the route definitions. For our post put patch and delete operations, those work A-okay because we can provide that information inside of the request body.
-
For our get requests though, those don't have a request body to work with. So if we want our lessons index method to be honed in to a particular module,
-
then we're gonna have to provide that information inside of the URL as a route parameter. If we were to leave things as we have them presently, then the expected behavior of our lessons index method
-
would be to return back a list of all of our lessons, regardless of course or module. That may be something that you want to implement, but for our use case, what we're gonna do
-
is go ahead and migrate away from the index method towards a separate module method honed in
-
on a specific module to get a list of courses underneath it. And then later on, we'll add a separate search endpoint that will allow us to search all of the lessons,
-
being able to hone in on specific courses, modules, filter in based off of status and things like that. So in case we decide to refactor
-
and add in an index list of all of our lessons, we'll leave this name available. And instead we'll switch what we have currently to module
-
and we'll add an additional route definition for it that includes a module ID route parameter so that we can filter down and get just those.
-
So we'll grab our params and organization out of our HTTP context. We'll go ahead and authorize our token for a read operation using that organization. Let's go ahead and write our query first
-
and then we'll extract it into an action after we're happy with the results. So let's do const lessons equals await,
-
reach for our organization, grab the related lessons, and we'll query off of there. Then we need to filter it down to the specific module ID
-
that we'll be providing through to our route parameters. So we'll do params.moduleID, and then we'll also want to order it by the order field
-
that we have on those lessons in ascending fashion so that they are provided in the same order that they're actually listed underneath the module. Once we have that, we should be good to go ahead
-
and return those lessons as the response. And let's jump back into our route definitions because on that lesson resource, we now need to include our index method
-
as we're no longer implementing it. And we need to add in an additional route definition for a get to slash modules slash,
-
and then add in our module_ID route parameter, add in another slash lessons. To note that we're after the lessons
-
within the specific module that we're providing as the route parameter. To handle this, we'll use our lessons controller and its module method.
-
We'll give it a name as well called modules.lessons. And if you see any potential other routes coming your way in the future
-
that may also need to use the name modules.lessons, we'll probably wanna give that one more suffix. So I'm just gonna go ahead and add dot index
-
since we are after the list of the module lessons to give it some specificity within that namespace. Okay, we'll go ahead and give that a save. And we should be able to jump back into Hopscotch now,
-
and we need to switch the name of our get lessons to be a little bit more specific to get module lessons. And then we can dig into that endpoint,
-
switch the URL from slash courses to slash modules, and then provide in a module_ID. I'm just gonna go ahead and assume that we should have one with an ID of one.
-
And then we also want to hone in on that modules lessons. Okay, with that set, make sure that you have a get request there and go ahead and send it off and cool.
-
Okay, we get back a list of lessons here. We have my first lesson, my second lesson, and then our test lesson 01-edit. So that seems to be working A-okay.
-
That one has a module ID of one, so does this one, and so does our third. So that's working perfect. Now that we're happy with how that query is returning back, let's go and jump back into that lessons controller
-
and extract this into an action. So to do that, let's jump into our terminal, node.ace.make_action, and we'll put this inside of our lessons folder.
-
And we'll call this action get lessons by module, hit enter to create that, clear that out, back into our text editor, and let's dig into our actions,
-
lessons, get lessons by module. As always, we want to make sure that we're sticking to the organization. So we're going to accept our organization into this actions parameters,
-
and then we'll also take in the module ID so that we can filter down further from there. Let's grab both of those out of our params,
-
and then we need to dig back into our controller, and let's just copy that query back into that action. We can go ahead and just directly return back the result of that query,
-
and also get rid of the await, that's not needed. Switch params here to our module ID. We can also just go ahead and jump that over into an object just to make it a little bit cleaner
-
since we have a variable of the same name. Give that a save, jump back over into our controller, let's await get lessons by module, call our handle method, provide in that organization,
-
as well as our module ID of params.module_id. Further yet, we can go ahead and just directly return back from that action as well.
-
Okay, let's give that a save, back over into Hopscotch, try sending this off one more time just to make sure everything works, and sure enough, it does.