1
00:00:02,110 --> 00:00:03,480
Now in the last lecture,

2
00:00:03,480 --> 00:00:07,780
we finished implementing the model view controller pattern

3
00:00:07,780 --> 00:00:10,623
at the example of our post related logic.

4
00:00:11,620 --> 00:00:14,890
Now we can leave the MVC pattern again

5
00:00:14,890 --> 00:00:19,430
and do some general improvements and refactorings again.

6
00:00:19,430 --> 00:00:22,600
Because if we have a look at the post controllers JS file,

7
00:00:22,600 --> 00:00:25,230
this is now still a pretty big file.

8
00:00:25,230 --> 00:00:29,490
And there still is some redundant logic in there,

9
00:00:29,490 --> 00:00:33,970
which we might want to get rid of to simplify that file

10
00:00:33,970 --> 00:00:37,600
and avoid unnecessary code duplication.

11
00:00:37,600 --> 00:00:41,190
And the first big thing that comes to my mind here

12
00:00:41,190 --> 00:00:45,620
is this session usage for error management.

13
00:00:45,620 --> 00:00:48,920
If we have some invalid input and we use the session

14
00:00:48,920 --> 00:00:53,029
for storing information about that error

15
00:00:53,029 --> 00:00:55,050
after we redirected.

16
00:00:55,050 --> 00:00:58,240
That is something we do in a couple of places,

17
00:00:58,240 --> 00:01:00,400
it takes up a couple of lines of code

18
00:01:00,400 --> 00:01:04,833
and it's some unnecessary repetition here, I would argue.

19
00:01:05,950 --> 00:01:06,980
The same, by the way,

20
00:01:06,980 --> 00:01:11,240
goes for the general validation of input in my post routes,

21
00:01:11,240 --> 00:01:13,960
where I also performed the same checks

22
00:01:13,960 --> 00:01:16,540
and then the same updates of my session data

23
00:01:16,540 --> 00:01:18,790
in two different routes.

24
00:01:18,790 --> 00:01:21,490
And that's there for some code duplication,

25
00:01:21,490 --> 00:01:24,110
which we can probably get rid of.

26
00:01:24,110 --> 00:01:28,170
Now, I will get started with extracting that error data

27
00:01:28,170 --> 00:01:30,780
from the session and setting a default data

28
00:01:30,780 --> 00:01:33,653
if we hadn't any errors in the session.

29
00:01:34,920 --> 00:01:35,753
For that,

30
00:01:35,753 --> 00:01:38,390
I'll add yet another folder in the project.

31
00:01:38,390 --> 00:01:41,510
And it is quite normal that as your project grows,

32
00:01:41,510 --> 00:01:45,220
you work with a couple of folders to organize your code.

33
00:01:45,220 --> 00:01:49,310
And we could give this folder a variety of names.

34
00:01:49,310 --> 00:01:52,620
We could name it util or utilities since it stores

35
00:01:52,620 --> 00:01:55,010
some utility functionalities,

36
00:01:55,010 --> 00:01:59,410
or validations, since it will be involved with validation.

37
00:01:59,410 --> 00:02:02,110
And managing that error data into session is,

38
00:02:02,110 --> 00:02:05,110
of course, is also part of the validation process,

39
00:02:05,110 --> 00:02:09,002
because we're doing that because some validation failed.

40
00:02:10,400 --> 00:02:15,400
Still, I will go for the more generic util for utility name.

41
00:02:15,520 --> 00:02:17,280
And in there, I'll add a new file,

42
00:02:17,280 --> 00:02:20,870
which I named validation dash session JS,

43
00:02:20,870 --> 00:02:24,720
because I want to store all the validation related session

44
00:02:24,720 --> 00:02:26,453
related logic in there.

45
00:02:27,590 --> 00:02:29,470
Now what could that be?

46
00:02:29,470 --> 00:02:32,780
In the end, if we have a look in our get routes,

47
00:02:32,780 --> 00:02:36,850
when we load a page and we check wherever we have errors

