Welcome back in this lesson we're continuing with the camp and in particular we're going to focus on
the user model that we just created.
And the comment model and associating the two so that when you create a new comment it's automatically
associated with the current user with your user ID and your username.
So I'll show you what I mean before I do that I'll highlight that I'm in V8 now.
So a new version we will be making pretty substantial changes to some of the models into the database
so it warrants a new version.
So V8 if you want to follow along.
And we're going to run the server to start and then I'm going to go to slash campgrounds and first I'll
sign up with a new account and this account will be potato and password will be password again.
Sign up.
Now let's take a look at Desert Mesa and go to add a new comment and we get this for in our goal is
to have this Arthur field go away so that we can automatically know the author's name and save that
to the new comment without the user having to manually specify it.
So we'll just have a comment be potato Taito potato and the other is potato.
All right.
Submit and receive this.
And we want to see this.
It should look exactly the same except the user shouldn't have to actually enter that username manually
.
To do that the first thing you need to do is on the comment model we need to add some fields in where
we can store the user's ID and the users name.
So let's stop the server and will open up the comment model so that models slash comment that yes and
inside of here under author it's no longer just going to be a string actually going to be an object
and an author will have two things in ID.
And then also a user name what we could actually do is store the entire author in there.
So it looks like this one we have a new user.
Remember the user account looks like user name potato and then it has underscore or ID some ID.
But there's also the hash which is that crazy long string and then the salt.
And we don't want to store all of that data inside of each comment.
Rather than doing that we'll just take the important pieces which are the ID of the user that created
the comment and the user name of that user.
We could just store the ID and then use that ID to look up the correct author and then grab the user
name from there.
But because we'll be printing out the name pretty often that's the whole point of this is that we loop
through and we print out each comment text but also the author.
It wouldn't be very efficient if we had to look up the correct author every time by taking an author
ID then finding that author and then finding it username.
So instead we're just going to store the data right here inside the comment which is something that
we could only do with a non-relational database like Mongo.
So Id is going to be another object as has a type which is mongoose that schema types that object id
which is just like we've done before.
If we take a look at the campground where we have comments where a type is Mangu schema types of KDE
and then we need a ref and ref refers to the model that we're going to refer to with this object id
which is a user and then user name will just be a string.
And we need to make sure we have our comma in here and save and we should be good to go before we move
on any further.
I'm actually going to stop the seeds in my database so I'm going to open the seeds file and I'm going
to comment out quite a bit of code for now.
So we will remove all but then I'm not going to create campgrounds.
We'll do it manually to start.
So let's make sure we come out the right code.
So we have CGP and campground out remove everything else we'll get rid of just commented out because
comments will now have a user ID and an author name or user name and our code and here doesn't account
for that.
So basically I just want a blank slate to start from.
So I'm not going to see the database but I will run it.
And what this will do is remove everything from our database and I'll just do that one time right now
and if I restart and then go back here I have to start the server again.
We should see there are no campgrounds there you go OK.
So now I will uncomment everything and save and then I'll go back to my apt Yes and just remove the
line where we're calling seed.
So I add a comment here seed database.
And for now and I'll actually do that on the same line.
See the database.
And for now we're going to comment that out we're not going to see the database.
We'll manually go in and add a new comment to a new post and create users.
Ok so now that we have that out of the way let's focus on the logic of associating the author and the
ID and username of that author with the comments.
So we need to go to the code where we're creating the comment which is instead of routes and instead
of comments.
Yes and it's right here.
The comments create in this code is responsible for first finding the correct campground.
Or using that ID in the route.
And then once we find that campground then creating a comment and then pushing that comment into the
campground then saving the campground what we'll do is before we actually push the comment into the
campground into the comments array we're going to add username and ID to comments and then we'll save
comment.
So it's actually pretty easy now that we have our data structure set up all we need to do is figure
out how we get the current users the user name and the current user's ID.
And fortunately we've seen that before it's request that user and we can count on there being a request
out user because if we get to this code the only way we get here is if the user is locked in.
And that's because we have this is locked in Middleware where if there isn't a user we'd redirect back
to slash log in.
So this ensures that someone is logged in.
So why don't we just do a simple cancel dot log.
And we want requests that user that username to start and let's do it like this new comment username
will be.
And just print that out request that user that username and let's start the server up and we will have
to go in and add a new post into our database.
The first thing I'll do is log in as potato with my password which is password and then that takes me
to slash campgrounds.
Now it's at a new campground.
They have a Creative Commons image that I'm just going to use here.
But feel free to use any image of course.
And then I'll add a name.
Let's just call it crazy insane you know full mountains.
And then for that description I have some more I'm ipsum that I'll paste in just some placeholder text
.
Let's click on submit.
Make new campground.
Now let's go to more info and try and add a comment.
You can see I'm signed in as potato.
Let's add a new comment.
Fill up some text here.
I'll just do more Lorem Ipsum some of that.
They're great.
And then I'll leave the author blank just to show you that we can still get potato even if I don't type
it here.
Then we click submit.
Now let's look at what was counted out loud.
So I will need to scroll up right here.
New comments you name will be potato.
Great.
So that's request.
Use your username.
And there's also request that user underscore ID and I won't go through Konsole like that again.
But trust me it's there.
We've seen it in previous videos request that user contained those two pieces of information I.D. and
the username.
So before we go any further let's go and remove this field this author field from the form so that's
inside of the comment new.
So rather than stopping my server I'll just use my mouse to find it.
So that's views comments new and we'll just get rid of this entire thing right here.
So we no longer have author.
It's just text.
And then we'll follow the text and hit submit.
And that will then send our data here we'll create a new comment.
To use request a user username and Dot I.D. to actually associate the user with the comment.
So to add the user name and ID to the comment.
Let's start with the ID just right comment that author ID equals request user dot underscore ID and
common dot author dot ID is because of the way our model was set up.
So a comet has an author and an author has an ID and a username.
So let's go back now comment.
Author ID request that user ID and comment author dot username equals requested user dot username just
like that and all save.
And then what we also have to do get rid of this line is actually save the comment so comment that save
.
And then we'll push it into the comments on the campground and then we'll save the whole campground
.
And then we'll redirect.
And why don't we before we do that let's just do a concert log comment so you can see what it looks
like at the end there before we redirect.
OK restart the server Now let's go back.
We'll have to
refresh the page.
It will have to log in again.
So potato password is password log in.
Let's go and add a new comment and the comment here will just be blah blah blah.
Submit.
All right.
Let's take a look at what was canceled out logged.
So it's hard to see because we have some other council that logs but it's rate here.
So we have text which is blah blah blah.
We have the common ID itself and then we have author which has an I.D. and a user name potato.
Great.
Now the last thing we want to do is actually display the user name here.
What we're doing now is displaying the entire user because all that we had was comment dot author and
this is on the show template.
So let's open up the show template for campground and I'll show you what I'm talking about down here
we're printing comment.
Author and that's referring to this entire thing.
It has an I.D. and a user name we want comment that author dot username.
So that's an easy fix dot user name here and save.
Now let's stop the server restarted and then we go back and can refresh the page and you'll see that
we have a potato here.
This first one we didn't create correctly where we didn't actually have that code set up yet.
So it has an empty author username but this one is working just fine.
Let's add one more under a different account and I'll call this one tomato and the password is also
password.
Sign up.
Let's go at a comment and this will just be HA HA HA.
Submit and there we go.
Automatically says the username tomato.
We didn't have to have the user enter that manually as always.
I'll end this video with a quick recap of everything we did.
We started by going to comment model and adding in an author field.
We already had author those string but adding an author is an object that has two properties ID and
username and ID is a reference to a user model ID and username is just the string name of the current
user.
So we set that up and then we need it to go into our comments route.
And it was pretty simple when it comments created we use requests that user ID and request that user
username and just plug those right in to comment author.
And then we saved the comment and that's all we had to do.
And the very last thing was making sure we displayed a comment that authored user name not just comment
that author.
Awesome.
So this is really starting to come together.
If you take a look at how far we've come we still have a ways to go with some of the styling.
We have a little bit more logic to do but we've really covered a lot so far with the camp