1
00:00:00,470 --> 00:00:00,680
Next.

2
00:00:00,680 --> 00:00:03,260
Let's work on get single job.

3
00:00:03,680 --> 00:00:08,960
And the first thing we need to understand is that whenever we're talking about single job, it's going

4
00:00:08,960 --> 00:00:11,090
to be based on something.

5
00:00:11,300 --> 00:00:15,530
If we have a list, I mean, you're not going to send some random job, correct?

6
00:00:15,710 --> 00:00:23,690
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

7
00:00:23,690 --> 00:00:25,440
route parameters.

8
00:00:25,460 --> 00:00:26,390
How do they work?

9
00:00:26,390 --> 00:00:28,510
Well, again, we go with URL.

10
00:00:28,520 --> 00:00:34,040
And in this case, yes, it's going to be pretty much the same URL API version one jobs.

11
00:00:34,040 --> 00:00:36,810
But then we go with Forward Slash and then colon.

12
00:00:36,830 --> 00:00:39,350
Now this name is irrelevant.

13
00:00:39,380 --> 00:00:44,060
Whatever you name this, you'll have access to it in Req.params.

14
00:00:44,060 --> 00:00:46,610
So this is the case where we don't need to install anything.

15
00:00:46,610 --> 00:00:49,070
We don't need to set up the middleware, nothing like that.

16
00:00:49,070 --> 00:00:50,750
It's provided by default.

17
00:00:51,530 --> 00:00:54,370
And again, it's a route parameter.

18
00:00:54,380 --> 00:01:00,050
And if you write here, colon, banana in Req.params, you'll have a banana.

19
00:01:00,080 --> 00:01:02,060
If you write here, shake and bake.

20
00:01:02,060 --> 00:01:04,069
Hopefully you see where I'm going with this.

21
00:01:04,099 --> 00:01:06,500
So in our case, we'll write ID.

22
00:01:06,920 --> 00:01:09,110
So essentially this will be dynamic.

23
00:01:09,140 --> 00:01:15,210
Now when we send this, of course we won't use the colon, so we'll just provide the ID there.

24
00:01:15,230 --> 00:01:20,240
But in order to access it, we first need to set up a route parameter and this is going to be available

25
00:01:20,240 --> 00:01:22,190
in Req.params.

26
00:01:22,220 --> 00:01:26,630
We'll grab that single job based on the ID.

27
00:01:26,900 --> 00:01:30,800
If for some reason the user is sending a wrong.

28
00:01:31,640 --> 00:01:32,000
ID?

29
00:01:32,120 --> 00:01:33,500
Well, then we'll have to respond.

30
00:01:33,500 --> 00:01:40,250
We'll say, Hey, listen, there is no job with such ID Now, If we're successful, then of course we

31
00:01:40,250 --> 00:01:42,500
will send back that single job.

32
00:01:42,530 --> 00:01:43,940
So let's try it out.

33
00:01:44,240 --> 00:01:45,710
Let's navigate over here.

34
00:01:46,280 --> 00:01:51,170
Again, I think I'll reuse some of these values, so I'll just copy first.

35
00:01:51,200 --> 00:01:52,880
We're not creating a job.

36
00:01:53,360 --> 00:01:57,620
We'll right over here get a single job.

37
00:01:57,710 --> 00:01:58,400
Then.

38
00:01:58,400 --> 00:02:00,490
As far as the method, it's a get.

39
00:02:00,500 --> 00:02:02,840
So again, we are requesting a resource.

40
00:02:02,840 --> 00:02:07,220
We're not creating one and therefore we're going to go over here with get.

41
00:02:08,039 --> 00:02:13,770
Then we want to set up that root parameter and therefore we're going to go with forward Slash and then

42
00:02:13,770 --> 00:02:14,880
colon ID.

43
00:02:15,360 --> 00:02:23,370
Like I said, it's going to be located in the rec dot params and of course you can log the entire object

44
00:02:23,370 --> 00:02:26,430
if you want, but in my case I'm just going to go with the ID.

45
00:02:27,550 --> 00:02:29,710
And we'll look for Req.params.

46
00:02:29,740 --> 00:02:32,650
Again, all of this is built into Express.

47
00:02:32,740 --> 00:02:34,540
Then let's access it.

48
00:02:34,720 --> 00:02:36,110
We're going to go with Job.

49
00:02:36,130 --> 00:02:37,570
Then jobs.

50
00:02:37,570 --> 00:02:38,650
That's our array.

51
00:02:38,650 --> 00:02:42,730
We can use the JavaScript method by the name of Find.

52
00:02:42,920 --> 00:02:45,940
And in here we need to pass in the callback function.

53
00:02:46,690 --> 00:02:52,750
As far as the parameter for our callback function, I'm going to name this job and I'll say if the job

54
00:02:52,750 --> 00:02:58,870
ID matches the ID I'm getting from Req.params, well, get me that job.

55
00:02:58,900 --> 00:03:06,490
Now, if there is no job, if for some reason the ID is wrong, well, we need to send back some response.