48
00:02:36,850 --> 00:02:37,683
in the session,

49
00:02:37,683 --> 00:02:41,390
which we want to use for pre-populating a form,

50
00:02:41,390 --> 00:02:44,630
we always perform the same couple of steps.

51
00:02:44,630 --> 00:02:49,610
We extract that potential error data from the session.

52
00:02:49,610 --> 00:02:53,850
Then we check if we were successful extracting that.

53
00:02:53,850 --> 00:02:57,660
And if we were not, we set some default data.

54
00:02:57,660 --> 00:02:59,920
Then we clear the data in the session,

55
00:02:59,920 --> 00:03:03,250
so that for subsequent requests and page reloads,

56
00:03:03,250 --> 00:03:06,480
we don't see the same errors over and over again.

57
00:03:06,480 --> 00:03:09,763
And then ultimately we pass that data to the template.

58
00:03:11,040 --> 00:03:16,040
So these three steps actually can be cut here

59
00:03:16,440 --> 00:03:19,570
and they can be outsourced into this validation

60
00:03:19,570 --> 00:03:21,280
session JS file.

61
00:03:21,280 --> 00:03:23,760
And there I'll add a new function,

62
00:03:23,760 --> 00:03:28,670
which I will name get session error data,

63
00:03:28,670 --> 00:03:31,250
since that is what I need to do here.

64
00:03:31,250 --> 00:03:34,863
I go to my session and check for error data in it.

65
00:03:36,550 --> 00:03:40,360
Now I'm relying on the existence of this request object

66
00:03:40,360 --> 00:03:41,193
in there.

67
00:03:41,193 --> 00:03:42,026
So therefore,

68
00:03:42,026 --> 00:03:45,270
I assume that I do get it as a parameter value

69
00:03:45,270 --> 00:03:48,380
and we'll have to make sure we pass in that request

70
00:03:48,380 --> 00:03:50,393
when we call this function later.

71
00:03:51,670 --> 00:03:54,830
Then, I perform the same steps as before,

72
00:03:54,830 --> 00:03:56,450
reading data from the session

73
00:03:56,450 --> 00:03:58,600
and clearing the data ultimately.

74
00:03:58,600 --> 00:04:02,883
And then I returned my parsed session input data here.

75
00:04:04,340 --> 00:04:05,800
Now I will, of course,

76
00:04:05,800 --> 00:04:10,410
export this to make this available outside of this file.

77
00:04:10,410 --> 00:04:14,210
And since I will soon add more to this file,

78
00:04:14,210 --> 00:04:17,339
I will have more than one function in there soon.

79
00:04:17,339 --> 00:04:21,493
I will actually export this in an object

80
00:04:21,493 --> 00:04:25,890
so that we can group multiple exported functions together.

81
00:04:25,890 --> 00:04:28,920
And here I'll use get session error data as a key.

82
00:04:28,920 --> 00:04:31,853
And then the value is a pointer at this function.

83
00:04:33,330 --> 00:04:36,410
And now in post controller, we can use that.

84
00:04:36,410 --> 00:04:39,660
Here at the top where I also import my model,

85
00:04:39,660 --> 00:04:44,660
I can now also import my session validation object

86
00:04:44,850 --> 00:04:49,850
here by requiring util validation session.

87
00:04:52,660 --> 00:04:56,360
So actually let's maybe name it validation session

88
00:04:56,360 --> 00:04:57,800
here as well.

89
00:04:57,800 --> 00:05:02,050
And then in this get admin controller,

90
00:05:02,050 --> 00:05:04,830
I will call validation session

91
00:05:04,830 --> 00:05:08,010
and call get session error data.

92
00:05:08,010 --> 00:05:11,590
And what I'll get back is always such a session

93
00:05:11,590 --> 00:05:15,630
input data object in the end,

94
00:05:15,630 --> 00:05:18,010
or a session error data,

95
00:05:18,010 --> 00:05:19,800
if you want to call it like this,

