﻿1
00:00:01,336 --> 00:00:03,960
‫In this video, let's continue to translate

2
00:00:03,960 --> 00:00:06,900
‫the data model that we established right at the beginning

3
00:00:06,900 --> 00:00:09,600
‫of the section into some actual code.

4
00:00:09,600 --> 00:00:13,023
‫And so, this time we're gonna implement the reviews model.

5
00:00:14,780 --> 00:00:17,870
‫So the first step is to create a new file

6
00:00:17,870 --> 00:00:22,273
‫in the models folder and call it ReviewModel.js.

7
00:00:25,800 --> 00:00:29,480
‫And to me, this looks like yet another great challenge

8
00:00:29,480 --> 00:00:31,640
‫for you to practice some of the skills

9
00:00:31,640 --> 00:00:34,090
‫that you already learned up to this point.

10
00:00:34,090 --> 00:00:36,890
‫Okay so, in this challenge, I want you to create

11
00:00:36,890 --> 00:00:38,200
‫the review model.

12
00:00:38,200 --> 00:00:41,370
‫And what we need in there is actually very simple.

13
00:00:41,370 --> 00:00:43,220
‫So let me put that here in a comment.

14
00:00:44,360 --> 00:00:46,090
‫So we basically want a review,

15
00:00:46,090 --> 00:00:48,030
‫so the text for the review.

16
00:00:48,030 --> 00:00:49,350
‫We want a rating

17
00:00:50,230 --> 00:00:51,840
‫so just like this.

18
00:00:51,840 --> 00:00:54,950
‫Then we also want a createdAt

19
00:00:54,950 --> 00:00:56,760
‫so with the current timestamp

20
00:00:56,760 --> 00:01:00,880
‫and then we also want a reference to the tour

21
00:01:00,880 --> 00:01:02,590
‫that this review belongs to

22
00:01:02,590 --> 00:01:05,793
‫and also to the user who wrote this review.

23
00:01:07,870 --> 00:01:12,090
‫Okay, so basically two parent references here, right.

24
00:01:12,090 --> 00:01:14,270
‫And so with the stuff that you learned

25
00:01:14,270 --> 00:01:15,790
‫in the last couple of lectures,

26
00:01:15,790 --> 00:01:18,410
‫you should be able to actually complete this challenge

27
00:01:18,410 --> 00:01:19,660
‫completely on your own.

28
00:01:19,660 --> 00:01:23,033
‫So, pause the video now and good luck with that.

29
00:01:26,690 --> 00:01:29,430
‫Now, okay, so I hope you didn't just skip this

30
00:01:29,430 --> 00:01:33,070
‫and instead, you really implemented it by yourself

31
00:01:33,070 --> 00:01:35,813
‫and anyway, this is how I created it.

32
00:01:36,780 --> 00:01:39,490
‫So of course, we need to start by requiring mongoose

33
00:01:46,880 --> 00:01:47,713
‫and then

34
00:01:50,728 --> 00:01:54,480
‫the reviewSchema is equal to new mongoose.Schema.

35
00:02:00,780 --> 00:02:03,053
‫And so let's start adding our fields.

36
00:02:03,960 --> 00:02:06,790
‫So first the review is of course a string.

37
00:02:06,790 --> 00:02:09,830
‫Now, right and of course, I'm doing this too fast,

38
00:02:09,830 --> 00:02:12,660
‫so I also need to specify type

39
00:02:12,660 --> 00:02:15,013
‫and then let's also set it to a required,

40
00:02:16,930 --> 00:02:17,763
‫true,

41
00:02:19,180 --> 00:02:22,623
‫review can not be empty.

42
00:02:23,820 --> 00:02:26,333
‫Then we want a rating,

43
00:02:30,130 --> 00:02:32,663
‫and this one is of the type number.

44
00:02:33,940 --> 00:02:37,333
‫And the rating is ideally between one and five.

45
00:02:38,580 --> 00:02:43,580
‫And so the minimum of one and the max is five.

46
00:02:43,620 --> 00:02:44,700
‫So as you already know,

47
00:02:44,700 --> 00:02:47,000
‫these are some simple validators.

48
00:02:47,000 --> 00:02:50,260
‫That work only for numbers, right.

49
00:02:50,260 --> 00:02:51,543
‫This is not correct here.

50
00:02:52,570 --> 00:02:53,640
‫Okay.

51
00:02:53,640 --> 00:02:55,733
‫Next up, the createdAt.

52
00:02:59,630 --> 00:03:01,783
‫Into this is of type, date,

