﻿1
00:00:01,010 --> 00:00:03,970
‫All right, and now just to finish this part,

2
00:00:03,970 --> 00:00:07,183
‫let's create some factories for getting documents.

3
00:00:08,740 --> 00:00:11,963
‫And let's start with getOne here, okay?

4
00:00:12,910 --> 00:00:17,910
‫Exports.getOne, but this one is actually a bit trickier,

5
00:00:18,580 --> 00:00:20,690
‫and that's because we have a populate

6
00:00:20,690 --> 00:00:22,710
‫in the getTour handler.

7
00:00:22,710 --> 00:00:24,803
‫So, let's take a look at that.

8
00:00:25,900 --> 00:00:30,090
‫So, right here, in this one, we have this populate here,

9
00:00:30,090 --> 00:00:32,570
‫which is different from all the other get handlers

10
00:00:32,570 --> 00:00:35,220
‫in the other resources, right?

11
00:00:35,220 --> 00:00:37,620
‫But, well, this is not really a problem

12
00:00:37,620 --> 00:00:40,670
‫because we will simply allow ourselves to pass in

13
00:00:40,670 --> 00:00:45,670
‫a populate options object into our getOne function, okay?

14
00:00:48,030 --> 00:00:50,840
‫So, instead of simply passing in the model,

15
00:00:50,840 --> 00:00:55,700
‫we will also have populate, so options, all right.

16
00:00:57,890 --> 00:01:00,370
‫And so, from here we will then return

17
00:01:00,370 --> 00:01:02,053
‫the normal handler function.

18
00:01:03,360 --> 00:01:06,563
‫So, let's go ahead and again copy all of this,

19
00:01:10,850 --> 00:01:11,683
‫all right.

20
00:01:13,730 --> 00:01:15,330
‫So, we don't need this one here.

21
00:01:17,690 --> 00:01:19,743
‫Then here, as always, it is model,

22
00:01:23,140 --> 00:01:25,800
‫and here it is doc.

23
00:01:25,800 --> 00:01:27,993
‫Well, actually here, document,

24
00:01:30,520 --> 00:01:33,850
‫and here, once again, data equal to doc.

25
00:01:33,850 --> 00:01:36,210
‫Now, we also need to make some changes here now

26
00:01:36,210 --> 00:01:38,390
‫because of this populate,

27
00:01:38,390 --> 00:01:41,610
‫so basically, we will first create the query,

28
00:01:41,610 --> 00:01:44,950
‫and then, if there is the populate options object,

29
00:01:44,950 --> 00:01:46,550
‫we will then add that to the query,

30
00:01:46,550 --> 00:01:50,810
‫and then by the end, await that query, okay?

31
00:01:50,810 --> 00:01:51,643
‫Make sense?

32
00:01:53,390 --> 00:01:55,890
‫So, let's translate what I just said.

33
00:01:55,890 --> 00:02:00,773
‫So, the query is simply equal to Model.findById,

34
00:02:02,770 --> 00:02:04,993
‫like this, right?

35
00:02:06,630 --> 00:02:10,980
‫Then, if there is a populate options object,

36
00:02:10,980 --> 00:02:12,193
‫well, then in that case,

37
00:02:13,420 --> 00:02:15,020
‫query is gonna be

38
00:02:15,020 --> 00:02:20,020
‫query.populate with the populate options, okay?

39
00:02:24,410 --> 00:02:27,370
‫And then, finally we await our query,

40
00:02:27,370 --> 00:02:28,853
‫and save it in the document.

41
00:02:30,010 --> 00:02:35,010
‫So, await query just like this, all right.

42
00:02:36,210 --> 00:02:38,660
‫And so, that's actually it.

43
00:02:38,660 --> 00:02:40,360
‫So, this logic that we have here

44
00:02:40,360 --> 00:02:43,200
‫with not awaiting the query right away,

45
00:02:43,200 --> 00:02:45,300
‫but instead just saving it into a variable,

46
00:02:45,300 --> 00:02:48,220
‫so that we can then, in the next step, manipulate it.

