1
00:00:00,020 --> 00:00:00,350
All right.

2
00:00:00,350 --> 00:00:07,400
And the last thing that I want to do before we start working with the database is to set up our controller.

3
00:00:07,640 --> 00:00:11,390
The job controller, as well as the job router.

4
00:00:11,390 --> 00:00:12,890
Because take a look.

5
00:00:12,920 --> 00:00:16,700
Our server is getting quite busy.

6
00:00:17,420 --> 00:00:24,260
And effectively we just want to come up with some kind of structure where we can have the functionality

7
00:00:24,560 --> 00:00:27,290
aka our controllers in one place.

8
00:00:27,320 --> 00:00:34,700
Also, we want to set up the router so we can nicely group requests in a different file and as a result

9
00:00:34,730 --> 00:00:42,170
our server JS is going to have less code because keep in mind we still need to create quite a few requests

10
00:00:42,170 --> 00:00:45,950
and just jamming them in one file.

11
00:00:46,580 --> 00:00:50,050
In the long run, it's probably not the best idea.

12
00:00:50,060 --> 00:00:51,290
So let's start over here.

13
00:00:51,290 --> 00:00:53,960
By creating a new folder.

14
00:00:54,290 --> 00:00:58,610
And a common convention is to call this controllers.

15
00:00:58,640 --> 00:00:59,330
Again.

16
00:01:00,010 --> 00:01:02,890
Technically you don't have to, but it is a convention.

17
00:01:02,890 --> 00:01:05,860
So we go over here with controllers and we need to come up with a name.

18
00:01:05,860 --> 00:01:09,820
So in my case, I'm going to go with job controller.

19
00:01:09,910 --> 00:01:11,920
So this is what we're creating.

20
00:01:12,070 --> 00:01:18,820
And eventually, yes, of course we'll have functionality in here to communicate with the database.

21
00:01:18,820 --> 00:01:20,620
Now at the moment we don't have that.

22
00:01:20,620 --> 00:01:26,980
So what I'm going to do is just copy and paste for now temporarily, because we will remove this code,

23
00:01:27,700 --> 00:01:30,250
the jobs and the nano ID.

24
00:01:30,520 --> 00:01:36,280
Again, I just want to showcase how it works and since I'm not going to use it directly in the server,

25
00:01:36,280 --> 00:01:41,350
I'll just cut it out then we want to go to job controller, copy and paste.

26
00:01:41,350 --> 00:01:45,250
So again, we're just looking for this 900 ID just so we can create the jobs.

27
00:01:45,250 --> 00:01:54,640
And then one by one, let's set up the functions that are essentially going to be our controllers.

28
00:01:54,760 --> 00:02:01,190
We'll just set it equal to a function so we can export and then import in our router.

29
00:02:01,190 --> 00:02:02,960
So what am I talking about?

30
00:02:03,050 --> 00:02:09,229
Notice how pretty much we have these requests and we have the URL part and then the controller.

31
00:02:09,229 --> 00:02:13,550
So now I want to move this logic to a separate file.

32
00:02:13,550 --> 00:02:15,020
So that's what we're doing.

33
00:02:15,020 --> 00:02:18,140
And basically first I just want to create those functions.

34
00:02:18,620 --> 00:02:23,000
We'll start with export since again, we'll export it away from this.

35
00:02:23,730 --> 00:02:24,410
File.

36
00:02:24,420 --> 00:02:27,570
I'm going to call this get all jobs.

37
00:02:28,660 --> 00:02:31,570
And we want to set it equal to this.

38
00:02:31,570 --> 00:02:38,970
So essentially take this entire thing out and you're not going to import right away back into server.

39
00:02:38,980 --> 00:02:40,810
No, we'll set up the router.

40
00:02:40,810 --> 00:02:43,180
So there's going to be something in between.

41
00:02:43,360 --> 00:02:46,210
And since eventually we will have logic.

42
00:02:47,060 --> 00:02:49,670
Which is going to require the await.

43
00:02:49,700 --> 00:02:51,820
I'll right away set it as async.

44
00:02:51,830 --> 00:02:56,660
Please keep in mind that of course, in this case, technically we're not using that functionality when

45
00:02:56,660 --> 00:03:00,800
we want to go with create job and one by one pretty much will repeat.

46
00:03:00,800 --> 00:03:02,930
So again, this is going to be async.

47
00:03:02,960 --> 00:03:05,390
We want to swing back to the server.

48
00:03:05,510 --> 00:03:07,700
Let's take this one out over here.

49
00:03:09,010 --> 00:03:10,160
Drop controller.