56
00:03:06,490 --> 00:03:06,970
Correct.

57
00:03:06,970 --> 00:03:11,890
So again, we'll go here right away with the return rest dot status.

58
00:03:11,890 --> 00:03:17,860
And normally when the resource is missing, you go with rest status 404.

59
00:03:17,860 --> 00:03:25,330
And as a quick side note, when we are creating a resource, a proper status is 201.

60
00:03:25,360 --> 00:03:25,840
My bad.

61
00:03:25,840 --> 00:03:28,630
So in the previous video I set up 200.

62
00:03:28,630 --> 00:03:36,220
But whenever you create a resource, you go with 201 and after that we want to go Json and this is going

63
00:03:36,220 --> 00:03:43,150
to be again the case where we'll be sending back messages because we'll use a toast library to display

64
00:03:43,150 --> 00:03:44,620
them on our front end.

65
00:03:44,620 --> 00:03:50,000
So if, let's say user is trying to do something and if there's an error coming back from the server

66
00:03:50,000 --> 00:03:56,210
we want to nicely display and therefore we'll keep the same syntax where essentially we'll send back

67
00:03:56,210 --> 00:04:02,990
Json and in there there's going to be a message property and we'll just be sticking those error messages

68
00:04:02,990 --> 00:04:04,010
there as well.

69
00:04:04,010 --> 00:04:12,800
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

70
00:04:12,920 --> 00:04:14,750
and then at the very end, what do we want to do?

71
00:04:14,750 --> 00:04:20,360
Well, we want to send back the job if the ID.

72
00:04:21,300 --> 00:04:22,350
Was the correct one.

73
00:04:22,350 --> 00:04:24,930
So go over here with status.

74
00:04:24,930 --> 00:04:30,550
It's 200 in this case, then Json and then we'll send back the job.

75
00:04:30,570 --> 00:04:34,950
Now let's navigate to our job routes.

76
00:04:35,130 --> 00:04:36,810
Let's again duplicate.

77
00:04:38,290 --> 00:04:41,260
Since we'll be able to reuse everything.

78
00:04:42,420 --> 00:04:44,820
This is going to be the copy we're looking for.

79
00:04:44,850 --> 00:04:45,990
Get request.

80
00:04:46,170 --> 00:04:47,490
It's the jobs.

81
00:04:47,520 --> 00:04:49,380
Now, of course, we can remove everything.

82
00:04:49,380 --> 00:04:51,550
Pretty much what we have in the body.

83
00:04:51,570 --> 00:04:52,800
We won't use it.

84
00:04:52,800 --> 00:04:57,060
And as far as the URL, we have the global variable.

85
00:04:57,090 --> 00:04:58,260
We have the jobs.

86
00:04:58,260 --> 00:04:59,790
So all of that is correct.

87
00:04:59,820 --> 00:05:01,590
What's missing is the ID?

88
00:05:02,070 --> 00:05:02,600
Correct.

89
00:05:02,610 --> 00:05:11,190
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

90
00:05:11,190 --> 00:05:16,560
create job copy, effectively we want to pass over here this ID.

91
00:05:17,700 --> 00:05:19,320
The method, like I said, is get.

92
00:05:19,320 --> 00:05:23,250
And if everything is correct, we should have access to this job.

93
00:05:24,010 --> 00:05:29,140
Now notice how I actually get back the error response.

94
00:05:29,170 --> 00:05:30,160
Now, why is that?

95
00:05:30,160 --> 00:05:30,310
Well.

96
00:05:30,340 --> 00:05:35,970
Because, remember, pretty much every time we stop the server, we will set up everything from scratch.

97
00:05:35,980 --> 00:05:37,410
So here's what we want to do.

98
00:05:37,420 --> 00:05:41,440
We want to send another request to get all jobs.

99
00:05:41,440 --> 00:05:45,730
So again, in this case, we have only two of them because we started from scratch.

100
00:05:45,880 --> 00:05:49,210
And no, I'm not going to add a job right now.

101
00:05:49,210 --> 00:05:52,210
We already saw that the functionality works.

102
00:05:52,240 --> 00:05:58,060
Now again, I want to grab the ID, navigate back to get single job.

103
00:05:58,060 --> 00:06:03,610
For some reason I wasn't able to rename it, so let me do it right now.

104
00:06:06,330 --> 00:06:09,750
And then let's change the over here.

105
00:06:09,840 --> 00:06:11,970
So forward, Slash, let's send it.

106
00:06:11,970 --> 00:06:15,090
And if everything is correct, we'll get back the matching job.

107
00:06:16,010 --> 00:06:16,580
Awesome.

108
00:06:16,580 --> 00:06:19,090
And now let's try it out again one more time with error.

109
00:06:19,100 --> 00:06:21,980
Let me add some nonsense here and now.

110
00:06:22,010 --> 00:06:27,890
Of course, I'll have the 404 response with no job, with ID, such and such.

111
00:06:27,890 --> 00:06:33,320
And with this in place we can work on the edit job functionality.

