WEBVTT 0 00:00.480 --> 00:09.390 So now all that remains in completing our RESTful API is to complete the last cell on our table which 1 00:09.390 --> 00:14.130 is when a DELETE request comes in for a specific article 2 00:14.130 --> 00:19.290 our server should delete that specific article from our database. 3 00:19.290 --> 00:23.270 So I'm going to leave this up to you as a challenge. 4 00:23.310 --> 00:31.320 So think about what we've done before and use the Express and Mongoose documentation to try and complete 5 00:31.320 --> 00:36.990 this so that you can get it doing exactly what it needs to do. So pause the video now and try to complete 6 00:37.020 --> 00:40.510 the challenge. All right. 7 00:40.510 --> 00:41.700 So how did that go? 8 00:41.800 --> 00:47.650 Well it shouldn't be too hard to figure out that we've used app.post, app.get, app.put. All we 9 00:47.650 --> 00:54.220 have to do is just change that verb's name into lowercase and we've got app.delete which will do 10 00:54.250 --> 00:55.880 exactly what we need it to do. 11 00:55.930 --> 01:03.130 So converting that logic to our chained method we'll delete that semicolon that ends our PATCH and we're 12 01:03.130 --> 01:08.350 going to extend our chain method to include the delete method as well. 13 01:08.350 --> 01:12.990 And as always we're going to add our callback with our req and res. 14 01:13.420 --> 01:20.620 So inside that callback we're going to be using the delete method out of the CRUD database actions. 15 01:20.800 --> 01:28.780 And unlike previously when we coded up the DELETE request for the f/articles route where 16 01:28.780 --> 01:32.200 we deleted everything from our articles collection, 17 01:32.200 --> 01:40.450 in this case we actually only want to delete a specific article that matches the title that's in the 18 01:40.450 --> 01:42.220 URL requested by the client. 19 01:42.640 --> 01:50.020 So if we had a /articles/Jack%20Bauer then this method should only delete the article 20 01:50.070 --> 01:52.180 that has the title of Jack Bauer. 21 01:52.450 --> 02:00.100 So inside the Mongoose documentation if we head over to model and look down on the many things that 22 02:00.100 --> 02:07.430 we can do to our model including deleteOne, then we come across something that looks like this. 23 02:07.480 --> 02:11.400 So it takes some conditions and also a callback. 24 02:11.440 --> 02:15.210 And this is an example method call that they've got over here. 25 02:15.250 --> 02:23.020 So we're going to be using the deleteOne method providing the conditions for the particular document 26 02:23.050 --> 02:27.730 that we want to delete out of our articles collection and then we're going to see if there were any 27 02:27.730 --> 02:28.490 errors. 28 02:28.540 --> 02:39.850 So inside our delete method we're going to call Article.deleteOne and the condition is going to 29 02:39.850 --> 02:50.190 be the fact that it has a title that matches the req.params.articleTitle. And then we 30 02:50.190 --> 02:59.340 have our callback which can give us an error if there was one and if there were no errors then we're 31 02:59.340 --> 03:11.940 going to res.send that we have successfully deleted the corresponding article. But if there were some 32 03:11.940 --> 03:19.740 errors then we're going to res.send those errors to the client. And now we can close everything off 33 03:19.800 --> 03:27.610 with some semicolons and hit save and we're ready to test our final request. 34 03:27.690 --> 03:34.740 So I'm going to change that from PATCH to DELETE and we're targeting the article that has a title of 35 03:34.740 --> 03:36.150 Jack Bauer. 36 03:36.150 --> 03:43.710 And at the moment you can see that our article on Jack Bauer is still here alive and well even though it's 37 03:43.710 --> 03:51.090 got a slightly changed content. But once I go ahead and click send and I get my success message back 38 03:51.450 --> 03:58.950 then if we refresh our list of articles you can see that it no longer exists. 39 03:58.950 --> 04:03.780 So that completes our RESTful API creation.