In this video, we are going to create a controller to update a comment.
Again, the concept is going to be the same for updating a post.
So let's get into post and let's copy the logic.
So this is the update.
So inside the try, I'm going to copy the entire logic as that.
And back to the command controller, which is this and let's collapse this and here is the implementation
I'm going to paste.
The one I copied.
The first step is find the comment.
So let me change this one to find the comment.
Likewise this.
And then check if the comment belongs to the user.
All right.
So the same concept here, we are going to find the comment by ID.
So here is going to be the comment model and here is going to be comment.
And on the comment, we have the user and then we are comparing to the look in user, which is this.
The message would be you are not allowed to update this comment.
And if everything goes right, then you go ahead and update the comment.
And here is going to be as the comments update updated.
All right.
And let's send that.
For this one.
We are not uploading any image, so let's remove this and let's remove the category.
What we want is a message.
So going to be the message and the value can be found on the rec, the body, the message.
All right, that is it, guys.
Again, we need to put this one behind authentication.
So let me send the error here in case we have one.
So next and pass in the app error and then the actual message, could it be error message?
So let's pass next into this.
Likewise the delete.
Let's go to pass next into that and inside here pass next and then the app error and error message or
we can we pass in the entire error so let's remove this one to make it as error.
All right.
So that is it.
What about the fetching single?
Let's also pass in the next and then here, let's send the error by calling next, then pass in the
app error and then the message, the error into that.
All right.
What about the details?
Let's also pass the next here and let's copy this one going to be the same concept.
Okay.
So moment of truth.
Let's go ahead and update a comment.
So let's create a new comment with the same value as good.
Let's copy this ID and click on Update and let's provide the ID again, let's put the end points behind
authentication for update, which is this.
We're going to be as protected.
So let's pass in the message as that and now change this one to the best and hit send.
Well, I got an error, so let's have a look.
Well, app error is not defined.
Meaning less require the app error inside the controller of comment.
So here const app error is equal to required.
And inside the utils then app error.
All right, so let's hit send.
Well, you got this error meaning that cannot read properly of user list.
Locate which line of code comments.
Line number 92.
So command, line number 92 for update, which is this This error was called meaning a kind of find
the user on the comment.
So what is the problem?
Let's console.log the comment.
So let's see what is inside the comments.
So let's click on send.
Let's check the terminal.
And we got undefined meaning that there is no comment being found so we can even make a check.
If there is no comment, then we can throw an error.
Right.
So here before we go ahead and then update, we are going to check if there is no comment, then we
are going to return and call next, then pass in the app error the message by saying that comment not
found.
All right, So with this one, we can get beautiful message as comment not found.
So let's check the route for comment and let's see if you have the ID.
Yes, we have it as that.
So let's go ahead and then create a new comment.
So let me change this one to excellent and hit send.
Now we have a comment being created so our copy the ID of this comment and I'll provide the ID for the
update and hit send.
And it has been updated successfully.
All right, guys.
So here ends the entire API for the blog application and you can improve this API if you want.
What has left is how we can connect the API to the front end.
So let's get into that in the next video.