1
00:00:00,110 --> 00:00:04,410
Well come back in the next few videos we're going to work on adding comments to your camp.

2
00:00:04,560 --> 00:00:08,280
So currently all that we have is a list of campgrounds.

3
00:00:08,450 --> 00:00:12,930
And when I click on a campground to go to the show page we see some information about it.

4
00:00:13,200 --> 00:00:18,270
But what we want to happen is for there to be a list of comments or reviews here and then a button that

5
00:00:18,270 --> 00:00:23,440
says I had a new review and we can then click on that and go to a form submit a new comment.

6
00:00:23,460 --> 00:00:26,560
Now take us back here and we'll see a list of comments.

7
00:00:27,000 --> 00:00:29,290
So there's a lot of stuff involved in getting that to work.

8
00:00:29,460 --> 00:00:31,120
We have to create the comment model.

9
00:00:31,140 --> 00:00:33,530
We have to associate it with the campground model.

10
00:00:33,530 --> 00:00:37,740
We have to require all the files correctly then we have to create all the routes that we have to create

11
00:00:37,740 --> 00:00:38,870
all the views.

12
00:00:39,540 --> 00:00:41,310
But we're actually going to start somewhere else.

13
00:00:41,430 --> 00:00:44,290
We're going to start by creating what's called a seed's file.

14
00:00:44,520 --> 00:00:49,340
And the point of a seed's file is that we can run it to seed our database with some data.

15
00:00:49,560 --> 00:00:53,240
So right now we have four campgrounds in the database.

16
00:00:53,430 --> 00:00:58,710
But what I want to do is write a file that I can run and what that file will do is empty everything

17
00:00:58,710 --> 00:01:03,240
in my database to start and then it's going to go in add in three or four campgrounds.

18
00:01:03,330 --> 00:01:04,910
And each one will have a few comments.

19
00:01:04,920 --> 00:01:07,080
So we have some sample data to work with.

20
00:01:07,140 --> 00:01:12,060
And the reason I'm doing this is so that when we add comments in we'll have a way to see immediately

21
00:01:12,060 --> 00:01:13,580
if the comments are working or not.

22
00:01:13,920 --> 00:01:18,810
Otherwise once we create the comment model then we'd have to get the comment new and create route working

23
00:01:18,820 --> 00:01:19,090
.

24
00:01:19,290 --> 00:01:23,260
And we have to do a lot more work before we can even tell if our comments are set up correctly.

25
00:01:23,310 --> 00:01:28,730
So we're going to create a seed's file going to do that right now in the same directory.

26
00:01:28,800 --> 00:01:30,530
I'm still working in version 3.

27
00:01:30,630 --> 00:01:34,300
I'm going to create a new file called seeds that yes.

28
00:01:34,890 --> 00:01:38,630
And I'll open that up.

29
00:01:38,820 --> 00:01:44,100
So to reiterate in here we're going to create a bunch of campgrounds and a few comments for each campground

30
00:01:44,110 --> 00:01:44,240
.

31
00:01:44,430 --> 00:01:49,170
And at first we're going to get errors and this is what some people call error driven development where

32
00:01:49,170 --> 00:01:50,960
we write some code that we want to work.

33
00:01:51,180 --> 00:01:55,590
We get an error and then we write some code to make that error go away and then we keep doing that until

34
00:01:55,590 --> 00:01:56,460
it works.

35
00:01:56,460 --> 00:01:58,260
So we're going to require mongoose first

36
00:02:01,230 --> 00:02:07,630
just like that and then we're going to require a campground and that will be require.

37
00:02:07,680 --> 00:02:11,330
And this is in the models directory slash camp ground.

38
00:02:11,960 --> 00:02:15,810
And I need quotes can't forget that.

39
00:02:15,810 --> 00:02:17,270
There we go.

40
00:02:17,340 --> 00:02:20,890
And then what we want to do to start is just wipe everything out of our database.

41
00:02:21,030 --> 00:02:26,240
And the easiest way to do that with Mongoose is campground remove.

42
00:02:26,790 --> 00:02:32,940
And we're just going to ask it to remove everything and then we'll add in a callback function error

43
00:02:32,960 --> 00:02:33,550
.

44
00:02:34,800 --> 00:02:41,520
And then here just add a console that log removed campgrounds

45
00:02:44,840 --> 00:02:48,080
and let's see what our error is here expected error to be handled.

