In this video, we are going to create a controller to update a post.
So inside the post controller, which is this one, let's collapse the finished one and we have the
last one here.
The first step is that we need to find the post that we want to update.
So I'm going to add a comment to this place as that and I'm going to structure the field.
So I'm going to be the title, the description, the category, and that is what we need and which is
equal to the red dot body.
The logic would be the same for the list controller.
First of all, we are going to find the post and I can copy this one and even this one as well, because
you will check if the post belongs to the user who is trying to update.
So let me pass it here as we did before.
So the first step is that we are finding the post by ID and on that we can check if the look in user
is the owner of the post.
Then after that we can go ahead and then update the post here as that const post updated is equal to
a weight and post dot.
Find by ID and update and the id can be found on the red dot params dot ID.
Then the properties that I want to update.
I want the user to update the title description and then the category.
And then the image as well as I provide the image here.
And remember, we can have the image on red dot file dot path.
So let's go ahead and check if a user is trying to send an empty data.
So the same way we did for the create are going to use the same concept here.
So I'll copy this one.
So inside the updates, I'm going to place it here.
So here we are checking for empty data.
So now that we have it, let's go ahead and send back the response as data.
And then the post are data.
So let's go ahead and then make the requests.
Let's find a post.
Let's say I want to update the first one for HTML, so I'll copy that and for update our provider ID.
And remember, we are passing an image, so we need to pass more to as a murderer to the update route.
So here we go for the update.
First of all, we add the protected and then the mortar package as upload dot single and we pass in
the file and comma separated.
So let's go ahead and make the request So under body.
Click on form and click on files.
The field name.
I want to update the title only and I would change this one to CSS.
And then for the file it's going to be the file.
And then the fire to update.
So I would choose this one.
And I think the chicken is not ideal because a user can choose to update the title or description.
So let's remove the chicken here.
And then we need to pass additional configuration to the update.
So here, come on, object and name to true meaning that we want to have the updated data or record.
So now let's hit send and let's see.
You can see that it has been updated to CCIS and you can see the image has been updated.
If you copy the image and open a browser, you can see that we have newly updated image.
So let's say that I want to update a post belong to somebody else.
So let me make a request to find all posts and I want to find the main stock post.
So down here, which is this and I'll copy the ID, so let me paste the ID here and let me hit send.
You can see I go back and empty object, meaning that our error is not being caught.
The problem is we need to pass in next year and that is it.
The next one is let's change this text by saying that you are not allowed to update this post.
So now let's make the requests and we have the response back.
And you can see you are not allowed to update this post.
All right, guys.
So here ends the API for post the next section.
We are going to work on the comments.