1
00:00:00,170 --> 00:00:00,560
All right.

2
00:00:00,560 --> 00:00:06,440
And up next, let's go down the list and let's set up for more requests.

3
00:00:06,590 --> 00:00:14,270
So we want to have one for create job, find one job, basically get one single job based on the ID,

4
00:00:14,660 --> 00:00:22,520
modify the existing job again based on the ID, and then lastly, delete a single job.

5
00:00:22,880 --> 00:00:25,970
And I guess let's start with the create job.

6
00:00:25,970 --> 00:00:28,400
So let's navigate back to our project.

7
00:00:28,400 --> 00:00:28,940
And you know what?

8
00:00:28,940 --> 00:00:30,690
In here I will add some comments.

9
00:00:30,710 --> 00:00:32,840
I'll say, get all jobs.

10
00:00:32,870 --> 00:00:34,130
That's the request.

11
00:00:34,400 --> 00:00:37,850
And now let's copy and paste since a lot of the stuff is going to be.

12
00:00:38,570 --> 00:00:39,710
Very similar.

13
00:00:40,420 --> 00:00:41,140
As far as the.

14
00:00:41,650 --> 00:00:48,550
Yes, we'll have this one to get all the jobs on the URL for creating a job is actually going to be

15
00:00:48,550 --> 00:00:49,720
exactly the same.

16
00:00:49,720 --> 00:00:52,960
So it's going to be API version one and then jobs.

17
00:00:52,960 --> 00:00:56,050
So they both go to the same place.

18
00:00:56,050 --> 00:01:01,260
But since the method is different, of course the request is different.

19
00:01:01,270 --> 00:01:03,490
So in this case we are creating a job.

20
00:01:03,490 --> 00:01:04,569
So create.

21
00:01:05,550 --> 00:01:06,210
Job.

22
00:01:07,270 --> 00:01:13,660
Now, as far as the functionality, we want to first grab some values from Req.body because keep in

23
00:01:13,660 --> 00:01:21,070
mind, in order to create a job, well, we need to provide what we need to provide company and position.

24
00:01:21,100 --> 00:01:23,920
Now, typically this gets set.

25
00:01:24,800 --> 00:01:29,550
In the database and therefore we will create that ID in our controller again.

26
00:01:29,570 --> 00:01:32,910
Eventually the database will do that job.

27
00:01:32,930 --> 00:01:36,320
But for now, we'll do it over here in Create Job.

28
00:01:36,350 --> 00:01:42,170
So first we want to grab from the Req.body Body company and position.

29
00:01:42,260 --> 00:01:44,770
How do I know that those values are going to be there?

30
00:01:44,780 --> 00:01:51,270
Well, because we will be setting up that request in a second in the Thunder client.

31
00:01:51,290 --> 00:01:51,620
Correct.

32
00:01:51,620 --> 00:01:53,420
So we're going to go here with the Req.body.

33
00:01:53,420 --> 00:01:56,180
Remember, that's where the Json data is located.

34
00:01:56,220 --> 00:02:03,440
And we want to set up a check where if the value is missing, if one of them is missing, then essentially

35
00:02:03,440 --> 00:02:05,680
we'll send back a 400.

36
00:02:05,690 --> 00:02:12,050
And essentially when it comes to status codes, 400 stands for bad request.

37
00:02:12,050 --> 00:02:20,030
So before we create the job based on company and position, we want to check if those values are present.

38
00:02:20,060 --> 00:02:26,580
Now, eventually we'll set up a layer which is going to be sitting on top of our requests.

39
00:02:26,580 --> 00:02:31,230
But for now, yes, we'll do this manually in the controller.

40
00:02:31,230 --> 00:02:36,750
So we'll say if the company is missing or the position is missing, here's what I want to do.

41
00:02:37,480 --> 00:02:39,880
I want to send back a different request.

42
00:02:40,120 --> 00:02:43,450
And in order to speed this up, I'll just copy some values.

43
00:02:43,460 --> 00:02:48,070
So I'll take it from here and it's going to be 400 again.

44
00:02:48,070 --> 00:02:49,780
That's a bad request.

45
00:02:49,930 --> 00:02:55,870
And we do want to add here a return, because please keep in mind that if the return is going to be

46
00:02:55,870 --> 00:02:59,530
missing over here, you'll get an error from Express.

47
00:02:59,770 --> 00:03:02,590
Essentially, it's because JavaScript just keeps reading the code.

48
00:03:02,590 --> 00:03:08,530
So if you have no return over here in this condition, then you'll have two responses and express is

49
00:03:08,530 --> 00:03:10,000
not going to be happy about that.

50
00:03:10,420 --> 00:03:17,320
And then as far as the Json, well we'll send back a message property and we'll say please provide.

51
00:03:18,210 --> 00:03:18,960
Company.

52
00:03:20,370 --> 00:03:21,480
And position.

53
00:03:21,570 --> 00:03:22,740
Let's save this.

54
00:03:22,740 --> 00:03:29,640
And if everything is correct, then of course, we continue setting up our new job.

55
00:03:30,060 --> 00:03:36,030
And first we'll start with an ID and technically we'll cheat a little bit because again, database usually

56
00:03:36,030 --> 00:03:37,020
does that job.

57
00:03:37,020 --> 00:03:43,290
But in our case we're going to go with const ID is equal to then let's invoke nano ID and then just

58
00:03:43,290 --> 00:03:46,890
provide some number value how big that ID is going to be.

59
00:03:46,890 --> 00:03:49,350
And I'm going to go with ten over here.