46
00:02:48,210 --> 00:02:55,110
OK so technically that's not a deal breaker but if we do get an error we'll add cancel that log error

47
00:02:55,230 --> 00:02:56,670
right here.

48
00:02:56,700 --> 00:02:59,730
The next thing I want to talk about is how we're going to run this file.

49
00:02:59,880 --> 00:03:06,150
So in my app taught us what I want to be able to do is actually require the seeds file and we'll call

50
00:03:06,150 --> 00:03:16,830
it something like seed D.B and that should be equal to requiring dot slash seed's which is the name

51
00:03:16,830 --> 00:03:20,570
of our file right here seeds and that in the same directory is actually yes.

52
00:03:20,700 --> 00:03:26,570
So we need the dot slash and then I want to be able to execute it afterwards and do something like seed

53
00:03:26,580 --> 00:03:30,200
the database seeded be at the beginning.

54
00:03:30,210 --> 00:03:33,450
Every time we start the server over this code should run.

55
00:03:33,600 --> 00:03:37,440
Right now though when we require this file we're not exporting anything.

56
00:03:37,710 --> 00:03:39,600
And we want to export a function.

57
00:03:39,630 --> 00:03:42,580
So let's go back and let's just write a function here.

58
00:03:42,720 --> 00:03:46,310
Function seed DBI.

59
00:03:47,190 --> 00:03:55,530
And we're going to move this code into here just like that and indent this a bit and then we're going

60
00:03:55,530 --> 00:04:00,250
to do a module that exports equals seed DB.

61
00:04:01,110 --> 00:04:02,240
Just like that.

62
00:04:02,310 --> 00:04:10,050
And so this will send this function out and it will be stored inside of CDB here and then we can execute

63
00:04:10,050 --> 00:04:15,210
it and it will run whatever's in this function and all that it does right now is removes all campgrounds

64
00:04:15,230 --> 00:04:15,590
.

65
00:04:15,930 --> 00:04:20,120
So if it works when we load up the page all the campground should be gone.

66
00:04:20,790 --> 00:04:25,620
Which seems a little counterintuitive for now but this will make more sense when we have the Creation

67
00:04:25,620 --> 00:04:26,490
working too.

68
00:04:26,790 --> 00:04:27,980
So let's do this now.

69
00:04:28,170 --> 00:04:32,480
Node Aptor Yes we get removed campgrounds.

70
00:04:32,490 --> 00:04:35,760
So call this function if we are fresh.

71
00:04:36,030 --> 00:04:37,940
We now have no campgrounds.

72
00:04:37,950 --> 00:04:38,580
All right.

73
00:04:39,000 --> 00:04:41,670
So let's clean this up and out a little comment here.

74
00:04:41,670 --> 00:04:47,700
Remove all campgrounds then what we want to do next is add in a few campgrounds.

75
00:04:48,030 --> 00:04:54,680
So add a few campgrounds and then we also want to add a few comments eventually.

76
00:04:55,380 --> 00:04:56,840
Let's start with the campground.

77
00:04:57,240 --> 00:05:01,080
So to create a campground we need to do a campground created.

78
00:05:01,440 --> 00:05:03,030
And we need to pass in data.

79
00:05:03,180 --> 00:05:07,500
But if we want to create a bunch of different campgrounds when I'm going to do is actually find some

80
00:05:07,500 --> 00:05:09,850
data up here we'll just call it data.

81
00:05:10,160 --> 00:05:11,210
And it's an array.

82
00:05:11,490 --> 00:05:17,610
And in that array going to have a few objects and each object is going to be the starter data or the

83
00:05:17,880 --> 00:05:19,570
data for one campground.

84
00:05:19,650 --> 00:05:27,150
So we'll have name and we'll just do Cloud's rest again and then I'll get a new photo or use this one

85
00:05:27,150 --> 00:05:28,280
for now.

86
00:05:30,620 --> 00:05:35,980
And then that will be image and then format this a little bit nicer.

87
00:05:36,580 --> 00:05:42,430
Let's put this on separate lines and then we need a description as well and our description will just

88
00:05:42,430 --> 00:05:44,480
be blah blah blah.

89
00:05:45,040 --> 00:05:52,930
OK so that's one piece of data and then I'm going to add in at least two more just like that and we'll

90
00:05:52,930 --> 00:05:54,410
do one more now.

