All right.
And the last thing that I want to do before we start working with the database is to set up our controller.
The job controller, as well as the job router.
Because take a look.
Our server is getting quite busy.
And effectively we just want to come up with some kind of structure where we can have the functionality
aka our controllers in one place.
Also, we want to set up the router so we can nicely group requests in a different file and as a result
our server JS is going to have less code because keep in mind we still need to create quite a few requests
and just jamming them in one file.
In the long run, it's probably not the best idea.
So let's start over here.
By creating a new folder.
And a common convention is to call this controllers.
Again.
Technically you don't have to, but it is a convention.
So we go over here with controllers and we need to come up with a name.
So in my case, I'm going to go with job controller.
So this is what we're creating.
And eventually, yes, of course we'll have functionality in here to communicate with the database.
Now at the moment we don't have that.
So what I'm going to do is just copy and paste for now temporarily, because we will remove this code,
the jobs and the nano ID.
Again, I just want to showcase how it works and since I'm not going to use it directly in the server,
I'll just cut it out then we want to go to job controller, copy and paste.
So again, we're just looking for this 900 ID just so we can create the jobs.
And then one by one, let's set up the functions that are essentially going to be our controllers.
We'll just set it equal to a function so we can export and then import in our router.
So what am I talking about?
Notice how pretty much we have these requests and we have the URL part and then the controller.
So now I want to move this logic to a separate file.
So that's what we're doing.
And basically first I just want to create those functions.
We'll start with export since again, we'll export it away from this.
File.
I'm going to call this get all jobs.
And we want to set it equal to this.
So essentially take this entire thing out and you're not going to import right away back into server.
No, we'll set up the router.
So there's going to be something in between.
And since eventually we will have logic.
Which is going to require the await.
I'll right away set it as async.
Please keep in mind that of course, in this case, technically we're not using that functionality when
we want to go with create job and one by one pretty much will repeat.
So again, this is going to be async.
We want to swing back to the server.
Let's take this one out over here.
Drop controller.
Yep, that's the one.
After that, we have get job or get single job.
That's also a name I use.
So back to the server.
And controller out.
Set it up over here again, async.
Make sure you have that.
I mean, technically you can add it later, but.
I just think it's easier if we do it right now.
Let me grab the edit one.
Cut it out.
I'm going to call this get.
Or I'm sorry.
Update job.
And then the last one will be delete job.
So export const update job.
Async.
Copy and paste.
And then lastly, let's do the same thing with the delete.
So let's get it out.
Job controller.
Export const.
Delete job.
Async and copy and paste.
Okay, now we have access to it.
And for some reason, of course, I have the error because I removed a little bit too much.
Okay, my bad.
Let me go back over here.
Let me first save the server.
Guess let me close it.
And then in the job controller, I'm not going to take the use.
My bad.
Let me take this one out.
And there's also an extra parenthese, which at the moment I have.
So let me set up the curly instead.
Then back in the server let's add.
Before the error one.
This one over here.
Let me add the parentheses so everything is correct.
And now we want to create our first router.
So notice all of these requests are technically going to the same base URL, so we still have API version
one jobs and then pretty much everything after that depends if it's a request which requires the root
param.
Of course we add it.
If not, then it's still API version one job.
So we could nicely group them together and therefore we're going to create another folder and this one
will call router.
Or roots, you know, let's go with the routes.
Then instead of the routes.
One, let's create a new file.
And I'm going to go with Job router and here's what we want to do.
We want to first import router from Express and we want to invoke it.
So go over here.
Router.
Yep.
That's the import from.
Express.
Then.
And as a side note, my bad.
Once in a while I just keep typing the common commonjs syntax.
My apologies.
So import from express.
That's the ES6 modules.
So const router.
Is equal to router.
Let's invoke it.
And now, essentially with this router instance, we can do the same thing.
We can set up router, dot, get router, dot post and effectively we want to import those controllers
and make sure that they handle the proper route.
Now, first guess, let's start by grabbing.
All of the controllers, but just make sure that it's coming from the same place.
So we want to go one level up.
We're looking for the controllers, then job controller.
Just make sure you add the JS again, otherwise you'll have the error and then in here.
What functions are we looking for?
Well, get all jobs.
We want to get the job.
Create the job, then update the job.
And delete the job.
Then once I have the controllers, effectively we have two approaches.
You can use this one where again you go with a router just like we did with app and let's say for get
all jobs, what is going to be the method?
Well, it's going to be get correct.
Now eventually I'll point the router to API version one jobs to basically this base URL over here and
therefore this is going to be the forward slash.
And essentially I'll say, okay, so this is going to be get all jobs and then in order to create the
job, what is the difference between the URLs?
Well, there is no difference.
The only difference is the method.
Correct.
So we just go here with post.
So this is one approach, but actually like a different approach because that way I can chain the controllers.
So what am I talking about?
And as a side note.
This is not correct.
So create job.
Remember, controller was also different.
Now we also have this route, so I can go with router dot route.
And then again, since this is going to be the base one, something you'll see in a second I'm going
to go with just forward slash and then I can chain what method do I want to use for get all jobs?
Well, it's a get one correct.
So we go with get all jobs.
Then we can also chain to the same one because again, the URL is the same.
I know it's probably a little bit redundant and annoying, but I do have to repeat that.
And in this case.
The difference is that the method is post.
And now of course we're using create job.
Again, I just like this setup because it's just less lines of code.
That's it.
There's really no magic behind it.
You can use this approach as well if you want.
So in this case we have router dot route.
But what is the additional thing we need to add over here for get job update, job and delete job.
Well it's the route param.
Correct.
So we just go with forward slash and then the ID and then one by one, let's set them up.
This is not going to be get all jobs.
It will be get job then.
It's not a post one, it's a patch one and we want to go with update job.
And then lastly, let's go with dot delete and we're going to delete the job now.
Very, very important.
We want to export default.
And the router.
So our router instance, let's export it and back in the server.
Somewhere here you want to import.
And I always like to keep import separate.
Basically, these are going to be imports for packages.
And then I'll have technically my custom imports, which essentially just means that these are going
to be things that are going to be coming from my own file.
Again, you can put them all together.
It doesn't really matter.
So let me go over here and say routers and we'll start with import then.
Name is up to you since we're setting this up as a default export.
In my case, I'm going to go with a router and in this case I have dot and forward slash since it's
in the same folder, then routes job router and again.
Yes, add JS extension and then you want to set up app dot use again, it's going to be middleware and
pretty much we want to add the API version one and jobs.
So that's going to be the starting point for our router.
And then from there, if it's a base route, then we handle it this way.
If we have route param, then we'll use these controllers.
So let me navigate back to the server.
And pretty much we can remove all these ones the comments and get all the way up to delete.
We won't use it over here.
And in between the post and the 404, we can go with app dot use and essentially provide the full URL
API version one job.
So this is going to be the starting URL and which router is going to handle that?
Well, in our case, that is going to be the job router.
Now I'm not going to test each and every request over here.
But I do want to check quickly the main ones.
So let's go back to get all jobs.
If everything is correct, we should see the list of jobs.
And also I want to create the job.
So let's go here.
Let's send the request.
Notice we have 201.
We were successful.
And then if I check all jobs, of course I'll have one more job added to the list.
And if everything is correct, we can start working with our database.