Now that we tested our first two endpoints,
it's time to go back to the Todo's controller
and add the logic for the updateTodo
and deleteTodo actions here.
In updateTodo, I expect to get a Todo ID
and it will be part of the params here of my path
because that's the idea.
If you have a look back at the slide I showed you earlier,
the idea for the patch and the delete routes is
that the ID of the Todo with which we're working
is part of the path of the incoming request.
And you learned that you can extract
such dynamic path parameters with req.params.
That's a built in Express feature.
And then, of course, we also need the new text,
the newTodoText, which we get by accessing request body
and then maybe, again, text or newText.
That will be up to us
because we are the developer of the API.
And in the end, we just have to make sure
that it's then used appropriately,
either because we are the one using it anyways,
and then of course we know which data it wants,
or if we expose it to the public,
if we sell it to customers,
by providing proper documentation
that explains how to use this API.
So here I expect to get a new text field
on the incoming request body.
Now with that, we can then create the Todo
that should be updated by using new Todo
and to new Todo, we then pass our new Todo text
and Todo ID.
That's how we configured our model to work.
Now that we got an ID here,
if we now call save in the Todo model,
we will update the existing Todo instead
of creating a new one.
That's the logic we wrote before.
Hence here we can now call Todo save.
And, of course, this returns a promise.
So again, we want to await this down here.
And since it could fail of course,
as always, we also want to wrap this with,
try-catch here and catch any errors that we might be getting
so that ultimately here we can return next error.
But if we make it past this try-catch block,
we know that it worked.
And then we can send back a confirmation response where we
just have a message of Todo updated.
And maybe we also include the updated Todo,
which is that Todo I created here.
It's the same Todo because that already has the new text
and the Todo ID.
So that should do the trick for updating.
Now for deleting, it's quite similar.
I will, actually, copy the code from updating.
But here, of course, when we delete the Todo,
we don't get a Todo ID text as part of our body.
So I'll delete that.
And, therefore, I don't have a Todo text
for initializing a Todo.
I'll set it to null therefore, because for deleting
in the Todo model I, in the end, only care
about the ID anyways and I need that ID for deleting.
So just passing in an ID here is enough.
And then here we can call Todo delete,
which of course still returns a promise here.
That's how we built this delete method.
And therefore we, of course, still try-catch this.
But then, in the end, we can say Todo deleted here
and return nothing else.
Now, with that, I'm getting an error down here as I see,
let's see.
Yeah, I'm using a weight in a function
that doesn't have asynch on it.
Of course we should add async here in front of delete Todo,
and now that is fixed.
So with that, we added these two functions
or the code for these functions.
Now in Todo's routes, we need to add the routes.
So we want to have a patch route
for slash colon ID here.
Slash Todos is automatically added in front of that
but now we need that dynamic path parameter.
And then here we point at the Todo's controller
and there add update Todo.
And then below that we have a last route,
which is our delete route for slash colon ID here
with that dynamic parameter and we point
at delete Todo here.
So these are the finished routes now.
And with that, we should be able to also update
and delete Todos.
To test this, I will again use postman.
And I'll, first of all, add a second Todo,
which is test all routes.
So I'm using the post request again in this first tab
with my updated text here in the body,
and I'll click send.
This worked.
If I now get all Todos, I should have two Todos, and I do.
And now I can add a new request,
where I send a patch request
to local host three thousand slash Todos.
And here I want to update a Todo. Therefore, actually in
the path, we need to include the ID of the Todo that should
be updated because that is how we configured our route.
And, therefore, I'll grab one of these IDs.
Let's say the first one, doesn't matter.
By the way I just see I do have an error here, of course.
Didn't notice this before.
Text and ID is swapped here.
Yeah, that's not the idea.
We'll have to fix that
because in Todo model and get all Todos,
I'm of course passing the data into the constructor
in the wrong order.
So, thankfully, it's not stored incorrectly in the database.
I did just map it incorrectly here.
But if we fix this here and I send my get request again,
now this is correct.
So, now again, let's pick that ID here off that first Todo
and then we can add this here in the path
of the patch request.
And then the patch request needs a body because
in the Todo's controller for updating a Todo,
I'm extracting the new text from the body.
So we have to make sure that this field is present
in the request.
And then for here in the body, I'll again
choose raw and Jcon, and add my basic Jcon body here.
Where I do now add this new text field between double
quotes and then I add my new text,
which could just say updated.
If I click send, this seems to work.
I got my updated Todo here.
And if I get all Todos again,
I see it was updated here as well.
Now let's also see if deleting works.
For this one we'll again, pick this ID maybe, again,
off this first Todo and here I will then delete.
Actually, I'll pick the ID off the second Todo,
though it doesn't matter.
But then here, for delete, I send a delete request here
to local host three thousand slash Todos slash this ID.
Nothing else, no bodies required, no special headers.
And hence, if I now click send, I get Todo deleted here.
And if I get all my Todo's again, it indeed is gone.
So now all these current operations work
with help of our web API here.
And because it is an API,
it's all about exchanging data here.