47
00:02:48,220 --> 00:02:50,770
‫We actually already did this kind of stuff before

48
00:02:50,770 --> 00:02:54,020
‫when we were building our API features, right?

49
00:02:54,020 --> 00:02:57,210
‫So, only in the end, when the entire query is ready,

50
00:02:57,210 --> 00:03:00,563
‫we will then await it and store it somewhere in a variable.

51
00:03:02,260 --> 00:03:06,600
‫Now, let's go ahead and actually use this one everywhere,

52
00:03:06,600 --> 00:03:08,803
‫starting of course with the Tour.

53
00:03:10,470 --> 00:03:12,170
‫So, getTour will now be

54
00:03:13,670 --> 00:03:15,473
‫factory.getOne,

55
00:03:18,260 --> 00:03:20,900
‫and pass in the Tour

56
00:03:20,900 --> 00:03:24,440
‫and populate options object, remember that?

57
00:03:24,440 --> 00:03:29,440
‫And so, what we're gonna do is to say path and reviews.

58
00:03:31,590 --> 00:03:35,540
‫Right, so remember that the path property is basically

59
00:03:35,540 --> 00:03:37,720
‫the field that we want to populate,

60
00:03:37,720 --> 00:03:40,950
‫and then we can also specify select,

61
00:03:40,950 --> 00:03:43,750
‫so remember this one, where we specify

62
00:03:43,750 --> 00:03:47,770
‫which of the fields we actually want to get, right?

63
00:03:47,770 --> 00:03:48,603
‫But in this case,

64
00:03:48,603 --> 00:03:50,050
‫we don't have any of that,

65
00:03:50,050 --> 00:03:51,900
‫and so let's just leave it like this.

66
00:03:53,180 --> 00:03:57,723
‫And now, we can actually go ahead and delete all of this.

67
00:03:58,910 --> 00:04:00,570
‫So, great.

68
00:04:00,570 --> 00:04:03,473
‫And, just to make sure, let's actually quickly test this.

69
00:04:06,200 --> 00:04:07,963
‫So, we'll gt a certain Tour,

70
00:04:10,880 --> 00:04:12,630
‫and, indeed, it still works.

71
00:04:12,630 --> 00:04:15,503
‫Let's see if this one had some reviews.

72
00:04:16,630 --> 00:04:19,210
‫Oh, yeah, actually it does, and you see

73
00:04:19,210 --> 00:04:22,993
‫that the population process is also still working.

74
00:04:24,920 --> 00:04:28,177
‫So, that was successful, I would say.

75
00:04:28,177 --> 00:04:30,870
‫And so, now lets also use this getOne

76
00:04:30,870 --> 00:04:32,710
‫in all the other resources.

77
00:04:32,710 --> 00:04:36,833
‫So, the user controller here where we have,

78
00:04:38,200 --> 00:04:40,603
‫let's see where it is, ah, down here.

79
00:04:43,760 --> 00:04:47,047
‫So, let's say factory.getOne,

80
00:04:48,930 --> 00:04:52,290
‫but only with the user model,

81
00:04:52,290 --> 00:04:54,343
‫and no populate options object.

82
00:04:56,900 --> 00:04:58,740
‫Okay, and since we're here,

83
00:04:58,740 --> 00:05:02,030
‫let's actually just change the error message

84
00:05:02,030 --> 00:05:03,850
‫here for create user,

85
00:05:03,850 --> 00:05:07,583
‫so this route is not defined, it never will be,

86
00:05:08,980 --> 00:05:11,703
‫and please use signup instead.

87
00:05:17,010 --> 00:05:19,110
‫Next up, and finally,

88
00:05:19,110 --> 00:05:23,410
‫the same thing for the reviews basically.

89
00:05:23,410 --> 00:05:25,063
‫So, exports.getReview

90
00:05:29,018 --> 00:05:31,427
‫is equal factory.getOne review,

91
00:05:35,450 --> 00:05:40,193
‫and I think we don't have (mumbles) our routes just yet.