91
00:05:55,210 --> 00:05:57,120
So we have three starter pieces of data.

92
00:05:57,130 --> 00:05:58,770
I will change the images.

93
00:05:59,020 --> 00:06:01,030
So let's take this one as an image

94
00:06:06,250 --> 00:06:10,890
and we'll call this desert Mesa or something like that.

95
00:06:11,140 --> 00:06:15,960
And then we'll add one more in and we'll go with this right here.

96
00:06:16,030 --> 00:06:24,430
Copy the image or L and we'll call this canyon floor and I'll paste that image in and then we'll keep

97
00:06:24,430 --> 00:06:25,790
the same description.

98
00:06:25,810 --> 00:06:26,040
All right.

99
00:06:26,050 --> 00:06:31,390
So what we've done so far we're not using this yet but we've to find an array that has three pieces

100
00:06:31,390 --> 00:06:37,060
of data and each one is an object that has a name image and description which is exactly what our model

101
00:06:37,060 --> 00:06:38,020
was expecting.

102
00:06:38,020 --> 00:06:42,490
If we go over here model his name image and description.

103
00:06:42,900 --> 00:06:43,230
OK.

104
00:06:43,240 --> 00:06:49,780
So then what we need to do is actually loop through this data and create a campground for each one so

105
00:06:49,780 --> 00:06:51,270
we can do it for each.

106
00:06:51,280 --> 00:06:53,390
So I'll do that right now.

107
00:06:53,500 --> 00:07:02,600
We'll do a data for each function and we'll just call it seed.

108
00:07:02,620 --> 00:07:05,190
So that seed is going to represent one of these.

109
00:07:05,410 --> 00:07:12,310
And then inside the loop we're going to do a campground or do you have it written here campgrounds create

110
00:07:13,040 --> 00:07:18,430
and what we're going to pass in is not an object in curly braces to give it seed just like that.

111
00:07:18,700 --> 00:07:26,040
And then our function here and this will have error and we'll also have data and we'll do it.

112
00:07:26,080 --> 00:07:36,460
If there's an error cancel out log error else Council vote log and we'll just you added a campground

113
00:07:36,470 --> 00:07:37,430
.

114
00:07:38,410 --> 00:07:44,170
So we've created this array of starter data three different objects that we want to add to the database

115
00:07:44,710 --> 00:07:50,120
and then we're looping through that and we're adding each one using campgrounds create.

116
00:07:50,140 --> 00:07:57,880
So if we run this now if we restart the server we should see Yelp server had started up camp server

117
00:07:58,090 --> 00:08:03,760
added a campground added campground added campground and then we get removed campgrounds and that might

118
00:08:03,760 --> 00:08:05,150
not be what you're expecting.

119
00:08:05,290 --> 00:08:07,970
But the reason that that's happening is really important.

120
00:08:08,020 --> 00:08:09,840
This is why we use callbacks.

121
00:08:10,030 --> 00:08:17,170
So remember when we run campgrounds remove there's no guarantee that this code is going to happen after

122
00:08:17,170 --> 00:08:20,290
this finishes unless we put it inside the callback.

123
00:08:20,320 --> 00:08:27,790
So we actually want to move all of this inside of this callback here right there campground out remove

124
00:08:28,690 --> 00:08:34,410
and we'll just paste that in and we'll indent this just like that.

125
00:08:34,720 --> 00:08:40,300
So what this will do now is wait until we remove all the existing campgrounds and then will add in our

126
00:08:40,300 --> 00:08:41,800
new campgrounds.

127
00:08:41,800 --> 00:08:42,260
All right.

128
00:08:42,490 --> 00:08:48,080
So now we should see those in a different order and we get removed campground added added added.

129
00:08:48,230 --> 00:08:52,620
And if we refresh we now see those three campgrounds.

130
00:08:52,990 --> 00:08:53,220
OK.

131
00:08:53,230 --> 00:08:58,990
So we have our sea data working now we want to work on comments and the way that I'm going to do this

132
00:08:58,990 --> 00:09:06,550
is after we create a campground inside the else I'm going to create our comments on each campground

133
00:09:06,850 --> 00:09:09,370
and we'll just do the same comment this time.

134
00:09:09,370 --> 00:09:15,610
So what we want to do is run comments that create which doesn't exist yet it's not going to work and