53
00:03:05,120 --> 00:03:07,360
‫and by default we want

54
00:03:10,380 --> 00:03:12,300
‫Date.now.

55
00:03:12,300 --> 00:03:13,133
‫Okay.

56
00:03:14,480 --> 00:03:16,700
‫So this was the easy part.

57
00:03:16,700 --> 00:03:18,980
‫And lets actually also create a model

58
00:03:18,980 --> 00:03:21,523
‫and export it before adding these references.

59
00:03:22,930 --> 00:03:25,023
‫So const,

60
00:03:26,090 --> 00:03:29,710
‫Review is equal too mongoose.model

61
00:03:32,630 --> 00:03:34,320
‫with the name Review.

62
00:03:34,320 --> 00:03:35,920
‫And then of course reviewSchema.

63
00:03:41,210 --> 00:03:44,430
‫To finish of course module.export

64
00:03:46,040 --> 00:03:49,340
‫equals the Review.

65
00:03:49,340 --> 00:03:52,890
‫Alright, and now comes the reference part.

66
00:03:52,890 --> 00:03:56,880
‫So a review of course needs to belong to a tour.

67
00:03:56,880 --> 00:03:58,900
‫And it also needs an author.

68
00:03:58,900 --> 00:03:59,733
‫Okay.

69
00:03:59,733 --> 00:04:01,710
‫So that's again what we specified

70
00:04:01,710 --> 00:04:03,910
‫in our data modeling lecture.

71
00:04:03,910 --> 00:04:06,000
‫So basically that we were going to implement

72
00:04:06,000 --> 00:04:09,030
‫parent referencing here in this case.

73
00:04:09,030 --> 00:04:12,060
‫Because both the tour and the user

74
00:04:12,060 --> 00:04:15,020
‫are in a sense the parents of this data set.

75
00:04:15,020 --> 00:04:17,080
‫And we decided to do it this way

76
00:04:17,080 --> 00:04:18,790
‫because we are going to potentially

77
00:04:18,790 --> 00:04:22,140
‫huge erase any parent elements right?

78
00:04:22,140 --> 00:04:24,680
‫So we should not design our application

79
00:04:24,680 --> 00:04:27,600
‫thinking that there will only be a few reviews.

80
00:04:27,600 --> 00:04:30,660
‫Only to then come back to it after some time

81
00:04:30,660 --> 00:04:33,700
‫and find out that our assumptions were wrong.

82
00:04:33,700 --> 00:04:36,660
‫And now we need to rebuild our entire data model.

83
00:04:36,660 --> 00:04:38,340
‫So that would be terrible.

84
00:04:38,340 --> 00:04:41,110
‫So in many situations, as you already know

85
00:04:41,110 --> 00:04:44,520
‫what we do not really know, how much our erase will grow.

86
00:04:44,520 --> 00:04:47,970
‫Then it's just best to opt for parent referencing.

87
00:04:47,970 --> 00:04:50,580
‫So again, that's exactly what we're doing here

88
00:04:50,580 --> 00:04:53,400
‫when we're referencing tour and user.

89
00:04:53,400 --> 00:04:56,383
‫So lets actually now go ahead and implement that.

90
00:04:57,390 --> 00:04:58,853
‫And starting with the tour.

91
00:05:01,070 --> 00:05:02,650
‫Alright.

92
00:05:02,650 --> 00:05:06,153
‫So here we need some space, and okay.

93
00:05:07,630 --> 00:05:12,140
‫So the type is as we did before, mongoose,

94
00:05:12,140 --> 00:05:13,423
‫and that's not correct.

95
00:05:14,290 --> 00:05:18,593
‫So mongoose.Schema.ObjectId,

96
00:05:21,600 --> 00:05:25,890
‫with a capital O and a capital I, but not the D.

97
00:05:25,890 --> 00:05:26,930
‫And I'm just telling you that

98
00:05:26,930 --> 00:05:29,760
‫because I once wrote ID like this,

99
00:05:29,760 --> 00:05:32,370
‫and then spent half an hour trying to figure out

100
00:05:32,370 --> 00:05:34,560
‫why my application wasn't working.

101
00:05:34,560 --> 00:05:37,293
‫Okay, so this D needs to be lowercase.

102
00:05:38,880 --> 00:05:39,713
‫Anyway.

103
00:05:39,713 --> 00:05:44,540
‫The reference in this case is of course the tour.

104
00:05:44,540 --> 00:05:46,813
‫And we also want to make this required.

105
00:05:49,680 --> 00:05:54,680
‫So true and then Review must belong