50
00:03:10,180 --> 00:03:12,040
Yep, that's the one.

51
00:03:12,460 --> 00:03:15,790
After that, we have get job or get single job.

52
00:03:15,820 --> 00:03:19,180
That's also a name I use.

53
00:03:19,900 --> 00:03:21,530
So back to the server.

54
00:03:22,670 --> 00:03:24,200
And controller out.

55
00:03:24,860 --> 00:03:27,650
Set it up over here again, async.

56
00:03:27,680 --> 00:03:28,990
Make sure you have that.

57
00:03:29,000 --> 00:03:31,190
I mean, technically you can add it later, but.

58
00:03:31,990 --> 00:03:34,210
I just think it's easier if we do it right now.

59
00:03:34,510 --> 00:03:36,700
Let me grab the edit one.

60
00:03:37,330 --> 00:03:38,330
Cut it out.

61
00:03:38,350 --> 00:03:40,600
I'm going to call this get.

62
00:03:40,840 --> 00:03:41,550
Or I'm sorry.

63
00:03:41,560 --> 00:03:42,370
Update job.

64
00:03:42,370 --> 00:03:44,170
And then the last one will be delete job.

65
00:03:44,620 --> 00:03:48,610
So export const update job.

66
00:03:49,770 --> 00:03:50,580
Async.

67
00:03:50,610 --> 00:03:51,840
Copy and paste.

68
00:03:51,840 --> 00:03:55,710
And then lastly, let's do the same thing with the delete.

69
00:03:56,160 --> 00:03:57,570
So let's get it out.

70
00:03:57,600 --> 00:03:59,040
Job controller.

71
00:03:59,970 --> 00:04:01,530
Export const.

72
00:04:01,560 --> 00:04:03,480
Delete job.

73
00:04:04,600 --> 00:04:06,880
Async and copy and paste.

74
00:04:07,390 --> 00:04:09,460
Okay, now we have access to it.

75
00:04:09,460 --> 00:04:16,000
And for some reason, of course, I have the error because I removed a little bit too much.

76
00:04:16,510 --> 00:04:17,320
Okay, my bad.

77
00:04:17,320 --> 00:04:18,910
Let me go back over here.

78
00:04:20,240 --> 00:04:22,340
Let me first save the server.

79
00:04:22,420 --> 00:04:23,750
Guess let me close it.

80
00:04:23,750 --> 00:04:26,930
And then in the job controller, I'm not going to take the use.

81
00:04:27,170 --> 00:04:27,950
My bad.

82
00:04:28,070 --> 00:04:29,750
Let me take this one out.

83
00:04:29,750 --> 00:04:33,830
And there's also an extra parenthese, which at the moment I have.

84
00:04:34,160 --> 00:04:36,650
So let me set up the curly instead.

85
00:04:36,680 --> 00:04:39,350
Then back in the server let's add.

86
00:04:40,430 --> 00:04:42,110
Before the error one.

87
00:04:42,200 --> 00:04:43,310
This one over here.

88
00:04:43,370 --> 00:04:46,070
Let me add the parentheses so everything is correct.

89
00:04:46,070 --> 00:04:48,890
And now we want to create our first router.

90
00:04:49,160 --> 00:04:58,400
So notice all of these requests are technically going to the same base URL, so we still have API version

91
00:04:58,400 --> 00:05:07,130
one jobs and then pretty much everything after that depends if it's a request which requires the root

92
00:05:07,130 --> 00:05:07,430
param.

93
00:05:07,460 --> 00:05:08,210
Of course we add it.

94
00:05:08,210 --> 00:05:12,950
If not, then it's still API version one job.

95
00:05:12,950 --> 00:05:19,700
So we could nicely group them together and therefore we're going to create another folder and this one

96
00:05:19,700 --> 00:05:20,960
will call router.

97
00:05:22,190 --> 00:05:25,130
Or roots, you know, let's go with the routes.

98
00:05:25,310 --> 00:05:26,990
Then instead of the routes.

99
00:05:26,990 --> 00:05:29,360
One, let's create a new file.

100
00:05:30,540 --> 00:05:35,130
And I'm going to go with Job router and here's what we want to do.

101
00:05:35,160 --> 00:05:39,900
We want to first import router from Express and we want to invoke it.

102
00:05:40,170 --> 00:05:41,880
So go over here.

103
00:05:42,240 --> 00:05:43,050
Router.

104
00:05:43,060 --> 00:05:43,620
Yep.

105
00:05:43,620 --> 00:05:45,240
That's the import from.

106
00:05:46,730 --> 00:05:47,570
Express.

