1
00:00:02,060 --> 00:00:04,730
So we had views, we added a model,

2
00:00:04,730 --> 00:00:08,480
now the controller part is missing for our blog

3
00:00:08,480 --> 00:00:11,450
or our post-related routes.

4
00:00:11,450 --> 00:00:13,080
Hence, that is what we'll add.

5
00:00:13,080 --> 00:00:15,740
And for this, I'll add a new sub folder in this project,

6
00:00:15,740 --> 00:00:17,500
which I'll name controllers.

7
00:00:17,500 --> 00:00:21,820
And in there, I'll add my post-controllers.js file

8
00:00:21,820 --> 00:00:25,000
because I wanna store all my post controllers in there.

9
00:00:25,000 --> 00:00:28,630
Now, actually, we'll only have one post controller

10
00:00:28,630 --> 00:00:31,108
because all the different functions

11
00:00:31,108 --> 00:00:33,160
that we need for the different routes

12
00:00:33,160 --> 00:00:36,210
will be called controller actions

13
00:00:36,210 --> 00:00:38,480
if we wanna be really precise.

14
00:00:38,480 --> 00:00:39,680
But still here for the moment,

15
00:00:39,680 --> 00:00:41,720
I'll name it post-controllers.

16
00:00:41,720 --> 00:00:45,956
Later, once we add a second controller for authentication,

17
00:00:45,956 --> 00:00:48,164
which we'll do later in this section.

18
00:00:48,164 --> 00:00:51,540
I'll rename this to then clarify that actually,

19
00:00:51,540 --> 00:00:53,178
we got two different controllers

20
00:00:53,178 --> 00:00:58,150
then for the two different areas of our application.

21
00:00:58,150 --> 00:01:00,040
Right now, since this is the only file,

22
00:01:00,040 --> 00:01:03,100
I'll treat the individual functions in there as controllers,

23
00:01:03,100 --> 00:01:05,092
even though technically these functions,

24
00:01:05,092 --> 00:01:07,550
which we are about to add in that file,

25
00:01:07,550 --> 00:01:10,989
will just be actions that belong to a single controller.

26
00:01:10,989 --> 00:01:13,510
But again, we'll change this later.

27
00:01:13,510 --> 00:01:16,017
And therefore, of course, if we have a look at our routes,

28
00:01:16,017 --> 00:01:18,540
these route handler functions,

29
00:01:18,540 --> 00:01:22,497
which we pass as second parameter values to our routes

30
00:01:22,497 --> 00:01:25,665
could be considered controllers.

31
00:01:25,665 --> 00:01:29,520
Now, I just wanna outsource them into separate files

32
00:01:29,520 --> 00:01:32,780
and then also make them a bit leaner in the next steps,

33
00:01:32,780 --> 00:01:35,690
so that the routes file is way slimmer

34
00:01:35,690 --> 00:01:38,330
and it's way easier to quickly see

35
00:01:38,330 --> 00:01:40,470
all the routes that we are supporting,

36
00:01:40,470 --> 00:01:43,960
because that is something you commonly wanna do.

37
00:01:43,960 --> 00:01:45,960
You don't wanna have large route files

38
00:01:45,960 --> 00:01:48,000
with a lot of logic in there.

39
00:01:48,000 --> 00:01:50,380
Instead, if you look into a route file,

40
00:01:50,380 --> 00:01:52,221
you wanna be able to quickly grasp

41
00:01:52,221 --> 00:01:56,110
which routes are supported by this website.

42
00:01:56,110 --> 00:01:59,420
And that's why we'll use a dedicated controller file

43
00:01:59,420 --> 00:02:02,103
for these route handler functions.

44
00:02:03,380 --> 00:02:04,990
And therefore, I'll simply go through

45
00:02:04,990 --> 00:02:07,800
that blog.js routes file from top to bottom

46
00:02:07,800 --> 00:02:10,479
and outsource all these functions here,

47
00:02:10,479 --> 00:02:12,520
these route handler functions

48
00:02:12,520 --> 00:02:15,630
into the post-controllers.js file.

49
00:02:15,630 --> 00:02:18,070
So therefore, I'll cut this first function,

50
00:02:18,070 --> 00:02:21,340
this home function and add it here.