96
00:05:19,800 --> 00:05:23,493
which I pass to my template then, using that same key name.

97
00:05:26,780 --> 00:05:29,800
And with that, we're executing the same logic as before,

98
00:05:29,800 --> 00:05:34,170
but we moved it out of the controller into a separate file

99
00:05:34,170 --> 00:05:37,100
to make that controller a bit easier to understand

100
00:05:37,100 --> 00:05:39,090
and a bit slimmer.

101
00:05:39,090 --> 00:05:43,000
And I'll do the same in this get single post route.

102
00:05:43,000 --> 00:05:46,940
Here, I also get my session error data

103
00:05:46,940 --> 00:05:50,460
and hence, I'll replace that with that same line of code.

104
00:05:50,460 --> 00:05:55,290
I copied that from the route where we worked in a second ago

105
00:05:55,290 --> 00:05:58,580
and I'll then use this session error data for this template

106
00:05:58,580 --> 00:05:59,413
as well.

107
00:06:00,520 --> 00:06:03,010
Now, like that it wouldn't work though.

108
00:06:03,010 --> 00:06:04,200
To make it work,

109
00:06:04,200 --> 00:06:07,530
we have to make sure that we pass this request object

110
00:06:07,530 --> 00:06:10,373
into this function because we rely on that.

111
00:06:11,230 --> 00:06:15,380
So in both the places where I call get session error data,

112
00:06:15,380 --> 00:06:17,863
I pass my request into that function.

113
00:06:18,880 --> 00:06:20,890
Here in get admin,

114
00:06:20,890 --> 00:06:24,503
and then also further down in get single post.

115
00:06:26,410 --> 00:06:29,680
With that the result should be that we can still visit

116
00:06:29,680 --> 00:06:31,110
the admin page.

117
00:06:31,110 --> 00:06:34,690
And if I use the developer tools to get rid of the required

118
00:06:34,690 --> 00:06:39,530
attribute so that I can submit this without a title,

119
00:06:39,530 --> 00:06:41,620
though, that normally should not work,

120
00:06:41,620 --> 00:06:44,290
but here it works because I removed required.

121
00:06:44,290 --> 00:06:47,240
Then my server side validation kicks in

122
00:06:47,240 --> 00:06:50,000
and I get this error message as expected.

123
00:06:50,000 --> 00:06:53,620
The input I did enter was saved, but if I reload again,

124
00:06:53,620 --> 00:06:56,730
everything is gone because we cleared that error data

125
00:06:56,730 --> 00:06:57,793
from the session.

126
00:06:59,040 --> 00:07:00,630
So that's the first step.

127
00:07:00,630 --> 00:07:03,290
We're still getting that data from the session,

128
00:07:03,290 --> 00:07:05,413
but we're doing it in a separate file.

129
00:07:06,700 --> 00:07:07,533
Now, of course,

130
00:07:07,533 --> 00:07:10,820
the other part that's also related with that

131
00:07:10,820 --> 00:07:15,053
is that we write data to the session if validation fails.

132
00:07:16,030 --> 00:07:18,500
For this, I got my validation logic here,

133
00:07:18,500 --> 00:07:22,293
but then ultimately I'm also writing data to a session.

134
00:07:23,140 --> 00:07:27,420
And I'll actually cut this logic here and move that

135
00:07:27,420 --> 00:07:31,533
into validation session as well, in this separate file.

136
00:07:32,700 --> 00:07:37,700
There, I'll add a new function which I'll name flash errors

137
00:07:37,720 --> 00:07:39,330
to session.

138
00:07:39,330 --> 00:07:43,323
Flashing simply means that it's on a session temporarily.

139
00:07:44,890 --> 00:07:47,880
And here I put my data onto a session.

140
00:07:47,880 --> 00:07:50,720
For this, I still need the request object though.

141
00:07:50,720 --> 00:07:53,960
And I also need the data that should be flashed

142
00:07:53,960 --> 00:07:55,123
onto the session.