60
00:03:49,380 --> 00:03:52,560
Then we want to create that new job.

61
00:03:52,650 --> 00:03:54,270
So go over here with ID.

62
00:03:55,520 --> 00:03:58,010
Company and position.

63
00:03:58,010 --> 00:03:59,990
So I'm creating a new object.

64
00:04:00,320 --> 00:04:05,960
ID is equal to ID, company is equal to company position is equal to a position.

65
00:04:06,140 --> 00:04:08,870
This is what we can do in JavaScript these days.

66
00:04:08,900 --> 00:04:13,310
And then we just want to add this job manually to our jobs array.

67
00:04:13,520 --> 00:04:15,230
Now, please keep in mind one thing.

68
00:04:15,260 --> 00:04:19,250
Of course this just runs when the server is running.

69
00:04:19,399 --> 00:04:20,640
Hopefully that is clear.

70
00:04:20,660 --> 00:04:24,260
Eventually, again, everything is going to be persisted in the database.

71
00:04:24,260 --> 00:04:29,660
So in here we just want to go with jobs dot push job and then as far as the response.

72
00:04:30,260 --> 00:04:35,090
Typically when you create a job, you don't send back all of the jobs like we just did.

73
00:04:35,240 --> 00:04:37,160
You go with one job.

74
00:04:37,380 --> 00:04:40,850
And now we want to navigate to our collections.

75
00:04:40,910 --> 00:04:43,740
And in here, we want to set up a new one.

76
00:04:43,760 --> 00:04:50,210
So in this case, I'll duplicate again a bunch of values that are going to be the same.

77
00:04:50,240 --> 00:04:52,550
We will right away rename it.

78
00:04:52,580 --> 00:04:55,340
We'll say create job.

79
00:04:56,300 --> 00:04:58,650
And as far as this request, we want to change some values.

80
00:04:58,670 --> 00:05:04,100
First, we're going to go with post and then we want to look for body.

81
00:05:04,550 --> 00:05:07,700
Then we'll set up company and position.

82
00:05:08,000 --> 00:05:09,770
Now, this is totally up to you.

83
00:05:09,800 --> 00:05:11,750
I'm going to go with Facebook here.

84
00:05:11,810 --> 00:05:16,190
And then as far as the position, let's go with DevOps.

85
00:05:16,760 --> 00:05:17,450
Why not?

86
00:05:17,480 --> 00:05:19,460
So we're looking for the position here.

87
00:05:20,420 --> 00:05:27,440
And then let's write DevOps and if everything is correct, we should get back this single job with an

88
00:05:27,440 --> 00:05:33,170
ID company and position basically what we just provided in the body.

89
00:05:33,660 --> 00:05:40,080
And not only that, if I go to all jobs, check it out now I have three of them over here.

90
00:05:40,100 --> 00:05:46,490
Again, if I'm going to stop the server right now, of course, we'll go back to those two jobs because

91
00:05:46,490 --> 00:05:48,800
we're not persisting this data anywhere.

92
00:05:48,800 --> 00:05:54,170
And as a quick side note, notice how cool are these logs here in the console?

93
00:05:54,170 --> 00:05:59,210
So I can clearly see that, hey, listen, there is a post request.

94
00:05:59,220 --> 00:06:02,000
This is the URL and this was my response.

95
00:06:02,030 --> 00:06:05,780
Now if I'll try to add a.

96
00:06:06,420 --> 00:06:10,170
Job without, for example, the position.

97
00:06:10,170 --> 00:06:11,550
What do you think is going to happen?

98
00:06:11,700 --> 00:06:15,330
First of all, we need to make sure that our syntax is correct over here.

99
00:06:15,330 --> 00:06:23,280
And if I send notice again, it was a post request, it went to API version one jobs, but the response

100
00:06:23,280 --> 00:06:24,120
was different.

101
00:06:24,150 --> 00:06:25,860
The response was 400.

102
00:06:25,890 --> 00:06:31,050
So now of course I have please provide company and position.

103
00:06:31,050 --> 00:06:36,480
And again, lastly, let me just showcase what happens if we don't have this return over here.

104
00:06:36,510 --> 00:06:43,200
Now, of course you can return this whole rest dot status or you can simply place return after again.

105
00:06:43,200 --> 00:06:45,420
That part is totally irrelevant.

106
00:06:45,420 --> 00:06:47,610
What you don't want to do is this.

107
00:06:48,270 --> 00:06:51,240
And, you know, let me switch back to Nodemon.

108
00:06:51,270 --> 00:06:57,630
It doesn't really matter, but I'm just going to use this one because so far I've been using the watch

109
00:06:57,660 --> 00:07:05,220
option and then let me navigate back to create job and I'll send with only one and notice you'll have

110
00:07:05,220 --> 00:07:11,900
this giant error in the console cannot set headers after they are sent.

111
00:07:11,910 --> 00:07:17,670
So again, effectively JavaScript just keeps reading the code and even though.

112
00:07:18,270 --> 00:07:19,890
We had a condition over here.

113
00:07:19,890 --> 00:07:25,170
And even though we already sent a response, well, we keep working on our job and then we send another

114
00:07:25,170 --> 00:07:28,710
response and that's not something we are allowed to do.

115
00:07:28,710 --> 00:07:33,900
So therefore, add here return and everything is going to be okay.

116
00:07:33,960 --> 00:07:38,280
And with this in place, we are done with create job functionality.

117
00:07:38,400 --> 00:07:44,040
And up next we can set up get single job functionality.

