1
00:00:00,150 --> 00:00:05,430
Welcome back in this lesson we're going to talk a bit more about this concept of rest and what that

2
00:00:05,430 --> 00:00:07,280
is and what restful routes are.

3
00:00:07,350 --> 00:00:12,080
And we're going to review the restful route we've seen and then we're going to add another route in

4
00:00:12,080 --> 00:00:12,280
.

5
00:00:12,360 --> 00:00:14,970
That's called this show page or this show route.

6
00:00:14,970 --> 00:00:19,440
So we have four main things reviewing the restful routes we've seen so far.

7
00:00:19,680 --> 00:00:25,020
We're going to change the campground schema and add a description to each campground when to show you

8
00:00:25,020 --> 00:00:30,240
a quick Mongo command and then we're going to spend the bulk of our time on creating a show route and

9
00:00:30,240 --> 00:00:32,110
a template showed that each of us.

10
00:00:32,430 --> 00:00:35,120
Let's begin by reviewing restful routes.

11
00:00:35,220 --> 00:00:41,610
So just before I move on I'm still in version and before I move on I'm still using V-2 just in case

12
00:00:41,610 --> 00:00:44,090
you want to follow along using the code.

13
00:00:44,100 --> 00:00:44,520
All right.

14
00:00:44,630 --> 00:00:45,460
So.

15
00:00:45,640 --> 00:00:48,060
Yes let's take a look at what we have.

16
00:00:48,090 --> 00:00:55,850
We have two main routes apt get slash campground act up post slash campground and we have one more apt

17
00:00:55,860 --> 00:01:03,570
get slash campground slash new and those three routes follow what's known as restful routes and there

18
00:01:03,570 --> 00:01:09,310
are seven of them and the first one that we're going to talk about is known as the index route.

19
00:01:09,720 --> 00:01:13,040
And we're going to do this as an example for dogs again.

20
00:01:13,080 --> 00:01:20,280
So slash dog should be the you know as a get request and what it does or what it's supposed to do is

21
00:01:21,930 --> 00:01:25,570
display a list of all dogs.

22
00:01:26,400 --> 00:01:29,260
So let me make this a little bit of a table here.

23
00:01:29,700 --> 00:01:31,110
It's going to be pretty rudimentary.

24
00:01:31,110 --> 00:01:32,710
Don't want to spend too much time on it.

25
00:01:33,150 --> 00:01:41,820
And our headers are just going to be the name of the route and then we'll have the URL or the path and

26
00:01:41,820 --> 00:01:51,040
then we'll have the verb in this case you get and then we have the description of what it does.

27
00:01:51,550 --> 00:01:52,120
OK.

28
00:01:52,410 --> 00:01:59,430
So we have the index route which for now what we have is slash campgrounds which is going to show a

29
00:01:59,430 --> 00:02:03,280
list of all campgrounds which is what it does.

30
00:02:03,750 --> 00:02:09,540
Then the next one that we have talked about is called new and new is not what you think it is.

31
00:02:09,720 --> 00:02:13,210
It's not actually the post route that makes the new dog or the new campground.

32
00:02:13,410 --> 00:02:15,560
It's slash dog slash new.

33
00:02:15,900 --> 00:02:20,040
And it's the route that actually shows the form.

34
00:02:20,040 --> 00:02:24,200
So let me space this out a little more.

35
00:02:24,270 --> 00:02:24,950
There we go.

36
00:02:25,110 --> 00:02:26,710
And it to get route.

37
00:02:27,330 --> 00:02:34,970
And it displays form to make a new dog and then that form submits to what's known as create which is

38
00:02:34,970 --> 00:02:44,130
flash dogs the same exact as the indexers same Yoro except it's a post and this is supposed to add new

39
00:02:44,130 --> 00:02:46,420
dog to database.

40
00:02:46,450 --> 00:02:48,360
You might be wondering why does this exist.

41
00:02:48,360 --> 00:02:50,310
Why do we need to follow this pattern.

42
00:02:50,310 --> 00:02:55,650
The first answer is that it's always good to have some structure just for our own purposes but also

43
00:02:55,650 --> 00:02:59,130
this pattern is something that lots and lots of applications will follow.

44
00:02:59,130 --> 00:03:03,220
And it makes it reliable and predictable for us to interact with other applications.

45
00:03:03,240 --> 00:03:10,050
Once we talk about API is we'll know that if we want to get all of the users from the get hub API we

46
00:03:10,050 --> 00:03:17,430
can do API get hub dot com slash users and that will give us a list of all the users that the users

47
00:03:17,460 --> 00:03:18,900
index.

48
00:03:18,900 --> 00:03:20,100
So we'll come back to that.

49
00:03:20,280 --> 00:03:25,130
But again these are the three we've seen so index is right here and I'll go label these.

50
00:03:25,200 --> 00:03:26,520
This is our index route