143
00:07:56,310 --> 00:07:59,750
Because I will always set has error to true,

144
00:07:59,750 --> 00:08:01,780
but the message title and content,

145
00:08:01,780 --> 00:08:04,890
that's not always necessarily the same,

146
00:08:04,890 --> 00:08:07,480
especially if we have a look at the auth routes,

147
00:08:07,480 --> 00:08:09,870
which we haven't really factored at all yet.

148
00:08:09,870 --> 00:08:10,703
There,

149
00:08:10,703 --> 00:08:13,770
we used different error messages for different routes.

150
00:08:13,770 --> 00:08:17,223
So that should be settable from outside this function.

151
00:08:18,340 --> 00:08:21,810
Hence, I'll replace this content here with

152
00:08:21,810 --> 00:08:23,393
dot dot dot data.

153
00:08:24,830 --> 00:08:27,960
You might remember this dot dot dot operator.

154
00:08:27,960 --> 00:08:31,780
I did briefly introduce that early in the course as well.

155
00:08:31,780 --> 00:08:34,100
That's the so-called spread operator

156
00:08:34,100 --> 00:08:37,130
and here I expect data to be an object.

157
00:08:37,130 --> 00:08:40,390
And when you use the spread operator on an object,

158
00:08:40,390 --> 00:08:44,860
it simply takes all the key value pairs in that object,

159
00:08:44,860 --> 00:08:47,720
all the properties of that object and adds them

160
00:08:47,720 --> 00:08:49,430
as key value pairs.

161
00:08:49,430 --> 00:08:52,510
So as new properties to that object

162
00:08:52,510 --> 00:08:54,960
in which you use the operator.

163
00:08:54,960 --> 00:08:57,530
So that will take all the key value pairs of data

164
00:08:57,530 --> 00:09:00,683
and add them to this object, which is added to the session.

165
00:09:02,520 --> 00:09:04,380
And then as a last step,

166
00:09:04,380 --> 00:09:08,473
I actually also expect to get a third parameter value,

167
00:09:09,620 --> 00:09:11,280
action, let's say,

168
00:09:11,280 --> 00:09:14,140
and that should be a function that should be performed

169
00:09:14,140 --> 00:09:17,250
after the data was stored in a session.

170
00:09:17,250 --> 00:09:21,490
So here I will manually call request sessions save.

171
00:09:21,490 --> 00:09:26,153
And then here, I pass action to this save method

172
00:09:27,260 --> 00:09:29,910
because the save method takes a function

173
00:09:29,910 --> 00:09:33,200
which it will execute after saving finished.

174
00:09:33,200 --> 00:09:36,810
And I want to be able to receive that to the executed

175
00:09:36,810 --> 00:09:39,203
function from the outside world,

176
00:09:40,210 --> 00:09:43,490
so that we only performed that action, for example,

177
00:09:43,490 --> 00:09:46,593
our redirect, after we saved the session.

178
00:09:48,582 --> 00:09:49,560
And then last but not least,

179
00:09:49,560 --> 00:09:53,840
I'll expose this flash errors to session under the same key

180
00:09:53,840 --> 00:09:55,133
to the outside world.

181
00:09:56,810 --> 00:09:58,930
Back in post controller JS,

182
00:09:58,930 --> 00:10:03,230
that means that here in that failed validation,

183
00:10:03,230 --> 00:10:06,710
I can use my validation session object

184
00:10:06,710 --> 00:10:11,710
and call flash errors to session, pass in the request,

185
00:10:12,200 --> 00:10:14,430
pass in the extra data.

186
00:10:14,430 --> 00:10:17,820
In this case, a new object where I still have my specific

187
00:10:17,820 --> 00:10:22,820
message, title and content, and then pass in the action,

188
00:10:23,210 --> 00:10:25,770
which should be this redirection.

189
00:10:25,770 --> 00:10:29,010
And I actually had the wrong code here before.

190
00:10:29,010 --> 00:10:32,550
I did not wrap this into the save function before.