106
00:05:57,300 --> 00:05:58,780
‫to a tour.

107
00:05:58,780 --> 00:05:59,670
‫Okay.

108
00:05:59,670 --> 00:06:02,820
‫And just like this, each Review document now knows

109
00:06:02,820 --> 00:06:05,870
‫exactly what tour it belongs too.

110
00:06:05,870 --> 00:06:08,570
‫While the tour of course doesn't know initially

111
00:06:08,570 --> 00:06:11,640
‫what Reviews and how many Reviews there are.

112
00:06:11,640 --> 00:06:13,070
‫But that it a problem that

113
00:06:13,070 --> 00:06:14,820
‫we will actually solve a bit later.

114
00:06:16,290 --> 00:06:17,220
‫Okay.

115
00:06:17,220 --> 00:06:20,453
‫Next up, when there is a Review, we not only

116
00:06:20,453 --> 00:06:23,270
‫want to know what tour it belongs to,

117
00:06:23,270 --> 00:06:25,373
‫but also who wrote this Review.

118
00:06:26,570 --> 00:06:27,933
‫So user,

119
00:06:29,580 --> 00:06:32,410
‫and again, the same type of

120
00:06:32,410 --> 00:06:37,243
‫mongoose.Schema.ObjectId,

121
00:06:38,610 --> 00:06:39,443
‫and this time

122
00:06:39,443 --> 00:06:44,443
‫the reference, as you can imagine is gonna be user.

123
00:06:44,460 --> 00:06:47,880
‫Okay, and again we make it required

124
00:06:47,880 --> 00:06:51,960
‫because well, a review really cannot work

125
00:06:51,960 --> 00:06:54,830
‫without an author basically.

126
00:06:54,830 --> 00:06:56,800
‫And so review

127
00:06:58,250 --> 00:07:01,163
‫must belong to a user.

128
00:07:05,080 --> 00:07:06,563
‫Okay, that's actually it.

129
00:07:07,470 --> 00:07:08,303
‫Alright.

130
00:07:09,670 --> 00:07:12,750
‫Now just to finish lets actually also add these options

131
00:07:12,750 --> 00:07:16,280
‫to the Schema where we make it so that virtual properties

132
00:07:16,280 --> 00:07:19,470
‫also show up in Json and object outputs.

133
00:07:19,470 --> 00:07:20,303
‫Okay.

134
00:07:20,303 --> 00:07:22,450
‫So we did that before in the tours.

135
00:07:22,450 --> 00:07:25,290
‫I'm not sure if we did it in the user model.

136
00:07:25,290 --> 00:07:27,080
‫But anyway, here we are actually

137
00:07:27,080 --> 00:07:28,480
‫gonna need it a bit later.

138
00:07:28,480 --> 00:07:30,160
‫So we need to specify that.

139
00:07:30,160 --> 00:07:32,710
‫Remember, basically what I mean

140
00:07:36,320 --> 00:07:37,860
‫is this part.

141
00:07:37,860 --> 00:07:40,570
‫So I'm actually going to go ahead and copy it.

142
00:07:40,570 --> 00:07:42,023
‫Just to save us some time.

143
00:07:45,250 --> 00:07:48,580
‫And so again, all this does is to really make sure

144
00:07:48,580 --> 00:07:50,730
‫that when we have a virtual property,

145
00:07:50,730 --> 00:07:53,350
‫basically a field that is not stored in the database

146
00:07:53,350 --> 00:07:55,850
‫but calculated using some other value.

147
00:07:55,850 --> 00:07:59,670
‫So we want this to also show up whenever there is an output.

148
00:07:59,670 --> 00:08:02,113
‫Right, and so that's what we do here.

149
00:08:03,130 --> 00:08:03,963
‫Okay.

150
00:08:03,963 --> 00:08:07,490
‫And so I actually think that's actually it for now.

151
00:08:07,490 --> 00:08:08,550
‫Very simple.

152
00:08:08,550 --> 00:08:11,530
‫The only a bit tricky part is actually to create

153
00:08:11,530 --> 00:08:13,010
‫these references here.

154
00:08:13,010 --> 00:08:14,680
‫But we actually did that before

155
00:08:14,680 --> 00:08:17,673
‫and so it really works in the exact same way.

156
00:08:19,430 --> 00:08:21,800
‫So in the next video we will then use the Schema

157
00:08:21,800 --> 00:08:24,470
‫in order to create some new reviews.

158
00:08:24,470 --> 00:08:25,653
‫So see you then.