51
00:03:29,240 --> 00:03:34,520
just right there show all campgrounds.

52
00:03:34,620 --> 00:03:47,880
This is our create route add new campground to database and this route is new which is show form to

53
00:03:47,910 --> 00:03:50,750
create new campgrounds.

54
00:03:50,880 --> 00:03:54,480
Remember we have to have two routes in order to send a post request.

55
00:03:54,480 --> 00:04:00,210
We need one to show the form and then we need that form to submit somewhere which is our create route

56
00:04:00,230 --> 00:04:00,620
.

57
00:04:00,990 --> 00:04:01,220
OK.

58
00:04:01,230 --> 00:04:03,450
So those are the three routes we've done so far.

59
00:04:03,780 --> 00:04:08,200
We're going to introduce another route right now which is called show.

60
00:04:08,520 --> 00:04:13,080
So imagine that we have our apps it up like this which you don't have to imagine we do have this set

61
00:04:13,080 --> 00:04:13,600
up.

62
00:04:13,820 --> 00:04:18,870
And if I wanted to be able to click on one of these and view more information and that's what we're

63
00:04:18,870 --> 00:04:24,360
going to do we want to be able to have a button here where I can view information like a map the different

64
00:04:24,360 --> 00:04:26,590
reviews the comments the ratings.

65
00:04:26,640 --> 00:04:31,890
Any other images or whatever other information I want to show that doesn't make sense to put right here

66
00:04:32,430 --> 00:04:34,410
which normally there's a lot to show.

67
00:04:34,410 --> 00:04:40,320
And we only show one or two things on the index page like a photo and the title just like on Reddit

68
00:04:40,440 --> 00:04:42,840
which I know I've been using a lot as an example.

69
00:04:42,900 --> 00:04:48,780
But if this is our index this is showing all of the posts that we have we're not going to show every

70
00:04:48,780 --> 00:04:50,690
single piece of information at once.

71
00:04:50,760 --> 00:04:56,460
It just shows that title shows the link the number of comments the time it was submitted.

72
00:04:56,460 --> 00:05:01,800
The user and then we can click to view the comments on a separate page and there's tons and tons of

73
00:05:01,800 --> 00:05:03,010
information to show here.

74
00:05:03,090 --> 00:05:08,790
But this isn't all just shown right away on the index page so what we're going to do is make another

75
00:05:08,790 --> 00:05:16,460
route which is called show and the URL looks like this slash Doug's slash ID as a variable and it's

76
00:05:16,470 --> 00:05:22,340
a GET request and it shows info about one dog.

77
00:05:22,820 --> 00:05:28,440
And the reason we put the ID there is that we need to know which dog we're showing because up until

78
00:05:28,440 --> 00:05:31,310
this point index always works the same way.

79
00:05:31,310 --> 00:05:33,410
It shows you all dogs no matter what.

80
00:05:33,560 --> 00:05:34,900
This always shows you a form.

81
00:05:34,940 --> 00:05:36,610
This always ends in a new dog.

82
00:05:36,720 --> 00:05:41,600
But show is a little different because now we need to show you information about one thing in particular

83
00:05:41,620 --> 00:05:42,050
.

84
00:05:42,480 --> 00:05:43,970
So to do that.

85
00:05:43,980 --> 00:05:46,610
Not sure how to open this up accidentally to do that.

86
00:05:46,620 --> 00:05:52,440
We need to define a new route and we're going to do that now go back here and we'll put it at the bottom

87
00:05:54,100 --> 00:06:03,270
and it's a GET request don't get slash campground slash colon ID and then we'll just add the basic thing

88
00:06:03,300 --> 00:06:07,630
here are basic callback with request and response.

89
00:06:07,700 --> 00:06:11,410
And then in here we'll do a redstart send.

90
00:06:11,430 --> 00:06:22,240
This will be the show page one day and save and let's start the app up and if we go to that you are

91
00:06:22,250 --> 00:06:23,780
out any you are all that follows.

92
00:06:23,790 --> 00:06:30,930
Campground slash ID any ID we get this will be the show page one day if we go to campgrounds slash new

93
00:06:30,930 --> 00:06:35,580
we still get the form and thats because the order of the route is correct.

94
00:06:35,570 --> 00:06:41,810
Remember if I switch These all that this pattern is saying is campground slash anything any single word

95
00:06:42,500 --> 00:06:45,400
which is technically what we have here campground slash new.

96
00:06:45,440 --> 00:06:48,420
So we need to be careful that this is declared first.

97
00:06:48,420 --> 00:06:54,360
Otherwise it will treat campgrounds slash new as a campground slash ID route and it will show us this

98
00:06:54,400 --> 00:06:54,640
.

99
00:06:54,880 --> 00:07:02,190
Okay so thats a minor note but its important what we'll want to do in here is find the campground with

100
00:07:02,190 --> 00:07:10,900
provided ID and then render show template with that campground and all set.

