-
All right, so we walked through each of the different methods that we're gonna need for our difficulties API within our organization.
-
Next, let's go through the exact same process for our access levels and statuses as well. They're set up the exact same as our difficulties, so we should be able to blow right through them.
-
First, let's dive back into our terminal and get our controller set up. In this lesson, we will focus specifically on our access levels.
-
So we'll node ace make controller API V1 and call this our access levels. Lastly, let's add in our hyphen A to stub our API methods within that controller.
-
All right, back within our text editor now, we have our access levels controller added into our API version one folder. Let's go ahead and start with our index
-
and let's grab the organization out of our HTTP context. All that we need to do here is return, reach through our organization's relationship for our access levels,
-
and then query them ordering by their order column. Then for our store, in addition to our requests, we also want to grab our organization
-
and then validate our data. So await request validate using, we should have an access level validator that we can import and make use of.
-
Then we'll get our access level from our action. So we'll await store access level, call that handle method,
-
and we can see that it needs our organization and data. So we'll pass in the organization and data into that. Once we have that, we go ahead and return that access level.
-
You could of course also just return directly from the action as well. So if you wanted to, you could just return right there. We'll go ahead and give the variable a name
-
to note what it is that we're getting back from this action. That takes us next to our show, where once again, we are going to wanna grab our organization so that we can reach through
-
its relationship to query the specific access level that we are after. So organization related access levels, grab the query builder for that.
-
And we'll do where the ID is equal to the programs.id being passed in through the URL. Then we just wanna grab the first or fail during a 404 if it cannot be found.
-
All right, that then takes us to our update, where yet again, we need that organization then to validate our data.
-
So we'll do const data equals await request validate using, and we should be able to make use of that access level validator yet again.
-
Let's grab the updated access level response from our update access level action, call the handle method.
-
This needs our organization, the ID and the data. So we'll go ahead and pass the ID in first from our programs.id that allows it to query the specific access level that should be updated.
-
Then we'll pass our organization and the data in after that. Once we have the updated access level, we can go ahead and return it back as a response for our request.
-
Okay, lastly, our destroy. And this is going to work the exact same as our difficulties. It's going to accept in a replacement ID that we'll use to replace the one that we're deleting with
-
on our courses and lessons, making use of the one that we're actually deleting. So in addition to our params, we need our requests so that we can validate our response
-
so that we can switch it to a 204 and our organization. The first thing that we're going to want to do is grab our validated data,
-
ensuring that the replacement ID that we're accepting in actually exists on the organization. So we'll do validate using,
-
and we should have an access level destroy validator there. And then we can import our with organization metadata helper and pass the organization ID into that.
-
That passes the organization ID into our validators metadata so that it can use it to ensure that the replacement ID exists on the specific organization.
-
Then we can go ahead and await destroy access level, call our handle method for that, pass the ID in from our params ID, the organization,
-
and then lastly, the data. Finally, we want to return response and set the status to 204 as we're not returning anything from this request.
-
Perfect, so there's our controller for our access levels. What we next need to do is rig it up to our route, and we can do it the exact same as we did for our difficulty.
-
So router resource access hyphen levels, and we'll handle that with our access levels controller. Again, make sure that you're importing the one
-
from your API version one folder there. Then we'll add the API only to omit the show routes for creating and updating. Okay, let's give that a save,
-
and we should be able to jump back over into Hopscotch now. I'm gonna go ahead and close out all of the open requests that we have so far. Looks like we've made some changes to that.
-
We can go ahead and save that, and I'm gonna collapse these two down. Let's go ahead and add in a new folder within our API called access levels,
-
and within our access levels folder, let's create a new request. We'll call this get access levels. Hit save on that. Switch the HTTP method to a get.
-
Go ahead and close that destroy out now, and we wanna switch the URL from difficulties to access hyphen levels, and then we can get rid of the route perimeter there as well. We also don't need a body,
-
so we can switch that back to none, and let's double check our headers real quick. Looks like we have our accept and authorization in there, so we are A-okay. Let's hit send, and perfect.
-
We get back our access levels. Right now we just have two, free and paid, and we get back a 200 for our response. Let's expand that folder on out.
-
Looks like we need to give that a save, and let's create our next one. So add request. We'll call this store access levels. Hit save on that.
-
This one will be a post, and this one will need a body, so we'll switch this back to application JSON. Just like our difficulties, I think this one just needs a name, so maybe we'll call it mixed.
-
Perhaps it's a combination of free and paid, and then a color, which needs to be a hexadecimal code. So not a hex. Let's make sure that our validation's working okay.
-
Let's try sending that off, and sure enough, we get back a 422, unprocessable entity, stating that the color field must be a valid hex code color. Perfect.
-
Let's switch that now to a hex color. So I'm just gonna go FF0000, which should be red. Let's send that off, and perfect.
-
We get back our newly created record as our response. That has an ID of five. Let's give that request to a save, and add in one more.
-
We'll call this get access level. Give that a save. Switch this one back to a get. Let's get rid of the body. Switch that to none.
-
And then we need to add in a route parameter with an ID. Let's try six first, which should not exist. We should get back a 404, which sure enough, we do. And let's switch that now to five,
-
which we have just created. So we should get a response back here. And perfect, we do. It's our mixed result. All right, give that a save. Moving right along, we now need to update an access level.
-
So we'll create an update one here. Switch this from get to put, and we need a update body. So we'll switch that to application JSON. Create our object.
-
Take in a name of mixed, and I'm just gonna add edit onto the end of that. And then our color, maybe we'll switch it to blue instead.
-
So 00FF00, and that should update our access level with an ID of five to be mixed-edit when we send that off.
-
And sure enough, that's exactly what we get back. Mixed-edit, and then we have 00FF00 as our hexadecimal code color. Let's go ahead and give that one to a save.
-
And finally, we need our delete. So let's do delete, destroy access level there. Switch the HTTP method down to delete.
-
And let's go ahead and create one more to specifically delete. So let's go back to our post and let's just create a brand new one. Again, we don't need to change anything since we'll just be deleting it. That has an ID of six.
-
Jump back over into our destroy that we're working with. Switch the route parameter ID here to six, and we can get rid of the bodies. We'll switch that back to none,
-
and should be able to send that on off. And, oh, right, we do need a body. We need that replacement ID. So, okay, let's go ahead and add that back in.
-
Get our object going, replacement ID, and we'll replace six with five. They both have the same name after all. So we can send that off, and perfect.
-
We get back a 204, no content, which means everything worked. So let's too give that a save, and now we're ready to move on to our statuses.