107
00:05:49,020 --> 00:05:49,680
Then.

108
00:05:50,330 --> 00:05:52,550
And as a side note, my bad.

109
00:05:52,970 --> 00:05:57,860
Once in a while I just keep typing the common commonjs syntax.

110
00:05:57,860 --> 00:05:58,430
My apologies.

111
00:05:58,430 --> 00:06:00,100
So import from express.

112
00:06:00,110 --> 00:06:02,300
That's the ES6 modules.

113
00:06:02,300 --> 00:06:04,010
So const router.

114
00:06:04,870 --> 00:06:07,870
Is equal to router.

115
00:06:07,900 --> 00:06:08,980
Let's invoke it.

116
00:06:09,100 --> 00:06:14,290
And now, essentially with this router instance, we can do the same thing.

117
00:06:14,380 --> 00:06:22,240
We can set up router, dot, get router, dot post and effectively we want to import those controllers

118
00:06:22,750 --> 00:06:26,800
and make sure that they handle the proper route.

119
00:06:26,890 --> 00:06:30,250
Now, first guess, let's start by grabbing.

120
00:06:31,190 --> 00:06:36,020
All of the controllers, but just make sure that it's coming from the same place.

121
00:06:36,020 --> 00:06:38,130
So we want to go one level up.

122
00:06:38,150 --> 00:06:43,010
We're looking for the controllers, then job controller.

123
00:06:43,010 --> 00:06:48,770
Just make sure you add the JS again, otherwise you'll have the error and then in here.

124
00:06:49,650 --> 00:06:51,140
What functions are we looking for?

125
00:06:51,150 --> 00:06:52,650
Well, get all jobs.

126
00:06:52,800 --> 00:06:54,720
We want to get the job.

127
00:06:55,390 --> 00:06:59,350
Create the job, then update the job.

128
00:07:00,400 --> 00:07:02,620
And delete the job.

129
00:07:03,100 --> 00:07:08,170
Then once I have the controllers, effectively we have two approaches.

130
00:07:08,170 --> 00:07:14,650
You can use this one where again you go with a router just like we did with app and let's say for get

131
00:07:14,650 --> 00:07:16,390
all jobs, what is going to be the method?

132
00:07:16,390 --> 00:07:18,280
Well, it's going to be get correct.

133
00:07:18,310 --> 00:07:28,180
Now eventually I'll point the router to API version one jobs to basically this base URL over here and

134
00:07:28,180 --> 00:07:31,030
therefore this is going to be the forward slash.

135
00:07:31,030 --> 00:07:36,610
And essentially I'll say, okay, so this is going to be get all jobs and then in order to create the

136
00:07:36,610 --> 00:07:39,130
job, what is the difference between the URLs?

137
00:07:39,130 --> 00:07:40,330
Well, there is no difference.

138
00:07:40,330 --> 00:07:41,740
The only difference is the method.

139
00:07:41,740 --> 00:07:42,220
Correct.

140
00:07:42,220 --> 00:07:44,020
So we just go here with post.

141
00:07:44,020 --> 00:07:52,360
So this is one approach, but actually like a different approach because that way I can chain the controllers.

142
00:07:52,360 --> 00:07:53,680
So what am I talking about?

143
00:07:53,680 --> 00:07:54,910
And as a side note.

144
00:07:55,530 --> 00:07:56,370
This is not correct.

145
00:07:56,370 --> 00:07:57,450
So create job.

146
00:07:57,480 --> 00:07:59,820
Remember, controller was also different.

147
00:08:00,060 --> 00:08:05,130
Now we also have this route, so I can go with router dot route.

148
00:08:05,130 --> 00:08:09,930
And then again, since this is going to be the base one, something you'll see in a second I'm going

149
00:08:09,930 --> 00:08:15,990
to go with just forward slash and then I can chain what method do I want to use for get all jobs?

150
00:08:15,990 --> 00:08:17,640
Well, it's a get one correct.

151
00:08:17,640 --> 00:08:19,490
So we go with get all jobs.

152
00:08:19,500 --> 00:08:24,900
Then we can also chain to the same one because again, the URL is the same.

153
00:08:24,900 --> 00:08:29,760
I know it's probably a little bit redundant and annoying, but I do have to repeat that.

154
00:08:30,180 --> 00:08:31,320
And in this case.

155
00:08:31,890 --> 00:08:34,140
The difference is that the method is post.

156
00:08:34,140 --> 00:08:36,250
And now of course we're using create job.

157
00:08:36,270 --> 00:08:40,799
Again, I just like this setup because it's just less lines of code.

