So did you succeed?
Let's implement, updating and deleting together.
Now for that,
I'll start with updating and therefore
this posts edit route.
What we're doing in there in the end,
is we are calling this updateOne method
and we're selecting a post by postId,
and then we're setting the updated title and content.
So I will grab this part here of the code
and go to my model.
And now in there we could add a new method,
an async method called update, for example.
But actually we could also reuse the safe method here,
and in there, we can use the fact
that the ID won't always be set.
And check if this thought ID is truthy,
so it's not undefined.
It is set instead.
Then this means that we created this post
with an existing ID.
And when we call save on a post that already has an ID,
we're definitely not creating a new post
because when we do that, we don't have an ID yet.
We only have IDs on posts that were already stored
in the database before.
So when we call, save on such an existing post,
we must be doing that because we wanna update
that post instead of creating it.
So therefore if we have an ID here in the save method,
I'll execute this update code and only else.
If we don't have an ID, I know that I'm creating a post.
So then I'll do it like that.
Now either way we'll get a result.
So I will actually create result as a standalone variable
and set result equal here in the update case,
and set it equal here, not as a new constant,
but instead as a value for this variable.
So that either way in the end,
we can return the result if we need that.
So that's how we can utilize this ID for updating.
And that also means that here for finding the correct post,
we can use this ID,
assuming that the ID that is stored
in this post object based on this class,
has the right format.
And to ensure this,
we should go back to the post constructor.
Instead of assigning the ID like that here,
we should check if ID is set.
If we're not getting an ID,
I don't wanna do anything.
But if I do get an ID,
then we probably get it as a raw string
and we wanna transform it to this ObjectId thing.
So then here,
I wanna set this ID equal to new ObjectId
just as I'm doing it here in blog.js,
when I transform the post ID I'm getting into an ObjectId.
Therefore, just as in blog.js,
we wanna get the ObjectId like this
by importing mongodb, and using the ObjectId like that.
And therefore in the post.js file, at the very top,
I will import mongodb by requiring mongodb.
And then once we imported all those things,
I will get my ObjectId Constructor,
my ObjectId class by accessing it
on the imported mongodb package like that.
And then here we can indeed create a new ObjectId
and pass the ID, which we got as a parameter value
into this constructor.
And with that, we'll set the ID only if we get one
and we therefore only have one down there
if we did set one when we created the post before.
This also means that the transformation
from string to ObjectId, now happens in the model
and hence back in blog.js,
we can actually go to this update post route.
So to this route and get rid of this line of code here.
And instead after checking that the input is correct
before we redirect,
we now create our post based on that model.
So we create a new post here based on this post class,
but now we create it by passing in the enteredTitle
and the enteredContent.
And we now also do pass in an ID here
because here we wanna create a post object
for a post that already exists in the database.
And the ID here is req.params.id
because we're using the ID, which we extract
from the URL path.
And then again, we can call post.save here
to save that and await this here.
Since save actually is a async function,
it will therefore automatically return a promise.
All functions decorated with async, do return promises.
So therefore we can await this here to wait
until it was saved before we redirect.
And actually that's the wrong place I just noticed.
I, of course, wanna do that down here.
Here, I was in the place where I was checking for errors.
I wanna do that after I checked this.
Now with that, I also implemented the updating logic
with help of a model.
And hence, if we now try that again,
and we go to the admin page and I try updating
this first post, this crashes, and let's see why.
If I expand this, enteredTitle is not the defined,
that's of course, my fault.
I have an error in that save logic.
When I set the data for updating the post,
this should not be enteredTitle and enteredContent.
instead we're inside of the post class.
So this is this.title and this here is this.content,
referring to the title and content that we do set up
when we instantiate this post class.
Now with that, if we saved that,
if I do click save again here, this was updated like this.
So that worked.
Let's now implement the deletion logic as well.
And for this, I will go to the delete route, this one.
And it's now this line of code that we
wanna outsource in the end.
In post.js, I will add a new method
because now it's not the save method,
instead it's a new async method,
which could be called delete because that's what we'll do.
And in there, I wanna execute this logic.
Now for the ID, I will use this.id,
assuming that we are calling delete on a post object
that did get an ID when it was instantiated.
And therefore, we might wanna check if not this thought ID,
if this is not set, in that case we know we have no ID,
so deleting won't work.
And in that case, we could throw a custom error
with the throw keyword, which I also mentioned before,
or we just return and don't do anything.
Either way, we wanna make sure that this line
of code doesn't execute because this relies
on the existence of an ID.
And therefore it won't work if we don't have one.
But with that, we should now be able to call delete
to delete a post.
We also get back a result here,
which we can return in case we should be interested
in that in the place where we called the delete method
and with all of that, back in blog.js,
here in the delete route,
I will just create my post by instantiating it.
And now here, I'm not really interested in the title
and the content.
Instead here, I'm really just interested in the ID.
What we can do in this case is we can pass in empty strings
for title and content and just pass in req.params.id.
Alternatively, we pass null as values for title and content
to make it clear that here I don't have any title
or content because I'm not creating this instance
of the post class to work on that existing post.
Instead, I'm creating it like this, just to delete it.
So here we can then call post.delete and await that.
And that should do the trick.
If we now save all of that,
and I reload here and I delete,
let's say the last post here, that seems to work.
If I reload, it's still gone.
I also have no error here. So that did the trick.
And that is how we can delete a post as well.