92
00:05:41,360 --> 00:05:46,357
‫And, indeed, we don't, so adding this real quick here,

93
00:05:46,357 --> 00:05:49,107
‫reviewController.getReview, okay?

94
00:05:54,300 --> 00:05:56,423
‫Let's just quickly test this one as well,

95
00:05:58,870 --> 00:06:01,300
‫just because I also want to then add it

96
00:06:01,300 --> 00:06:03,203
‫here to Postman, okay?

97
00:06:04,950 --> 00:06:08,453
‫So, just copying this one because it's similar,

98
00:06:10,560 --> 00:06:12,393
‫and then, indeed, it is get.

99
00:06:15,300 --> 00:06:20,300
‫And, yep, works perfectly fine, store it here in reviews,

100
00:06:23,730 --> 00:06:24,987
‫and getReview.

101
00:06:28,258 --> 00:06:30,560
‫All right, an now all that's missing is

102
00:06:30,560 --> 00:06:35,560
‫basically a getAll factory function, right?

103
00:06:36,070 --> 00:06:37,320
‫So, basically, to replace

104
00:06:39,200 --> 00:06:42,380
‫this getAllTours, getAllUsers,

105
00:06:42,380 --> 00:06:46,003
‫which we should also have somewhere here,

106
00:06:46,970 --> 00:06:50,530
‫so getAllUsers, and also getAllReviews,

107
00:06:50,530 --> 00:06:53,540
‫so let's again copy it here from the Tour controller

108
00:06:53,540 --> 00:06:56,380
‫because actually this is the one that is really complete

109
00:06:56,380 --> 00:06:59,920
‫because it has all these API features,

110
00:06:59,920 --> 00:07:03,390
‫and so, after creating our getAll factory function,

111
00:07:03,390 --> 00:07:07,083
‫all of the three resources will then get these API features.

112
00:07:09,299 --> 00:07:12,840
‫So again, copying all of this,

113
00:07:15,230 --> 00:07:17,433
‫exports.getAll,

114
00:07:24,630 --> 00:07:28,390
‫and so now, we will actually have to require

115
00:07:28,390 --> 00:07:29,743
‫these API features here,

116
00:07:31,690 --> 00:07:35,780
‫and in fact we can actually remove it from here

117
00:07:35,780 --> 00:07:38,320
‫because we will no longer need it

118
00:07:38,320 --> 00:07:43,277
‫because we now transfer this to the handler factory, right?

119
00:07:45,360 --> 00:07:49,403
‫Now, here all we need to do is then to replace, as always,

120
00:07:50,370 --> 00:07:53,243
‫with model, and then here,

121
00:07:56,300 --> 00:07:58,703
‫doc and data.

122
00:08:01,700 --> 00:08:03,253
‫Now, okay, perfect.

123
00:08:07,480 --> 00:08:09,133
‫So, let's see if this works,

124
00:08:13,947 --> 00:08:17,453
‫getAll with Tour.

125
00:08:22,530 --> 00:08:25,530
‫And now, I really want to see if this still works

126
00:08:25,530 --> 00:08:28,300
‫with all these features,

127
00:08:28,300 --> 00:08:31,380
‫and so it's here, getAll Tours,

128
00:08:31,380 --> 00:08:33,830
‫and so, let's add our query string here,

129
00:08:33,830 --> 00:08:38,467
‫and let's say duration greater or equal than let's say a 10,

130
00:08:43,040 --> 00:08:47,063
‫and sort by price.

131
00:08:50,470 --> 00:08:53,133
‫Okay, and so we get two results here.

132
00:08:56,170 --> 00:08:58,490
‫Okay, it's not working.

133
00:08:58,490 --> 00:09:03,030
‫Anyway, we get the two (chuckles) results here,

134
00:09:03,030 --> 00:09:06,290
‫and they both have a duration greater than 10,

135
00:09:06,290 --> 00:09:09,360
‫and the price is 1497,

136
00:09:09,360 --> 00:09:13,420
‫and the second one is higher, okay?

