Check out a free preview of the full API Design in Node.js, v3 course:
The "CRUD Controller Exercise" Lesson is part of the full, API Design in Node.js, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to hook the routes to the models in order to perform CRUD on the models based on the routes and verbs.

Get Unlimited Access Now

Transcript from the "CRUD Controller Exercise" Lesson

[00:00:00]
>> Scott Moss: So far we have the routes and the models. Now we need to hook our routes up to our models, so we can perform CRUD on the models based on the routes + the verbs. That's exactly what controllers do. So you're gonna be creating the generic CRUD resolvers, you're gonna create controllers for the item resources using the base generic CRUD resolvers that you just created, and you're gonna ensure that all test paths by running the test command, as always.

[00:00:23] So basically, what I'm saying is, if you go over to utils > crud.js, after you check out to lesson-4,
>> Scott Moss: And you go over to utils > crud.js, you'll see a few things in here. There's about five functions in here that are completely empty, they don't do anything.

[00:00:46] Because these are generic controllers, or because they're controllers in general, they have a signature of request response. Cuz that's just how controllers in middleware work and Express. But there's another function before them. This function is because they're generic, we've got to pass any model to the controller itself, because we won't know what the model is.

[00:01:08] The model might be an item, it might be a user, it might be a whatever, a list. So we have to pass that model in, and then those controllers get created. So that's just because like I said, we abstracted these and them generic. But that part you don't really have to concern yourself with, the only part that you have to concern yourself with is actually creating the controllers themselves.

[00:01:27] So for a getOne we know that we wanna be able to do a model.findOne or a model.findID. So for instance, in the case of you wanna be able to find by an ID if you go back here, so FindByID. We're expecting an ID here. If we go to our routes,

[00:01:49]
>> Scott Moss: Item router, you can see that we have this parameter ID here. So how do we get the access to that ID inside of our controller so we can use it in our database queries? Well, if we go to our controllers, you will be able to access that ID on the request.

[00:02:06] So if you req.params, that's an object of all the parameters on a URL, whether it's a slash id, whether it's a create parameter that somebody unexpectedly sent up, Express creates all that stuff for you. So in this case we have a parameter called ID. That means, if it's passed, there'll be something on the request called request dot params dot whatever this name is.

[00:02:33] In this case, ID. If this was called thing, then it would be req.params.thing. If this was called whatever, then you get the point. So in this case it's ID. So we'll have access to the ID that's passed by doing request.params.id. And that's how we get that ID that we can pass to a getOne or a put or a deleteOne.

[00:02:58]
>> Scott Moss: The other params, there aren't any other params, so you won't have any issues with that. But again, if somebody put create params, like a question mark after the URL, pass it up, Express will merge all of those into the params object, too. So there'd be collisions, so that's just one thing to take note of.

[00:03:13] So yeah, you're basically, you're going to create the functionality, given a model. All you have to do is just create the CRUD functionality based on the model queries that I just showed you for these controllers. And the tests are testing for that. There are some specific tests in there, they have some really good.

[00:03:33] They're really good documented, so if we look at the ones for the CRUD which in crud.spec. You'll see getOne, it finds authenticated users. It's doing a lot of different stuff here. So you'll have to test all of this, or you'll have to get all these test passed in order to move forward, and again, they're pretty well documented here.

[00:03:58] If you run the test here we can see.
>> Scott Moss: I'll say yarn run, npm run tests-controllers, I believe, yes.
>> Scott Moss: Get a whole bunch of failing tests here. We scroll up. You're gonna have two suites of failing tests, most likely. So you'll have the generic CRUD controllers that are failing.

[00:04:28] So it's looking for all these things to be satisfied, and then you have the ones failing based off of you not adding the controllers to the actual item's controller file itself. So the items controller file, which is where I wrote all this stuff, for you it'll just be empty, it's like almost nothing here, it's like an empty object.

[00:04:51] You gotta fill that out with the generic controllers that you created and pass in the right model. And if you get lost on that, there's examples inside of, like list, if you like list controller. You can kinda see what I'm talking about here.
>> Scott Moss: So if you get those to pass, you should be good.

[00:05:09] One thing to note is if we go over to the CRUD controllers or the test here. It's gonna be expecting there to be some user object on the request object, and it's gonna be looking for that. So that means you're gonna have to write your controllers in a way that it expects a user to come in.

[00:05:32] Cuz the next thing we're gonna be doing is authentication, so these controller are aware of that authentication. So just like I said, middleware can attach things to the request object. In this case, these controllers are assuming that there is a user object attached to the request object, and that's how it's gonna be identifying that you can or cannot do something.

[00:05:49] So some of the tests you might see finds authenticated user. Finds by authenticated user and ID. So in this case, it's expecting to query by a user ID which is created by, and a parameter ID. So it's doing a combination of both. But that's basically what's it's expected to do.

[00:06:09] So the test will tell you what to do. And you can see that if it doesn't find it, it expects it to 404. So you'll have to use the response object and set the status to be a 404 if you don't find something. So you might have to do some try catches, or you might have to do a .din or .catch in your premises to figure that out.

[00:06:30]
>> Scott Moss: Any questions on this? So there's only five functions you have to fill out. And then the next thing you have to do is just add it to the item controllers.js, which is very similar to the list controllers.js. It's almost identical, the only difference is the model that you pass in, which will be the item model.

[00:06:53]
>> Scott Moss: Yes?
>> Speaker 2: Could you put up the code again that shows where you connect to the database?
>> Scott Moss: You won't have to connect to the database in this one, but I can show you, yeah. You shouldn't have to do it. The only reason I had to connect to the database because I was running just that one file.

[00:07:08] Outside of the application, so I had to connect to the database, but you're gonna be running your code in the context. Yeah, exactly, so you should be fine.
>> Speaker 2: Okay.