So now all that remains in completing our RESTful API is to complete the last cell on our table which
is when a DELETE request comes in for a specific article
our server should delete that specific article from our database.
So I'm going to leave this up to you as a challenge.
So think about what we've done before and use the Express and Mongoose documentation to try and complete
this so that you can get it doing exactly what it needs to do. So pause the video now and try to complete
the challenge. All right.
So how did that go?
Well it shouldn't be too hard to figure out that we've used app.post, app.get, app.put. All we
have to do is just change that verb's name into lowercase and we've got app.delete which will do
exactly what we need it to do.
So converting that logic to our chained method we'll delete that semicolon that ends our PATCH and we're
going to extend our chain method to include the delete method as well.
And as always we're going to add our callback with our req and res.
So inside that callback we're going to be using the delete method out of the CRUD database actions.
And unlike previously when we coded up the DELETE request for the f/articles route where
we deleted everything from our articles collection,
in this case we actually only want to delete a specific article that matches the title that's in the
URL requested by the client.
So if we had a /articles/Jack%20Bauer then this method should only delete the article
that has the title of Jack Bauer.
So inside the Mongoose documentation if we head over to model and look down on the many things that
we can do to our model including deleteOne, then we come across something that looks like this.
So it takes some conditions and also a callback.
And this is an example method call that they've got over here.
So we're going to be using the deleteOne method providing the conditions for the particular document
that we want to delete out of our articles collection and then we're going to see if there were any
errors.
So inside our delete method we're going to call Article.deleteOne and the condition is going to
be the fact that it has a title that matches the req.params.articleTitle. And then we
have our callback which can give us an error if there was one and if there were no errors then we're
going to res.send that we have successfully deleted the corresponding article. But if there were some
errors then we're going to res.send those errors to the client. And now we can close everything off
with some semicolons and hit save and we're ready to test our final request.
So I'm going to change that from PATCH to DELETE and we're targeting the article that has a title of
Jack Bauer.
And at the moment you can see that our article on Jack Bauer is still here alive and well even though it's
got a slightly changed content. But once I go ahead and click send and I get my success message back
then if we refresh our list of articles you can see that it no longer exists.
So that completes our RESTful API creation.