In this section.
We are going to work on the comments API.
So let's get into the controllers and command and here we go for comments.
You're going to have a couple of steps.
Remember, first is you want to find the post that we want to add the comment to.
And after that we are going to push the comment credit into the user's comment field.
And for this one, it's an optional If you want to have all the comments that a user has created, then
you can push the comment into the user's field.
So first step is that we need to protect the controller or the root.
So let's get into the root and comment root, which is this.
So let's require the protected.
All right.
So let's pass it as a second argument to this, as protected.
So let's get into the comments and let's have the roadmap.
First step is less the structure, what the user is sending to the server as red dot body.
For comment, we need let's have a look at the comment model, which is this We need the user and then
the message the user is trying to create.
So here is supposed to be message inside here.
So inside the try first is let's find the post, So let's bring in the post model and then the user
model.
So let's save to have the go to import.
So here I'm going to be the post model and then the user let me also receive it and going to be user.
And I have the two models.
So find the post is going to be caused post is equal to await post dart fine by ID and we can have the
id on the rec dot params dot ID.
Now this one is done.
The next step is create the comment and it goes like this cost.
The comment is equal to await the comment model less in the comment model also.
Sorry guys.
So here we go.
The comment model.
Perfect.
All right.
So comment and then dot create.
Then you pass in the things that we want to create.
First one going to be the user who is creating this comment and it can be found under rec the session.
Don't user off then the message.
And that is it.
So after creating, then you push to the post.
So I'm going to be as this remember, we have the post here, so it's going to be as post dot comments
and dot push and we are pushing the ID of the comment going to be as comments dot underscore ID.
The next step is that I want to push the common credit into the array of the users comments.
So if you look at the users model, we have a field called comments.
So in here we need to find the user.
And going to be as cost user is equal to a weight user model defined by ID and we have the ID inside
rec dot session dot user of.
And let's push the comment into user.
So you're going to be as a user comment dot push and I'm going to push the comment as that going to
be as comment dot underscore ID.
So last step is we need to the last step is we need to receive the post and then the user.
And for this, let's go ahead and disable the validation.
Otherwise mongoose, we say that we are trying to create a post by for post title is required or description
is required.
So let's disable the one and going to be as disable validation.
And let's save the two models and going to be as I wait and the post.
Don't save and you pass in an object and provide validation before save and force.
And let's copy this one for the user as well, when it be as user.
And that is it.
Lastly, let's send back the comment.
Let's go ahead and make the request to create a command.
So let's get into the tender client and let's look at the comments, which is this.
So for comments, we need to provide the ID of the post.
So we're going to be asked API version one for this last comment.
And let's have the ID of the post.
So let's get back to all posts and hit send.
I want to add comment to this post called CSS, so I'll copy this ID and here I'll provide the ID for
that.
So let me hit send and let's have a look where we got four or four, meaning that this root is not found.
So let's get back to the comment route and let's see which is this well, supposed to be column ID for
the great and that is it.
So let's go ahead and make the request.
Well, we still have the same error.
The problem is we need to remove this a slash to have only one for slash.
So let's hit send.
You can see we have this error, meaning that we are not passing in the data.
So here to create a command, we need to pass in the message.
And I'll call this one as good.
So let's hit send.
And we have it as that is supposed to be.
Good.
So let me change this one to good and all right.
So now we have the comments being created.
So let's get back to our post and let's fetch our post and let's look at the post for CSS, which is
this.
And now we have to comment being added.
And if I go to my profile page, I'll be able to see the comments that I have created.
So if I go to users and then the profile, let's hit send and let's have a look on the comment.
You can see I have created to comment.
So let's see how we can populate the comment upon fetching all posts in the next video.