51
00:02:21,340 --> 00:02:23,230
Now, we just need to give it a name,

52
00:02:23,230 --> 00:02:24,796
because it's no longer valid

53
00:02:24,796 --> 00:02:27,240
to have an anonymous function here.

54
00:02:27,240 --> 00:02:30,970
Instead, we want to be able to reference it by name.

55
00:02:30,970 --> 00:02:32,660
And it's now up to you

56
00:02:32,660 --> 00:02:35,536
how you name your controller functions,

57
00:02:35,536 --> 00:02:39,363
but you wanna name them such that it's clear what they do.

58
00:02:40,270 --> 00:02:42,330
And here I'll name it getHome

59
00:02:42,330 --> 00:02:44,440
because this function will be responsible

60
00:02:44,440 --> 00:02:45,913
for getting the home page.

61
00:02:47,500 --> 00:02:50,060
Now, to make it available outside of this file,

62
00:02:50,060 --> 00:02:52,220
I'll again export an object

63
00:02:52,220 --> 00:02:54,310
and there I have to getHome key,

64
00:02:54,310 --> 00:02:56,673
which points at this getHome function.

65
00:02:57,940 --> 00:03:01,276
So that in blog.js, where I wanna use this function,

66
00:03:01,276 --> 00:03:06,276
I can get my blogControllers object,

67
00:03:06,430 --> 00:03:09,150
which I'm exporting, by requiring

68
00:03:09,150 --> 00:03:14,023
controllers/post-controllers.

69
00:03:14,970 --> 00:03:17,270
And then here, for this first get route,

70
00:03:17,270 --> 00:03:19,080
we use blogControllers.getHome.

71
00:03:20,720 --> 00:03:23,860
And very important, we don't execute it,

72
00:03:23,860 --> 00:03:26,350
instead, we again, just at point it

73
00:03:26,350 --> 00:03:30,410
because we just wanna register this function with Express,

74
00:03:30,410 --> 00:03:33,450
and Express.js should execute it for us

75
00:03:33,450 --> 00:03:36,520
whenever we got an incoming request.

76
00:03:36,520 --> 00:03:39,340
So it's a little bit like an event listener

77
00:03:39,340 --> 00:03:42,070
in the frontend JavaScript world,

78
00:03:42,070 --> 00:03:44,528
where we also just pointed at functions

79
00:03:44,528 --> 00:03:47,230
when we added a click listener.

80
00:03:47,230 --> 00:03:49,770
Now, we're doing the same here so that this function

81
00:03:49,770 --> 00:03:52,983
is executed once a request is arriving.

82
00:03:54,430 --> 00:03:55,424
We did the same before

83
00:03:55,424 --> 00:03:57,860
with the anonymous functions by the way.

84
00:03:57,860 --> 00:04:00,280
We also just the register the function there

85
00:04:00,280 --> 00:04:03,340
and we didn't execute it ourselves.

86
00:04:03,340 --> 00:04:06,010
So I'll continue with the admin function,

87
00:04:06,010 --> 00:04:08,360
and I'll cut that function here,

88
00:04:08,360 --> 00:04:13,040
move to post-controllers.js, and add it there,

89
00:04:13,040 --> 00:04:14,670
and again, give it a function.

90
00:04:14,670 --> 00:04:16,600
And here, since this is responsible

91
00:04:16,600 --> 00:04:20,214
for loading the admin page, I'll name it getAdmin,

92
00:04:20,214 --> 00:04:23,578
just as I name the other function getHome.

93
00:04:23,578 --> 00:04:25,355
This is still an async function

94
00:04:25,355 --> 00:04:29,346
because we still use the await keyword in there.

95
00:04:29,346 --> 00:04:33,333
Now, we can also make that available outside this file

96
00:04:33,333 --> 00:04:35,720
by using getAdmin as a key here

97
00:04:35,720 --> 00:04:38,970
and pointing at this getAdmin function.

98
00:04:39,837 --> 00:04:43,079
Very important though, in this getAdmin function,

99
00:04:43,079 --> 00:04:46,079
I am relying on this Post model now.

100
00:04:47,099 --> 00:04:50,208
So in order to do that without errors,

