In this video, let's implement delete functionality.
Let's change the comments to delete posts.
Let's try the endpoint and make sure that it's working.
And this is the delete.
Let me hit send and now I have the root.
Been working perfectly.
So what are the steps?
Well, the steps are going to be pretty simple as the way we create a post.
So first is we need to find the ID of the post that we want to delete.
So here we go.
So first is find the OR, get the ID, get the ID.
And as we did before, the ID can be found under rec dot params dot ID.
Then after that we are going to find the post by ID inside the post data.
So now inside the root here, let's filter out all the posts with a specific id so accounts.
And now call this one as filtered post.
And is equal to the posts array.
On that we have a method called floater and for this function we pass in a callback function.
Let me use normal function here.
So inside a function body we can go ahead and implement the logic.
So here we can retain a post dot id maybe guessing where is the ID?
If you can still log the posts inside the callback function, we're going to have the individual post,
so let's check it out and see back to Postman Hit St.
And now we see that these are the individual posts and we have an ID.
So back to the root here for the returning if the ID is.
Equal to the ID in the params, then it means that it's been found.
So let's go ahead and then console that log the filtered posts favorited posts as that.
This one down here.
Oh, it must be in the root server.
I always make mistake.
So now let's go ahead and make the request and see hit st.
And now we see empty array because the ID is not found.
So let's use ID that exists.
Let's see one.
Let me cancel the request and send again.
Now indeed I have the post that has been fetched, but what you want to do is that you want to display
all posts, which is not this.
So there is implementation.
So here instead of this I'm going to make use of it's not ID.
Meaning that you won't find all posts which is not equal to the ID in the params.
So now with this one, if we send again now I'm going to see array of posts.
You can see that we have all the posts with exception of the post with ID of one meaning that we have
filtered out that.
So now that we get this one, we can go ahead and then re update the file, meaning rewrite the content
onto the file.
So here we go.
As we did for creating, we can copy the same functionality and bring it here.
So let's look at create post here, which is this and we want this function that saves a content onto
a file.
So let me collapse this one and then copy this one and now I'm back to delete down here.
Let me paste it now instead of post data here we are passing in the filtered post and that is it.
And that is it.
So here is our.
I start to two.
So now we are done with it.
So let me try again and hit St and Nancy that post created but instead is post deleted.
So here post deleted the message.
But if you check the file, we see that we don't have that post.
Now we have two posts.
Let's say I want to delete this post, I'll provide the of 2022.
So here we go and provide 2020 to 2020.
Sorry 2020.
So hit st and now post deleted.
You see that.
Let me check the file and now we have one post.
And if I make a request to fetch a post, I have one post.
So this is how we can work.
Delete a post.
Let me add more post in the mix by creating this now post credit.
If I check the file now I have to post the last CRUD operation is what we call update.
So let's see how we can update the post inside our route.