101
00:07:11,190 --> 00:07:15,330
So the show page is supposed to show more information about one item.

102
00:07:15,560 --> 00:07:20,860
Right now we're showing all the information that we have on the index page.

103
00:07:20,880 --> 00:07:22,600
We don't have anything else to show.

104
00:07:22,640 --> 00:07:25,790
I guess we have an idea technically but that's not really useful.

105
00:07:25,800 --> 00:07:31,110
So we're going to add a description to our campground and that description will just be a little bit

106
00:07:31,110 --> 00:07:34,790
of text that we can enter in when we create a new campground.

107
00:07:34,800 --> 00:07:43,170
So we'll do that now and we'll go up here to our schema and just add in description and we started out

108
00:07:43,160 --> 00:07:50,240
as a string and see if we have a small problem which is that the campgrounds that we have already there's

109
00:07:50,250 --> 00:07:52,690
three of them don't have a description.

110
00:07:53,000 --> 00:07:57,990
So we can either update everything in our database to have a description or we can be a little bit more

111
00:07:57,990 --> 00:08:04,530
destructive and just delete everything in a database with what's known as DBI data collection drop and

112
00:08:04,520 --> 00:08:07,650
we'll drop all the campgrounds which is what I'm going to do here.

113
00:08:07,730 --> 00:08:11,990
It is something that you do occasionally when you make some big changes to your data and you have some

114
00:08:12,000 --> 00:08:14,290
pre-existing data that you don't really care about.

115
00:08:14,340 --> 00:08:16,680
You would never really drop users stayed on purpose.

116
00:08:16,710 --> 00:08:19,370
But in this case it's just our starter our sea data.

117
00:08:19,380 --> 00:08:20,840
So I'm going to drop it.

118
00:08:20,960 --> 00:08:23,220
So I'm going to open up Mongo.

119
00:08:23,220 --> 00:08:24,970
I'm going to connect to the right database.

120
00:08:24,990 --> 00:08:25,740
Let's see.

121
00:08:25,740 --> 00:08:27,060
Show DVRs.

122
00:08:27,120 --> 00:08:33,840
We're going to use Yelp camp and then I'm going to do a show collections and we should see campgrounds

123
00:08:33,840 --> 00:08:33,990
.

124
00:08:33,990 --> 00:08:34,920
There we go.

125
00:08:34,980 --> 00:08:41,380
And then if I want to just check how many camp grounds we have let's do D-B campgrounds that find.

126
00:08:42,210 --> 00:08:45,950
And I want to delete all three of them DVDs campground.

127
00:08:46,050 --> 00:08:47,840
Drop just like that.

128
00:08:48,090 --> 00:08:51,160
So this collection refers to the name of the collection.

129
00:08:51,360 --> 00:08:55,400
And if I hit Enter now tells me true which means that it worked.

130
00:08:55,620 --> 00:08:59,400
And if I try and find all the campgrounds all the campgrounds are gone.

131
00:08:59,780 --> 00:09:00,090
All right.

132
00:09:00,090 --> 00:09:03,860
So let's get out of here now and go back to her address.

133
00:09:04,110 --> 00:09:06,670
And we've now added in a description.

134
00:09:06,750 --> 00:09:10,040
So now let's create a new campground that has a description.

135
00:09:10,080 --> 00:09:16,220
So I'm just going to go with what I have here and comment that to create a new campground with name

136
00:09:16,230 --> 00:09:22,870
Grant Hill image this image and then description will be.

137
00:09:23,220 --> 00:09:36,340
This is a huge granite hill no bathrooms no water beautiful granite.

138
00:09:37,120 --> 00:09:37,620
OK.

139
00:09:37,620 --> 00:09:42,150
And I'm pretty sure the image is not of a hill but that doesn't matter in our case.

140
00:09:42,200 --> 00:09:43,660
So we're going to create a campground.

141
00:09:43,760 --> 00:09:49,060
And of course I could have done this in the browser and by going to the form and creating one.

142
00:09:49,070 --> 00:09:52,480
But I just want you to get a little more practice doing this through mongoose.

143
00:09:52,690 --> 00:09:54,400
OK so let's run the code now.

144
00:09:54,680 --> 00:09:56,420
All I need to do is run after you.

145
00:09:56,450 --> 00:09:59,980
Yes we get newly created campground.

146
00:10:00,140 --> 00:10:00,950
There we go.

147
00:10:01,230 --> 00:10:02,580
And it has a description.

148
00:10:02,820 --> 00:10:07,590
And if we wanted to open up Mongo and check on that and I'll do that very very quickly.

149
00:10:07,590 --> 00:10:15,320
So we'll use your camp and then we'll do D-B campgrounds up find.

150
00:10:15,890 --> 00:10:17,590
And there's our new campground.

151
00:10:17,820 --> 00:10:18,050
All right.

152
00:10:18,060 --> 00:10:20,290
So let's get out of there let's clear the terminal
