00:02
Now for our application, the courses are the bread and butter. It's what everything else revolves around and it's the entire purpose of the application itself.
00:12
So let's walk our way through getting the get request for this setup, because ultimately we're gonna want this paginated and we're going to want to be able to search upon it.
00:22
But let's start easy and let's just get a simple list of our courses in this lesson. So step one is to jump into our terminal and get our API controller setup.
00:32
So we'll do node ace make controller, put this inside of our API version one folder and we'll call it courses and add our hyphen A
00:41
for the API only method stubs. Okay, that has now created our courses controller along with the index store show update and destroy methods within it.
00:49
And since we're stepping our way through this, we'll just work within our controller rather than extracting it out into any actions thus far. So our primary goal with this endpoint
00:59
is to get a list of the organization's courses. We're going to want to reach through the relationship of our organization to help filter those out. So we'll do organization related
01:09
and then we can reach through the courses relationship to query upon them. From there, we will order them by the order field and there we go, we have our simple list of courses.
01:19
Let's go ahead and return that back as our response. And then we can jump into our API/version one routes there
01:28
and add our router resource for our courses using the courses controller from our API version one directory, marking this as API only.
01:38
Go ahead and give that a save and hop on over to Hopscotch real quick. Let's create ourselves a new folder collection within our API called courses.
01:47
Within our courses folder, we'll add a new request called get courses and then I'm going to put in parentheses paginated to note that this will be a paginated list.
01:56
Go ahead and create that request, switch the HTTP method to a get, switch our end point from statuses to just courses
02:05
and then we can go ahead and get rid of our body for now and let's make sure that we have our headers, which we do. Let's try sending that off and there we go. We get back a list of our courses
02:14
with which we just have my first course here. Now for the applications interfacing with our API, having the access level, difficulty and status IDs might be useful to them,
02:23
but I bet what would be more useful is to actually have the values, the names that those IDs represent so that they know what the actual access level, difficulty or status is without needing to go map it back
02:33
to the ID representation. So let's go ahead and preload that onto our courses via our query. So within our query here, we'll go ahead and preload the status
02:43
as well as the difficulty and the access level and what might be nice as well is to go ahead and just include a count
02:51
of how many modules as well as how many lessons this course contains. So let's give that a save, jump back over to Hopscotch, send that off one more time and there we go.
03:01
Now we have, in addition to our courses information, the relationship information for these IDs as well. So we have our status, our difficulty, our access level
03:11
that the applications interfacing with our API are now going to be able to directly read from to get the actual name for what that field is. One thing that is missing, you might've noticed,
03:20
is our counts that we added. Those are nowhere to be found and that's because counts go on the extras object of our Lucid models, which by default
03:28
are not serialized with the model itself. So we need to inform Lucid to go ahead and serialize that information for us. If we were using something like a DTO or a view model
03:38
or anything like that, we'd be able to define that directly within there. Since we're just returning back the model itself and the model is serializing,
03:46
let's go ahead and jump into our courses model here, scroll on up to the top, and we just need to add a property on here called serialize extras, and we can set that to true.
03:56
You can also do a callback that plucks particular extras to be serialized with our response, but just setting this to true will automatically serialize any extras
04:06
on our model instance. So with that saved, we can go ahead and close our model on out, jump back on over into Hopscotch and let's try sending this one more time. All right, let's scroll through our results here
04:16
and now we have an extra meta object, which consists of what was our extras on a model instance. And this contains our modules count
04:26
as well as our lessons count. So we can see that we have two modules and three lessons. Perfect, so our list is coming through a okay thus far. In order for us to start working with pagination,
04:36
though, we're gonna need more than one record to make sure everything's working okay. So next let's go ahead and create new courses.