135
00:09:15,610 --> 00:09:17,060
that's fine we'll get an error.

136
00:09:17,380 --> 00:09:25,600
And the comment that we're going to create we'll have a text of this place is great but I wish there

137
00:09:25,600 --> 00:09:29,280
was Internet and let's form at this a little bit better.

138
00:09:29,290 --> 00:09:36,010
So we're going to create this comment that has text and will have an author of Homer.

139
00:09:36,530 --> 00:09:42,850
OK so this will create a comment and then we need our callback here function error and then the comment

140
00:09:42,880 --> 00:09:48,170
that was created you can see this is getting out of hand quickly with all these comments.

141
00:09:48,310 --> 00:09:50,440
You will see a way to refactor this soon.

142
00:09:50,440 --> 00:09:52,460
This is what's called Colback hell.

143
00:09:52,510 --> 00:09:59,200
If so many callbacks So this will run once the comment has created and once it is created we then want

144
00:09:59,200 --> 00:10:03,970
to associate it with the campground and then we're going to grab the campground that was created which

145
00:10:03,970 --> 00:10:05,450
we're currently calling data.

146
00:10:05,710 --> 00:10:11,170
And rather than calling it data let's just call it campground and then down here we're going to do a

147
00:10:11,260 --> 00:10:19,900
campground dot comments up push the new comment that we just created that has this text and this author

148
00:10:19,910 --> 00:10:20,200
.

149
00:10:20,440 --> 00:10:22,400
I don't need a comma here as well.

150
00:10:23,230 --> 00:10:29,590
And then once we do that we need to save the campground and then we also add the error handling in here

151
00:10:29,590 --> 00:10:31,090
so will add an if error.

152
00:10:31,300 --> 00:10:36,190
So if we can't create a comment then we'll constantly log an error.

153
00:10:38,250 --> 00:10:45,880
And if we can create a comment then we're going to associate it with the campground and we'll also do

154
00:10:45,930 --> 00:10:53,330
cancel that log in here that says created new comments.

155
00:10:53,350 --> 00:10:53,850
All right.

156
00:10:53,890 --> 00:10:55,460
So this is a little bit of a marathon.

157
00:10:55,570 --> 00:10:58,230
Let's let's go over some of this.

158
00:10:58,270 --> 00:11:04,260
So what we're doing we're starting by removing everything in the database all campgrounds.

159
00:11:04,300 --> 00:11:08,890
Technically we're not removing comments so we can come back and fix that once we have comments.

160
00:11:09,040 --> 00:11:14,170
But we are moving all campgrounds and then we're creating three campgrounds.

161
00:11:14,170 --> 00:11:17,460
Each one unique because of the seed data.

162
00:11:17,980 --> 00:11:23,680
And then once we create one we're going to create a comment for that one and the comments are all the

163
00:11:23,680 --> 00:11:24,370
same.

164
00:11:24,370 --> 00:11:26,310
So we're going to end up with three different posts.

165
00:11:26,380 --> 00:11:27,510
Three different campgrounds.

166
00:11:27,730 --> 00:11:33,520
But when you click on them we'll see if they have the exact same comment that says This place is great

167
00:11:33,550 --> 00:11:36,240
but I wish there was Internet written by Homer.

168
00:11:36,520 --> 00:11:41,260
And then once we create that comment we then have to associate it with the campground push it into the

169
00:11:41,260 --> 00:11:47,110
comments array on the campground and then save the campground and then we'll print out created new comment

170
00:11:48,010 --> 00:11:49,730
and you'll see we have a little warning here.

171
00:11:49,750 --> 00:11:51,270
Comment is not defined.

172
00:11:51,640 --> 00:11:55,670
We'll need to add a VAR comment.

173
00:11:55,990 --> 00:12:03,830
Equals require the non-existent dot slash models slash comment.

174
00:12:04,210 --> 00:12:06,160
And this is what I mean by error driven development.

175
00:12:06,160 --> 00:12:13,130
If I stopped the server and I restart I get a message that tells me I cannot find module models.

176
00:12:13,150 --> 00:12:17,650
Slash comment in the next video we're going to focus on making these errors go away.

177
00:12:17,650 --> 00:12:23,320
So what we've done is we set up a plan seed's file that works the way that we want it to work but it

178
00:12:23,320 --> 00:12:27,440
doesn't quite work yet and we now need to make it work.