191
00:10:32,550 --> 00:10:35,770
Now I will add a new anonymous function here in which

192
00:10:35,770 --> 00:10:36,653
I redirect.

193
00:10:38,140 --> 00:10:40,140
And it's this anonymous function,

194
00:10:40,140 --> 00:10:42,650
which has passed as an action,

195
00:10:42,650 --> 00:10:46,350
to this flash errors to session method.

196
00:10:46,350 --> 00:10:48,640
So that it's this anonymous function,

197
00:10:48,640 --> 00:10:51,563
which will be executed here after we saved.

198
00:10:53,020 --> 00:10:53,930
So overall,

199
00:10:53,930 --> 00:10:56,780
we still have quite a bit of logic here

200
00:10:56,780 --> 00:10:59,700
in the post controllers JS file,

201
00:10:59,700 --> 00:11:03,550
but we also were able to outsource some common tasks,

202
00:11:03,550 --> 00:11:04,383
at least.

203
00:11:06,390 --> 00:11:08,960
It is important that we still return there after,

204
00:11:08,960 --> 00:11:11,620
so that no other code in there executes

205
00:11:11,620 --> 00:11:14,160
if we made it into this block though.

206
00:11:14,160 --> 00:11:17,610
So that should absolutely stay in that controller file

207
00:11:17,610 --> 00:11:21,150
because if we would return inside of flash errors

208
00:11:21,150 --> 00:11:24,800
to session, for example, that wouldn't help us at all,

209
00:11:24,800 --> 00:11:27,320
because then we would just end the execution

210
00:11:27,320 --> 00:11:30,820
of that function, but not of this controller function.

211
00:11:30,820 --> 00:11:34,673
So return needs to stay here in the controller instead.

212
00:11:35,770 --> 00:11:37,130
And now last but not least,

213
00:11:37,130 --> 00:11:40,450
I also got some repeated validation logic.

214
00:11:40,450 --> 00:11:44,020
I got the same checks here for updating a post,

215
00:11:44,020 --> 00:11:48,610
as I do have them here for creating a post and therefore,

216
00:11:48,610 --> 00:11:53,100
in my util folder, I'll also add a validation JS file,

217
00:11:53,100 --> 00:11:55,800
which holds the core validation logic.

218
00:11:55,800 --> 00:12:00,010
And here I'll add a new function.

219
00:12:00,010 --> 00:12:04,420
Name it post is valid because this function

220
00:12:04,420 --> 00:12:07,770
should answer the question whether a post is valid

221
00:12:08,730 --> 00:12:12,340
and I expect to get the title and the content of a post

222
00:12:12,340 --> 00:12:14,450
as a parameter here.

223
00:12:14,450 --> 00:12:18,773
And then I will actually grab this check here,

224
00:12:20,320 --> 00:12:25,320
copy that and returned that like this,

225
00:12:25,570 --> 00:12:30,570
this will overall evaluate to a Boolean value, remember.

226
00:12:30,820 --> 00:12:31,653
However,

227
00:12:31,653 --> 00:12:33,810
I need to invert it because the function is named

228
00:12:33,810 --> 00:12:38,020
post is valid, so it should return true if the post is valid

229
00:12:38,020 --> 00:12:42,250
and the post would be valid if entered title is truthy.

230
00:12:42,250 --> 00:12:46,910
So no exclamation mark and entered content is truthy.

231
00:12:46,910 --> 00:12:51,500
So no exclamation mark and the trimmed entered title

232
00:12:51,500 --> 00:12:54,210
is not empty.

233
00:12:54,210 --> 00:12:58,653
And the trimmed entered content is also not an empty string.

234
00:13:00,330 --> 00:13:04,180
If all these conditions are met, then a post is valid

235
00:13:04,180 --> 00:13:06,810
and therefore, that is a function which now contains

236
00:13:06,810 --> 00:13:08,440
all the validation logic.

237
00:13:08,440 --> 00:13:10,600
And we can then use it in a another file

238
00:13:10,600 --> 00:13:12,800
so that we don't have to repeat that logic