137
00:09:13,420 --> 00:09:15,810
‫Let's put it here like to seven,

138
00:09:15,810 --> 00:09:17,673
‫so we get a bit more results.

139
00:09:19,949 --> 00:09:21,249
‫Okay, and now we get five.

140
00:09:23,560 --> 00:09:27,480
‫Okay, anyway, let's now do the same for the reviews

141
00:09:28,910 --> 00:09:32,440
‫because I'm really excited to test out these API features

142
00:09:32,440 --> 00:09:34,350
‫on the reviews as well.

143
00:09:34,350 --> 00:09:36,510
‫Now, one problem that we have here is

144
00:09:36,510 --> 00:09:39,740
‫that this route handler actually has these two lines of code

145
00:09:40,706 --> 00:09:44,610
‫that all the other getAll handlers do not have, okay?

146
00:09:44,610 --> 00:09:47,730
‫But, what we're going to do here is to simply copy this code

147
00:09:47,730 --> 00:09:50,730
‫into our handler factory as well, okay?

148
00:09:50,730 --> 00:09:52,150
‫That's kind of a hack

149
00:09:52,150 --> 00:09:56,040
‫because we really only need this one for getAll reviews,

150
00:09:56,040 --> 00:09:59,210
‫but to get around this would be a bit too much work

151
00:09:59,210 --> 00:10:02,500
‫here in this situation, and once more,

152
00:10:02,500 --> 00:10:05,623
‫I simply want to keep it simple here, all right.

153
00:10:07,010 --> 00:10:08,593
‫So, let's copy this here,

154
00:10:10,530 --> 00:10:15,150
‫and I'm adding a comment here to allow

155
00:10:16,500 --> 00:10:21,500
‫for nested getReviews on Tour, okay?

156
00:10:24,980 --> 00:10:29,533
‫And so this filter, we will then also pass it in here, okay?

157
00:10:31,260 --> 00:10:33,363
‫So again, this is a small hack,

158
00:10:35,400 --> 00:10:37,460
‫but I'm gonna leave it here like this

159
00:10:37,460 --> 00:10:41,770
‫because I think it is reasonable, all right.

160
00:10:41,770 --> 00:10:44,223
‫Anyway, let's now get rid of this,

161
00:10:45,260 --> 00:10:48,373
‫so factory.getAll and review, okay?

162
00:10:54,476 --> 00:10:57,493
‫Let's actually put this down here next to all the others.

163
00:11:02,430 --> 00:11:03,430
‫And now, as you see,

164
00:11:03,430 --> 00:11:07,340
‫we actually do not even need this catchAsync here anymore,

165
00:11:07,340 --> 00:11:09,200
‫so I'm gonna comment it out here

166
00:11:09,200 --> 00:11:12,300
‫because, who knows, maybe in the future we're gonna need it

167
00:11:12,300 --> 00:11:15,523
‫for some other Middleware function here, all right.

168
00:11:16,870 --> 00:11:18,660
‫So, that looks nice and clean,

169
00:11:18,660 --> 00:11:21,783
‫and let's now test out the getAll reviews.

170
00:11:23,420 --> 00:11:28,420
‫All right, so let's actually just save changes here as well.

171
00:11:30,240 --> 00:11:32,310
‫These ones we don't need,

172
00:11:32,310 --> 00:11:36,143
‫but I actually want to get all the reviews.

173
00:11:37,530 --> 00:11:39,743
‫So, right now we have four reviews,

174
00:11:40,980 --> 00:11:43,430
‫and so let's now try to filter for reviews

175
00:11:43,430 --> 00:11:46,853
‫only with a rating of four, let's say.

176
00:11:48,860 --> 00:11:53,860
‫So, query string, rating equal four.

177
00:11:57,820 --> 00:12:00,170
‫Oh, and it actually works,

178
00:12:00,170 --> 00:12:02,850
‫so nice, that's really nice.

179
00:12:02,850 --> 00:12:06,780
‫So, just like this, we get access to all these API features