101
00:04:50,208 --> 00:04:54,589
we need to import that into this post-controllers.js file.

102
00:04:54,589 --> 00:04:59,589
We should get post here by requiring models/post like this.

103
00:05:01,332 --> 00:05:03,828
Otherwise, working with that Post model

104
00:05:03,828 --> 00:05:07,161
won't succeed and we would get an error.

105
00:05:08,640 --> 00:05:11,300
Now, back in blog.js, I can, therefore, now point

106
00:05:11,300 --> 00:05:14,963
at blogControllers.getAdmin here.

107
00:05:17,383 --> 00:05:20,270
And again, we continue with the post route

108
00:05:20,270 --> 00:05:22,463
for adding new posts now.

109
00:05:22,463 --> 00:05:27,224
I'll cut to that, and then go to post-controllers.

110
00:05:27,224 --> 00:05:32,090
And in there, I now do add this function as well

111
00:05:33,180 --> 00:05:34,500
and give it a name.

112
00:05:34,500 --> 00:05:36,437
And here I'll name it createPost

113
00:05:38,220 --> 00:05:40,423
describing what will happen in there.

114
00:05:41,430 --> 00:05:45,620
And I will, of course also expose this

115
00:05:45,620 --> 00:05:49,533
under a createPost key just as I did it before.

116
00:05:50,613 --> 00:05:53,243
And hence, here in blog.js,

117
00:05:54,080 --> 00:05:56,820
I now point at blogController.createPost.

118
00:05:59,105 --> 00:06:02,803
Of course, we do the same for getting the detail page.

119
00:06:04,480 --> 00:06:08,360
So back in post-controllers, we added that function.

120
00:06:08,360 --> 00:06:11,230
Give it a meaningful name, like getSinglePost,

121
00:06:12,860 --> 00:06:17,180
and then expose that under that same key here

122
00:06:17,180 --> 00:06:18,703
to the outside world.

123
00:06:21,000 --> 00:06:23,130
And back in blog.js,

124
00:06:23,130 --> 00:06:25,690
we now point at blogControllers.getSinglePost.

125
00:06:27,564 --> 00:06:30,300
Of course, we do the same for the post route

126
00:06:30,300 --> 00:06:34,270
that belongs to that, which we use for updating a post.

127
00:06:34,270 --> 00:06:37,740
So we put that into blogControllers as well.

128
00:06:37,740 --> 00:06:39,953
Give it a function that could be updatePost,

129
00:06:42,420 --> 00:06:45,680
and then we also expose that of course,

130
00:06:45,680 --> 00:06:48,580
updatePost pointing at updatePost.

131
00:06:51,770 --> 00:06:54,153
And then here again, I use blogControllers.updatePost.

132
00:06:55,400 --> 00:06:58,683
And last but not least, I do the same for deleting.

133
00:06:59,550 --> 00:07:04,550
So now in post-controller, I now add this function,

134
00:07:04,590 --> 00:07:09,590
name it deletePost and expose that to the outside world

135
00:07:10,890 --> 00:07:11,923
like this.

136
00:07:13,600 --> 00:07:16,370
And back in the blog.js routes file,

137
00:07:16,370 --> 00:07:18,693
I now point at blogControllers.deletePost.

138
00:07:22,040 --> 00:07:24,640
And now this is a way leaner routes file.

139
00:07:24,640 --> 00:07:27,640
Of course, we just moved the logic into a different file,

140
00:07:27,640 --> 00:07:29,610
but that was the goal.

141
00:07:29,610 --> 00:07:31,700
Now, it's super easy to quickly see

142
00:07:31,700 --> 00:07:35,260
which post related routes this website supports.

143
00:07:35,260 --> 00:07:37,670
And that's typically the goal you wanna achieve

144
00:07:37,670 --> 00:07:39,100
with your routes file.

145
00:07:39,100 --> 00:07:41,600
So that if another developer takes a look at it,

146
00:07:41,600 --> 00:07:45,312
he or she is able to quickly see what's going on.

147
00:07:45,312 --> 00:07:48,280
We can now remove the Post model import here

148
00:07:48,280 --> 00:07:49,400
in the routes file

149
00:07:49,400 --> 00:07:52,043
because we're not using the model here anymore.