158
00:08:40,830 --> 00:08:41,760
That's it.

159
00:08:41,820 --> 00:08:45,090
There's really no magic behind it.

160
00:08:45,180 --> 00:08:47,980
You can use this approach as well if you want.

161
00:08:48,000 --> 00:08:51,510
So in this case we have router dot route.

162
00:08:51,600 --> 00:08:57,420
But what is the additional thing we need to add over here for get job update, job and delete job.

163
00:08:57,420 --> 00:08:59,190
Well it's the route param.

164
00:08:59,400 --> 00:09:00,060
Correct.

165
00:09:00,060 --> 00:09:05,130
So we just go with forward slash and then the ID and then one by one, let's set them up.

166
00:09:05,160 --> 00:09:07,260
This is not going to be get all jobs.

167
00:09:07,530 --> 00:09:10,440
It will be get job then.

168
00:09:10,440 --> 00:09:15,450
It's not a post one, it's a patch one and we want to go with update job.

169
00:09:15,450 --> 00:09:21,420
And then lastly, let's go with dot delete and we're going to delete the job now.

170
00:09:21,420 --> 00:09:22,770
Very, very important.

171
00:09:22,800 --> 00:09:25,740
We want to export default.

172
00:09:26,880 --> 00:09:28,380
And the router.

173
00:09:28,380 --> 00:09:34,200
So our router instance, let's export it and back in the server.

174
00:09:35,050 --> 00:09:36,700
Somewhere here you want to import.

175
00:09:36,700 --> 00:09:40,250
And I always like to keep import separate.

176
00:09:40,270 --> 00:09:43,990
Basically, these are going to be imports for packages.

177
00:09:43,990 --> 00:09:49,930
And then I'll have technically my custom imports, which essentially just means that these are going

178
00:09:49,930 --> 00:09:52,840
to be things that are going to be coming from my own file.

179
00:09:53,640 --> 00:09:56,100
Again, you can put them all together.

180
00:09:56,100 --> 00:09:57,420
It doesn't really matter.

181
00:09:58,300 --> 00:10:06,040
So let me go over here and say routers and we'll start with import then.

182
00:10:06,160 --> 00:10:10,020
Name is up to you since we're setting this up as a default export.

183
00:10:10,030 --> 00:10:14,530
In my case, I'm going to go with a router and in this case I have dot and forward slash since it's

184
00:10:14,530 --> 00:10:19,120
in the same folder, then routes job router and again.

185
00:10:19,120 --> 00:10:27,880
Yes, add JS extension and then you want to set up app dot use again, it's going to be middleware and

186
00:10:27,880 --> 00:10:32,200
pretty much we want to add the API version one and jobs.

187
00:10:32,200 --> 00:10:36,580
So that's going to be the starting point for our router.

188
00:10:36,760 --> 00:10:42,430
And then from there, if it's a base route, then we handle it this way.

189
00:10:42,430 --> 00:10:46,000
If we have route param, then we'll use these controllers.

190
00:10:46,000 --> 00:10:48,430
So let me navigate back to the server.

191
00:10:49,310 --> 00:10:57,320
And pretty much we can remove all these ones the comments and get all the way up to delete.

192
00:10:57,350 --> 00:10:59,300
We won't use it over here.

193
00:10:59,300 --> 00:11:08,600
And in between the post and the 404, we can go with app dot use and essentially provide the full URL

194
00:11:08,630 --> 00:11:11,180
API version one job.

195
00:11:11,180 --> 00:11:16,780
So this is going to be the starting URL and which router is going to handle that?

196
00:11:16,790 --> 00:11:19,180
Well, in our case, that is going to be the job router.

197
00:11:19,190 --> 00:11:23,090
Now I'm not going to test each and every request over here.

198
00:11:23,910 --> 00:11:27,180
But I do want to check quickly the main ones.

199
00:11:27,180 --> 00:11:29,520
So let's go back to get all jobs.

200
00:11:29,520 --> 00:11:32,430
If everything is correct, we should see the list of jobs.

201
00:11:32,430 --> 00:11:35,280
And also I want to create the job.

202
00:11:35,280 --> 00:11:36,390
So let's go here.

203
00:11:36,390 --> 00:11:37,620
Let's send the request.

204
00:11:37,650 --> 00:11:39,120
Notice we have 201.

205
00:11:39,150 --> 00:11:40,440
We were successful.

206
00:11:40,440 --> 00:11:46,380
And then if I check all jobs, of course I'll have one more job added to the list.

207
00:11:46,620 --> 00:11:52,050
And if everything is correct, we can start working with our database.