180
00:12:06,780 --> 00:12:11,780
‫like filtering, sorting, pagination, and all that good stuff

181
00:12:11,810 --> 00:12:13,988
‫that we implemented (chuckles)

182
00:12:13,988 --> 00:12:15,890
‫a long time ago in the course, right?

183
00:12:15,890 --> 00:12:17,380
‫And, I also want to make sure

184
00:12:17,380 --> 00:12:21,423
‫that this getAll reviews on Tour also still works.

185
00:12:22,300 --> 00:12:24,120
‫So, let's check that,

186
00:12:24,120 --> 00:12:26,893
‫and, yeah, you see that it actually does.

187
00:12:28,050 --> 00:12:30,380
‫So, the Tour ID that we have here is

188
00:12:30,380 --> 00:12:34,490
‫exactly the one right here and right here.

189
00:12:34,490 --> 00:12:36,840
‫So, that one is also still working,

190
00:12:36,840 --> 00:12:39,580
‫and so let's finally also use getAll

191
00:12:39,580 --> 00:12:44,060
‫for the user controller, all right.

192
00:12:44,060 --> 00:12:46,733
‫So, basically replacing simply this,

193
00:12:47,760 --> 00:12:52,760
‫so factory.getAll user, all right.

194
00:12:55,180 --> 00:12:57,174
‫And, let's actually put this one

195
00:12:57,174 --> 00:13:01,483
‫here close to the other ones, all right.

196
00:13:04,960 --> 00:13:06,910
‫And, then this one as well,

197
00:13:06,910 --> 00:13:09,650
‫so basically having all of these ones generated

198
00:13:09,650 --> 00:13:11,590
‫by factory functions together,

199
00:13:11,590 --> 00:13:14,383
‫and then all the others simply above them.

200
00:13:15,800 --> 00:13:18,580
‫All right, let's do the same here,

201
00:13:18,580 --> 00:13:20,290
‫this one is already nice and clean,

202
00:13:20,290 --> 00:13:23,153
‫and the same for the Tour controller,

203
00:13:24,170 --> 00:13:25,620
‫so what else do we have here?

204
00:13:27,430 --> 00:13:29,263
‫This one we can get rid of,

205
00:13:30,690 --> 00:13:34,000
‫and, okay, I think it looks nice like this.

206
00:13:34,000 --> 00:13:37,150
‫Again, we can comment out this piece of code

207
00:13:37,150 --> 00:13:40,410
‫because, for now at least, we don't need it anymore.

208
00:13:40,410 --> 00:13:44,623
‫And so, just to test it again here with the users,

209
00:13:45,640 --> 00:13:47,423
‫let's get all of them together,

210
00:13:48,890 --> 00:13:50,973
‫and, indeed, here they are.

211
00:13:53,320 --> 00:13:55,960
‫We can now also say that we only want the ones

212
00:13:55,960 --> 00:13:58,873
‫with the role of user,

213
00:14:00,490 --> 00:14:03,760
‫and, indeed, that works as well.

214
00:14:03,760 --> 00:14:07,500
‫Great, and that actually wraps up this part

215
00:14:07,500 --> 00:14:10,170
‫of creating handler factory functions,

216
00:14:10,170 --> 00:14:14,300
‫and this really was a huge step forwards in our project,

217
00:14:14,300 --> 00:14:16,030
‫so remember that, a bit later,

218
00:14:16,030 --> 00:14:18,710
‫we will also create a booking resource,

219
00:14:18,710 --> 00:14:22,080
‫and so you see that probably we're gonna be able to create

220
00:14:22,080 --> 00:14:25,070
‫the main API for these bookings

221
00:14:25,070 --> 00:14:27,310
‫in like five seconds or something,

222
00:14:27,310 --> 00:14:32,310
‫simply using these five factory functions, all right.

223
00:14:32,350 --> 00:14:34,750
‫Anyway, good work, great job,

224
00:14:34,750 --> 00:14:36,683
‫and I'll see you in the next one.

