Welcome back in this lesson we're going to talk a bit more about this concept of rest and what that
is and what restful routes are.
And we're going to review the restful route we've seen and then we're going to add another route in
.
That's called this show page or this show route.
So we have four main things reviewing the restful routes we've seen so far.
We're going to change the campground schema and add a description to each campground when to show you
a quick Mongo command and then we're going to spend the bulk of our time on creating a show route and
a template showed that each of us.
Let's begin by reviewing restful routes.
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
you want to follow along using the code.
All right.
So.
Yes let's take a look at what we have.
We have two main routes apt get slash campground act up post slash campground and we have one more apt
get slash campground slash new and those three routes follow what's known as restful routes and there
are seven of them and the first one that we're going to talk about is known as the index route.
And we're going to do this as an example for dogs again.
So slash dog should be the you know as a get request and what it does or what it's supposed to do is
display a list of all dogs.
So let me make this a little bit of a table here.
It's going to be pretty rudimentary.
Don't want to spend too much time on it.
And our headers are just going to be the name of the route and then we'll have the URL or the path and
then we'll have the verb in this case you get and then we have the description of what it does.
OK.
So we have the index route which for now what we have is slash campgrounds which is going to show a
list of all campgrounds which is what it does.
Then the next one that we have talked about is called new and new is not what you think it is.
It's not actually the post route that makes the new dog or the new campground.
It's slash dog slash new.
And it's the route that actually shows the form.
So let me space this out a little more.
There we go.
And it to get route.
And it displays form to make a new dog and then that form submits to what's known as create which is
flash dogs the same exact as the indexers same Yoro except it's a post and this is supposed to add new
dog to database.
You might be wondering why does this exist.
Why do we need to follow this pattern.
The first answer is that it's always good to have some structure just for our own purposes but also
this pattern is something that lots and lots of applications will follow.
And it makes it reliable and predictable for us to interact with other applications.
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
can do API get hub dot com slash users and that will give us a list of all the users that the users
index.
So we'll come back to that.
But again these are the three we've seen so index is right here and I'll go label these.
This is our index route
just right there show all campgrounds.
This is our create route add new campground to database and this route is new which is show form to
create new campgrounds.
Remember we have to have two routes in order to send a post request.
We need one to show the form and then we need that form to submit somewhere which is our create route
.
OK.
So those are the three routes we've done so far.
We're going to introduce another route right now which is called show.
So imagine that we have our apps it up like this which you don't have to imagine we do have this set
up.
And if I wanted to be able to click on one of these and view more information and that's what we're
going to do we want to be able to have a button here where I can view information like a map the different
reviews the comments the ratings.
Any other images or whatever other information I want to show that doesn't make sense to put right here
which normally there's a lot to show.
And we only show one or two things on the index page like a photo and the title just like on Reddit
which I know I've been using a lot as an example.
But if this is our index this is showing all of the posts that we have we're not going to show every
single piece of information at once.
It just shows that title shows the link the number of comments the time it was submitted.
The user and then we can click to view the comments on a separate page and there's tons and tons of
information to show here.
But this isn't all just shown right away on the index page so what we're going to do is make another
route which is called show and the URL looks like this slash Doug's slash ID as a variable and it's
a GET request and it shows info about one dog.
And the reason we put the ID there is that we need to know which dog we're showing because up until
this point index always works the same way.
It shows you all dogs no matter what.
This always shows you a form.
This always ends in a new dog.
But show is a little different because now we need to show you information about one thing in particular
.
So to do that.
Not sure how to open this up accidentally to do that.
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
and it's a GET request don't get slash campground slash colon ID and then we'll just add the basic thing
here are basic callback with request and response.
And then in here we'll do a redstart send.
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
out any you are all that follows.
Campground slash ID any ID we get this will be the show page one day if we go to campgrounds slash new
we still get the form and thats because the order of the route is correct.
Remember if I switch These all that this pattern is saying is campground slash anything any single word
which is technically what we have here campground slash new.
So we need to be careful that this is declared first.
Otherwise it will treat campgrounds slash new as a campground slash ID route and it will show us this
.
Okay so thats a minor note but its important what we'll want to do in here is find the campground with
provided ID and then render show template with that campground and all set.
So the show page is supposed to show more information about one item.
Right now we're showing all the information that we have on the index page.
We don't have anything else to show.
I guess we have an idea technically but that's not really useful.
So we're going to add a description to our campground and that description will just be a little bit
of text that we can enter in when we create a new campground.
So we'll do that now and we'll go up here to our schema and just add in description and we started out
as a string and see if we have a small problem which is that the campgrounds that we have already there's
three of them don't have a description.
So we can either update everything in our database to have a description or we can be a little bit more
destructive and just delete everything in a database with what's known as DBI data collection drop and
we'll drop all the campgrounds which is what I'm going to do here.
It is something that you do occasionally when you make some big changes to your data and you have some
pre-existing data that you don't really care about.
You would never really drop users stayed on purpose.
But in this case it's just our starter our sea data.
So I'm going to drop it.
So I'm going to open up Mongo.
I'm going to connect to the right database.
Let's see.
Show DVRs.
We're going to use Yelp camp and then I'm going to do a show collections and we should see campgrounds
.
There we go.
And then if I want to just check how many camp grounds we have let's do D-B campgrounds that find.
And I want to delete all three of them DVDs campground.
Drop just like that.
So this collection refers to the name of the collection.
And if I hit Enter now tells me true which means that it worked.
And if I try and find all the campgrounds all the campgrounds are gone.
All right.
So let's get out of here now and go back to her address.
And we've now added in a description.
So now let's create a new campground that has a description.
So I'm just going to go with what I have here and comment that to create a new campground with name
Grant Hill image this image and then description will be.
This is a huge granite hill no bathrooms no water beautiful granite.
OK.
And I'm pretty sure the image is not of a hill but that doesn't matter in our case.
So we're going to create a campground.
And of course I could have done this in the browser and by going to the form and creating one.
But I just want you to get a little more practice doing this through mongoose.
OK so let's run the code now.
All I need to do is run after you.
Yes we get newly created campground.
There we go.
And it has a description.
And if we wanted to open up Mongo and check on that and I'll do that very very quickly.
So we'll use your camp and then we'll do D-B campgrounds up find.
And there's our new campground.
All right.
So let's get out of there let's clear the terminal