239
00:13:12,800 --> 00:13:14,253
over and over again.

240
00:13:15,100 --> 00:13:16,230
And then here,

241
00:13:16,230 --> 00:13:18,210
I will also export an object,

242
00:13:18,210 --> 00:13:21,910
since I will add more validation logic to that file soon.

243
00:13:21,910 --> 00:13:26,423
And we can return or export post is valid like this.

244
00:13:27,320 --> 00:13:30,950
Now to make this work here, this post is valid function.

245
00:13:30,950 --> 00:13:34,870
We now of course have to use title instead of entered title

246
00:13:34,870 --> 00:13:37,760
and content instead of entered content,

247
00:13:37,760 --> 00:13:40,900
because these are the parameter names I'm getting here.

248
00:13:40,900 --> 00:13:41,770
Alternatively,

249
00:13:41,770 --> 00:13:43,720
you would have renamed these parameters

250
00:13:45,070 --> 00:13:48,570
and then back in post controller where we want to find out

251
00:13:48,570 --> 00:13:49,890
if a post is valid,

252
00:13:49,890 --> 00:13:51,950
instead of having our condition here,

253
00:13:51,950 --> 00:13:55,250
which of course also works, but to save some space,

254
00:13:55,250 --> 00:13:57,690
we can now execute this function,

255
00:13:57,690 --> 00:13:59,803
which I defined in a separate file.

256
00:14:00,750 --> 00:14:03,743
For this, I'll just include it here in this file.

257
00:14:04,610 --> 00:14:06,730
I'll name the constant validation,

258
00:14:06,730 --> 00:14:11,463
and then require util validation like this.

259
00:14:13,110 --> 00:14:16,910
And then in the place where I want to validate, like here,

260
00:14:16,910 --> 00:14:21,910
I will just execute validation dot post is valid,

261
00:14:22,290 --> 00:14:26,000
execute this as a function and pass the entered title

262
00:14:26,000 --> 00:14:30,040
and the entered content as parameter values.

263
00:14:30,040 --> 00:14:32,880
And here, I want to know if that's not valid.

264
00:14:32,880 --> 00:14:35,780
So here I will add an exclamation mark.

265
00:14:35,780 --> 00:14:37,870
And now we execute this function.

266
00:14:37,870 --> 00:14:40,490
This function then will return a Boolean

267
00:14:40,490 --> 00:14:43,250
and we use that Boolean in this if check,

268
00:14:43,250 --> 00:14:45,640
so that we were able to outsource the overall

269
00:14:45,640 --> 00:14:48,023
condition into a separate file.

270
00:14:49,750 --> 00:14:51,030
Now here in create post,

271
00:14:51,030 --> 00:14:54,450
we can now also use our newly added method

272
00:14:54,450 --> 00:14:57,800
for flashing the error data onto a session

273
00:14:57,800 --> 00:15:02,800
and use the validation session object to call flash errors

274
00:15:03,340 --> 00:15:07,230
to session, pass in the request,

275
00:15:07,230 --> 00:15:09,300
our object with extra data,

276
00:15:09,300 --> 00:15:12,493
which contains the message title and entered content,

277
00:15:13,440 --> 00:15:15,543
and then get rid of this part here.

278
00:15:16,820 --> 00:15:19,010
And then as a third parameter value,

279
00:15:19,010 --> 00:15:21,160
pass in an anonymous function,

280
00:15:21,160 --> 00:15:23,720
which is the to be executed action,

281
00:15:23,720 --> 00:15:25,923
where I actually will redirect.

282
00:15:28,560 --> 00:15:31,840
And then if we go down to updating a post again,

283
00:15:31,840 --> 00:15:36,840
here, I can also, again, use validation dot post is valid.

284
00:15:37,550 --> 00:15:40,540
Execute this, pass in the entered title

285
00:15:40,540 --> 00:15:43,760
and entered content, check for the opposite,

286
00:15:43,760 --> 00:15:47,560
since we want to make it in there if the post is not valid

