00:02
- Now at present, in order for us to update a course's status, difficulty, or access level,
00:09
within our API, we need to update the entire course, which contains the name, access level, status, and difficulty as all required properties within that.
00:18
So we would need to provide the entire payload of our course to update any one of those. But in our UI here, we have the ability
00:25
to toggle them one by one via these little dropdowns here. So what would be great is if we also had a way to do that within our API, so that we didn't need
00:33
to post up the entire body payload of our course to update any one of them. So within our courses controller, what we wanna do is add a new method
00:43
that will be relatively similar to the method that we're doing this with on our courses controller outside of our API. So if we scroll down to that, we have this tag method right here,
00:53
and we'll be doing mostly the exact same thing, just returning back differently. We'll be returning back an API response rather than redirecting back to our UI. Great, so within our courses controller,
01:03
let's go ahead and add in that tag method. So async tag, let's grab our params so that we know what course it is that we're dealing with, the request so that we can validate our data,
01:13
and the organization from our HTTP context. And as always, the first thing that we need to do is authorize our token. And this is gonna be an update because we're updating the tag,
01:23
which is a combined name that we've chosen for our difficulty statuses and access levels for the course itself. The course needs to pre-exist in order for us to update any one of those.
01:31
So we'll pass into the authorized tokens update method, our organization, to verify that the interfacing application has permission to update this. Then we'll go ahead and await
01:41
our update course tag action, importing that, and we'll call the handle method passing in the ID from our params ID,
01:49
the data, and then the organization. Oh, right, we skipped a step. We have to validate our data as well.
01:55
So const data equals await request validate using, and we should have a course patch tag validator, if I can spell it right, patch tag validator.
02:05
There we go. And that too is going to need our with organization metadata so that the validator, if we take a look at it here, can go ahead and make sure that the status or difficulty
02:15
or access level actually exists within the organization. Additionally, these are all optional, but they all are required if the other two are missing. So for our status ID,
02:25
if the difficulty ID and access level are missing, then it is going to be required, despite it being optional, thanks to this required if missing.
02:33
The other two look the exact same as the status ID here. So from our validator, we're going to get back our data that has either an access level difficulty or status ID populated. If we did not provide it in the payload,
02:43
then it will be undefined, and our update operation will ignore it and leave it be. Within our update course tag action, we're querying the course that we're updating
02:52
off of our organization via the ID that we're passing in, and then just merging that data, which is what's going to omit or ignore
03:00
those undefined properties that we've passed in, allowing us to update any one of those three. So perfect. Let's go ahead and close that out and finish this off
03:08
by returning back the response from our course tag, which is our actual courses updated information. So we can go ahead and just simply return directly back from there.
03:18
Now, this new method's not going to be handled within our route definitions for our resource on our courses. Instead, what we need to do is add a specific route definition for it.
03:27
So underneath our resource here, let's do router. And since we're updating partial data, I'm going to go ahead and set this as a patch request. Since we're not updating the entire course, we're just updating specific properties
03:37
or a single property within it. And for the routes pattern, let's do /courses/id, mimicking the route definition structure
03:46
that will be set by our resource here. And then tag is what we're calling these. And then tag is the universal term that we're using to reference the access level status or difficulty
03:56
within our courses, modules, and lessons. So we'll do /tag there, so that we know with this patch request, we're updating the specific course for the ID that we're passing in
04:06
and specifically updating one of its tags. Then to handle this route, we'll use our courses controller and its tag method for that.
04:14
Then we'll go ahead and give it a name of as courses.tag. All right, so we can give that a save and let's jump on over to Hopscotch and get a new request set up for this.
04:24
So we'll call this patch course tag, save it. And with this, we want to switch the HTTP method from post to patch.
04:33
Then let's jump back over to our get courses here and take a look at what exactly the tag IDs are. Okay, so my first course is all ones
04:40
for our access level difficulty and status IDs there. So let's hone in on our first course and then note that we want to patch the tag.
04:50
Within our body, we only need to specify one of the three that we want to update. So if we wanted to update our access level ID,
04:59
we'd specify that and we could set it to something like two since it's currently one. If we send this off, we're going to get back our 200 OK response with our updated courses details
05:09
now set to the access level of an ID of two. Thanks to our validation, if we set this to an ID that does not exist within this organization, send this off,
05:18
we're going to get back a validation error noting that the selected access level ID is invalid. And this tag update operation should work for our difficulty ID.
05:28
We set that to two, send that off, as well as our status ID. If we send that off, that's going to now update all three to the ID of two
05:38
where they were all previously one.