All right.
And up next, let's go down the list and let's set up for more requests.
So we want to have one for create job, find one job, basically get one single job based on the ID,
modify the existing job again based on the ID, and then lastly, delete a single job.
And I guess let's start with the create job.
So let's navigate back to our project.
And you know what?
In here I will add some comments.
I'll say, get all jobs.
That's the request.
And now let's copy and paste since a lot of the stuff is going to be.
Very similar.
As far as the.
Yes, we'll have this one to get all the jobs on the URL for creating a job is actually going to be
exactly the same.
So it's going to be API version one and then jobs.
So they both go to the same place.
But since the method is different, of course the request is different.
So in this case we are creating a job.
So create.
Job.
Now, as far as the functionality, we want to first grab some values from Req.body because keep in
mind, in order to create a job, well, we need to provide what we need to provide company and position.
Now, typically this gets set.
In the database and therefore we will create that ID in our controller again.
Eventually the database will do that job.
But for now, we'll do it over here in Create Job.
So first we want to grab from the Req.body Body company and position.
How do I know that those values are going to be there?
Well, because we will be setting up that request in a second in the Thunder client.
Correct.
So we're going to go here with the Req.body.
Remember, that's where the Json data is located.
And we want to set up a check where if the value is missing, if one of them is missing, then essentially
we'll send back a 400.
And essentially when it comes to status codes, 400 stands for bad request.
So before we create the job based on company and position, we want to check if those values are present.
Now, eventually we'll set up a layer which is going to be sitting on top of our requests.
But for now, yes, we'll do this manually in the controller.
So we'll say if the company is missing or the position is missing, here's what I want to do.
I want to send back a different request.
And in order to speed this up, I'll just copy some values.
So I'll take it from here and it's going to be 400 again.
That's a bad request.
And we do want to add here a return, because please keep in mind that if the return is going to be
missing over here, you'll get an error from Express.
Essentially, it's because JavaScript just keeps reading the code.
So if you have no return over here in this condition, then you'll have two responses and express is
not going to be happy about that.
And then as far as the Json, well we'll send back a message property and we'll say please provide.
Company.
And position.
Let's save this.
And if everything is correct, then of course, we continue setting up our new job.
And first we'll start with an ID and technically we'll cheat a little bit because again, database usually
does that job.
But in our case we're going to go with const ID is equal to then let's invoke nano ID and then just
provide some number value how big that ID is going to be.
And I'm going to go with ten over here.
Then we want to create that new job.
So go over here with ID.
Company and position.
So I'm creating a new object.
ID is equal to ID, company is equal to company position is equal to a position.
This is what we can do in JavaScript these days.
And then we just want to add this job manually to our jobs array.
Now, please keep in mind one thing.
Of course this just runs when the server is running.
Hopefully that is clear.
Eventually, again, everything is going to be persisted in the database.
So in here we just want to go with jobs dot push job and then as far as the response.
Typically when you create a job, you don't send back all of the jobs like we just did.
You go with one job.
And now we want to navigate to our collections.
And in here, we want to set up a new one.
So in this case, I'll duplicate again a bunch of values that are going to be the same.
We will right away rename it.
We'll say create job.
And as far as this request, we want to change some values.
First, we're going to go with post and then we want to look for body.
Then we'll set up company and position.
Now, this is totally up to you.
I'm going to go with Facebook here.
And then as far as the position, let's go with DevOps.
Why not?
So we're looking for the position here.
And then let's write DevOps and if everything is correct, we should get back this single job with an
ID company and position basically what we just provided in the body.
And not only that, if I go to all jobs, check it out now I have three of them over here.
Again, if I'm going to stop the server right now, of course, we'll go back to those two jobs because
we're not persisting this data anywhere.
And as a quick side note, notice how cool are these logs here in the console?
So I can clearly see that, hey, listen, there is a post request.
This is the URL and this was my response.
Now if I'll try to add a.
Job without, for example, the position.
What do you think is going to happen?
First of all, we need to make sure that our syntax is correct over here.
And if I send notice again, it was a post request, it went to API version one jobs, but the response
was different.
The response was 400.
So now of course I have please provide company and position.
And again, lastly, let me just showcase what happens if we don't have this return over here.
Now, of course you can return this whole rest dot status or you can simply place return after again.
That part is totally irrelevant.
What you don't want to do is this.
And, you know, let me switch back to Nodemon.
It doesn't really matter, but I'm just going to use this one because so far I've been using the watch
option and then let me navigate back to create job and I'll send with only one and notice you'll have
this giant error in the console cannot set headers after they are sent.
So again, effectively JavaScript just keeps reading the code and even though.
We had a condition over here.
And even though we already sent a response, well, we keep working on our job and then we send another
response and that's not something we are allowed to do.
So therefore, add here return and everything is going to be okay.
And with this in place, we are done with create job functionality.
And up next we can set up get single job functionality.