287
00:15:47,560 --> 00:15:51,480
and we already refactored the flash it to the session

288
00:15:51,480 --> 00:15:52,943
logic before.

289
00:15:54,330 --> 00:15:57,720
Now, we did refactor the entire validation logic

290
00:15:57,720 --> 00:16:01,410
and added these extra methods for getting data

291
00:16:01,410 --> 00:16:06,400
out of a sessions, but one tiny bug I introduced with that

292
00:16:06,400 --> 00:16:10,950
is that if we do now try to view and edit an existing post,

293
00:16:10,950 --> 00:16:12,660
we load an empty form.

294
00:16:12,660 --> 00:16:16,213
It's not pre-populated with the existing post data.

295
00:16:17,310 --> 00:16:20,740
The reason for that is that in get session error data,

296
00:16:20,740 --> 00:16:24,820
I do always set title and content to an empty string

297
00:16:24,820 --> 00:16:28,540
if we don't have any error data in the session.

298
00:16:28,540 --> 00:16:31,040
And whilst this is correct for a new post,

299
00:16:31,040 --> 00:16:35,500
this is not correct for updating an existing post.

300
00:16:35,500 --> 00:16:38,460
Instead, I want to initialize this with existing titles

301
00:16:38,460 --> 00:16:39,403
and contents.

302
00:16:40,390 --> 00:16:44,700
Hence I will tune this get session error data function

303
00:16:44,700 --> 00:16:49,660
a little bit and accept the default values here.

304
00:16:49,660 --> 00:16:52,260
That's a parameter name we could use,

305
00:16:52,260 --> 00:16:55,070
which should be an object with any default values

306
00:16:55,070 --> 00:16:56,003
we want to set.

307
00:16:57,610 --> 00:16:58,630
And then we'll again,

308
00:16:58,630 --> 00:17:02,060
use the spread operator to spread that object

309
00:17:02,060 --> 00:17:06,400
of default values into this new object I'm using here.

310
00:17:06,400 --> 00:17:10,040
So we'll use all the key value pairs in default values

311
00:17:10,040 --> 00:17:11,463
in this object.

312
00:17:13,020 --> 00:17:17,119
And then in the post controller in the places where

313
00:17:17,119 --> 00:17:19,640
I use get session error data,

314
00:17:19,640 --> 00:17:23,020
I pass in that second parameter value

315
00:17:23,020 --> 00:17:28,020
where I do set my starting title equal to post dot title

316
00:17:29,670 --> 00:17:33,223
and my content to post dot content.

317
00:17:34,400 --> 00:17:37,340
And on the other hand, when we create a new post,

318
00:17:37,340 --> 00:17:41,690
then that starting content or the default values,

319
00:17:41,690 --> 00:17:44,500
of course will be these empty strings.

320
00:17:44,500 --> 00:17:47,430
So then I set title equal to an empty string

321
00:17:47,430 --> 00:17:50,280
and content equal to an empty string.

322
00:17:50,280 --> 00:17:52,800
But now we have different default values

323
00:17:52,800 --> 00:17:56,350
for the different pages for the different routes we have,

324
00:17:56,350 --> 00:18:00,563
whilst still outsourcing the general shared logic.

325
00:18:03,370 --> 00:18:05,420
And with that, if I view and edit this,

326
00:18:05,420 --> 00:18:08,040
it is now pre-populated correctly.

327
00:18:08,040 --> 00:18:11,270
The same goes for creating a new post where it is empty

328
00:18:11,270 --> 00:18:12,343
as expected.

329
00:18:13,530 --> 00:18:17,180
And that now were a bunch of refactorings.

330
00:18:17,180 --> 00:18:19,700
And with all those refactorings,

331
00:18:19,700 --> 00:18:23,840
this post controller file is way leaner.

332
00:18:23,840 --> 00:18:27,120
Now, the last thing I want to do in here is related

333
00:18:27,120 --> 00:18:28,713
to the CSRF token.

