So, let's make sure posts are deletable.
For this we got the delete button here
in the post item EJS file.
And it's a button, not a link.
We could convert it into a link
and point at some path
where we then handle GIT requests
to that path to delete that post.
That would work and you could implement it like this.
But deleting data actually impacts the database, of course.
It's a little bit like storing data,
of course the opposite
but we change something in the database.
Hence in my opinion,
a post request is a little bit better to use here.
Again, you could delete the post
both with a GIT and a post request.
This does not impact what you can do on the server.
It's just about which message you wanna send.
Post typically implies that things change on the server.
GIT implies that you just, well get data.
That's why I would like to send a post request here,
but how can we do that?
We can't add a link to the button like this.
Well, we can wrap the button
in a form as we did it
before in the course as well.
Of course here, the users shouldn't enter
anything into fields
but by wrapping the button of form,
now we can attach a link to the button
by adding an action to the form
because the button will submit that form.
And here we could point at posts.
Then the idea of that post,
which is post._id,
as used down there,
/delete for example.
Now this kind of request would be sent.
We can set method to post
to ensure that a post request will be sent.
And that is how we could ensure
that whenever the button is clicked here,
we do a send such a post request to this path.
And hence, now we can register a post route
for this is path in our blog JS file
to then implement the deletion logic there.
And that's what I'll do.
In blog JS, at the bottom let's say,
we can register a new route for /posts,
/:id,
/delete
and then execute our deletion logic
in this route handling function here.
Of course, since we'll talk to a database,
we can already add async,
since we'll perform some async operations.
And then as a first step, we can again,
extract our post ID,
since we'll need that for identifying the post
that should be deleted.
So we can get our post ID like this here
and now we can get hold of our database here.
Then they're reach out to the posts collection
and then run the delete one method
on that collection to delete one post.
And just like update one or find one we can
and we should now pass in a parameter value
that defines how the posts that should be deleted,
should be identified.
And here I wanna look for the post
where _id is equal to the postId I created here.
And that should be all.
That should delete the post with that ID.
So we can await this.
We also would be getting back a result here
with more information about the deletion process.
If we're interested in that.
You can log into the console if you wanna look into it.
Here, I'll ignore it.
And instead I'll just redirect the user
back to /posts here,
once the deletion was executed.
That should be all.
If I now save that and I reload here
and I click delete post,
it's deleted and hence I see the no posts message.
And of course, now we can, again, add more posts,
a new post,
this works for real,
and maybe attach that to manual
and we see it here.
But deleting, as you saw,
also works.