Hi everyone.
Let's continue with CRUD operations.
And in this video will be the update action.
Okay.
Now let me remove DTO here to fix the warning.
And now go to the post controller and I will create a method called @Put.
Okay.
The @Put will be for update.
And because we update we need to know what is the ID of the post we want to update.
Right.
And we will update with @Param('id) id: string
And now because we update we need the payload or the request body.
And for that reason I will use the annotation @Body right here.
And request body.
But what is inside the request body right here.
Now if you see in the post of the create.
We have a createPostDTO right here.
So for that reason, we need.
We need the post.
The UpdatePostDTO
Right.
So I will create a new file inside the DTO, update-post.dto.ts
And actually right here we will export the class.
Actually we use the same with the CreatePostDTO
I will copy all of that because I'm very lazy.
So I'll use the UpdatePostDTO
Okay?
And that what we want
It will be the same, but.
We need to separate it.
Okay?
Make sure we separate it.
And right here I will use the UpdatePostDTO
The reason we separate is because later if you want to add some validation only for the updates,
we can easily add it right here and now.
Let me go to the service and scroll down and call async update
Give the ID string.
And the request body.
We will UpdatePostDTO like that.
Very cool.
Now how I can update.
Actually, in order to update we can use the find.
findByIdAndUpdate or findOneAndUpdate
And update it will be the same.
And the second way we will use, we will update it by hand.
So look up the get one and update.
Let me show you the second way.
Okay.
I will find the post.
= await this.
getOne
Okay.
I will get the post.
Inside the method.
Right here.
We already write it before, right?
And for that reason, I can give you the post.title = requestBody.title and post.description
= requestBody.description
And I will return.
I will return post.save()
Okay.
And that's it, that for the update
And I will go to the controller right here.
And I will return this.
postService
.update(id, requestBody), okay, that's it
And let me show you how it works, in here I will duplicate and I will change to the PUT method, right?
and will be update post.
We have a ID so we will pass the ID right here.
The title
Will be "post 1 update"and description
Will be "description 1"
Update
Okay, let me send.
Uh, actually it's not found
Let me go to the database.
I will be here and I will paste it right here.
I will send it again.
Uh, why it not found?
This I.
Go.
We use the @Put
And the :id, oh sorry.
We make a mistake right here.
Okay, you typo correctly, will be :id
Okay, let me send.
Let me send.
And now you can say this already updated for me the title and description.
Right.
And that's What we want.
But for example, if I remove the description and I send the update. Let's see what happens.
If I send, now it say the description should not be empty.
The reason because I used is not empty right here.
It means we will pass the description and the title.
So how I can only update the title or only update the description?
I want to do that.
How I can do that right.
And actually this is not the job of the @Put method.
With the PUT method we will update all fields
And in the next video I will show you how we can use the PATCH method.
The PATCH methods will be update one fields.
Okay.
The job of the PATCH and I will show you in the next one.