Next.
Let's work on get single job.
And the first thing we need to understand is that whenever we're talking about single job, it's going
to be based on something.
If we have a list, I mean, you're not going to send some random job, correct?
So in our case, we're going to send job based on the ID, And in order to set it up, we need to use
route parameters.
How do they work?
Well, again, we go with URL.
And in this case, yes, it's going to be pretty much the same URL API version one jobs.
But then we go with Forward Slash and then colon.
Now this name is irrelevant.
Whatever you name this, you'll have access to it in Req.params.
So this is the case where we don't need to install anything.
We don't need to set up the middleware, nothing like that.
It's provided by default.
And again, it's a route parameter.
And if you write here, colon, banana in Req.params, you'll have a banana.
If you write here, shake and bake.
Hopefully you see where I'm going with this.
So in our case, we'll write ID.
So essentially this will be dynamic.
Now when we send this, of course we won't use the colon, so we'll just provide the ID there.
But in order to access it, we first need to set up a route parameter and this is going to be available
in Req.params.
We'll grab that single job based on the ID.
If for some reason the user is sending a wrong.
ID?
Well, then we'll have to respond.
We'll say, Hey, listen, there is no job with such ID Now, If we're successful, then of course we
will send back that single job.
So let's try it out.
Let's navigate over here.
Again, I think I'll reuse some of these values, so I'll just copy first.
We're not creating a job.
We'll right over here get a single job.
Then.
As far as the method, it's a get.
So again, we are requesting a resource.
We're not creating one and therefore we're going to go over here with get.
Then we want to set up that root parameter and therefore we're going to go with forward Slash and then
colon ID.
Like I said, it's going to be located in the rec dot params and of course you can log the entire object
if you want, but in my case I'm just going to go with the ID.
And we'll look for Req.params.
Again, all of this is built into Express.
Then let's access it.
We're going to go with Job.
Then jobs.
That's our array.
We can use the JavaScript method by the name of Find.
And in here we need to pass in the callback function.
As far as the parameter for our callback function, I'm going to name this job and I'll say if the job
ID matches the ID I'm getting from Req.params, well, get me that job.
Now, if there is no job, if for some reason the ID is wrong, well, we need to send back some response.
Correct.
So again, we'll go here right away with the return rest dot status.
And normally when the resource is missing, you go with rest status 404.
And as a quick side note, when we are creating a resource, a proper status is 201.
My bad.
So in the previous video I set up 200.
But whenever you create a resource, you go with 201 and after that we want to go Json and this is going
to be again the case where we'll be sending back messages because we'll use a toast library to display
them on our front end.
So if, let's say user is trying to do something and if there's an error coming back from the server
we want to nicely display and therefore we'll keep the same syntax where essentially we'll send back
Json and in there there's going to be a message property and we'll just be sticking those error messages
there as well.
So in this case I'm going to go with no job and then with ID, and then let's access the ID from Req.params
and then at the very end, what do we want to do?
Well, we want to send back the job if the ID.
Was the correct one.
So go over here with status.
It's 200 in this case, then Json and then we'll send back the job.
Now let's navigate to our job routes.
Let's again duplicate.
Since we'll be able to reuse everything.
This is going to be the copy we're looking for.
Get request.
It's the jobs.
Now, of course, we can remove everything.
Pretty much what we have in the body.
We won't use it.
And as far as the URL, we have the global variable.
We have the jobs.
So all of that is correct.
What's missing is the ID?
Correct.
So let's navigate to get all jobs or we have list of jobs and we're looking for the ID and then in the
create job copy, effectively we want to pass over here this ID.
The method, like I said, is get.
And if everything is correct, we should have access to this job.
Now notice how I actually get back the error response.
Now, why is that?
Well.
Because, remember, pretty much every time we stop the server, we will set up everything from scratch.
So here's what we want to do.
We want to send another request to get all jobs.
So again, in this case, we have only two of them because we started from scratch.
And no, I'm not going to add a job right now.
We already saw that the functionality works.
Now again, I want to grab the ID, navigate back to get single job.
For some reason I wasn't able to rename it, so let me do it right now.
And then let's change the over here.
So forward, Slash, let's send it.
And if everything is correct, we'll get back the matching job.
Awesome.
And now let's try it out again one more time with error.
Let me add some nonsense here and now.
Of course, I'll have the 404 response with no job, with ID, such and such.
And with this in place we can work on the edit job functionality.