WEBVTT

00:00.000 --> 00:00.270
Okay.

00:00.300 --> 00:06.630
Our next task will be to delete an entity from our database or delete a resource from our database.

00:06.630 --> 00:08.670
So let's go back to VS code.

00:08.700 --> 00:10.410
Pretty straightforward this one.

00:10.410 --> 00:17.040
And what I would suggest if you're feeling confident at this stage of the course then you don't really

00:17.040 --> 00:19.320
need to watch me demonstrate this.

00:19.320 --> 00:23.730
I'm pretty sure you'd be able to cope with this based on what we've covered so far.

00:23.730 --> 00:29.040
I'm going to demonstrate it anyway, but feel free to pause the video and tackle this yourself, and

00:29.040 --> 00:34.980
then just compare your version of what you've done with the version I'm about to demonstrate, and I'll

00:34.980 --> 00:40.260
put a timestamp of where you can skip to, just to take a look at a completed code for this method that's

00:40.260 --> 00:41.820
going to come up in this lesson.

00:41.820 --> 00:44.010
But anyway, I'm going to demonstrate this now.

00:44.010 --> 00:49.950
So I'm going to use Authorize and roles as equal to admin again just as I've done before.

00:49.950 --> 00:55.680
And this is going to be an HTTP delete type of method.

00:55.680 --> 01:00.180
And we're going to take a parameter inside here inside quotes inside curly brackets.

01:00.180 --> 01:02.340
We're going to take the id parameter.

01:02.370 --> 01:06.210
Feel free to use it if you prefer inside here as well.

01:06.240 --> 01:11.880
And like I mentioned before, I don't really see much benefit in that, but it makes it clear what we're

01:11.880 --> 01:13.950
expecting I guess at least.

01:14.040 --> 01:21.760
So I'll use public async task and a delete method doesn't return anything apart from the HTTP response

01:21.760 --> 01:23.500
saying that the request was okay.

01:23.500 --> 01:30.250
So we'll use task of action result again without a type parameter.

01:30.250 --> 01:37.150
And we'll call this method delete products and we'll pass in the int and the id inside.

01:37.150 --> 01:39.040
Here we first need to get hold of our product.

01:39.040 --> 01:44.710
So I'll say our product equals await context dot products.

01:44.710 --> 01:48.730
And we'll use the find async method and pass in the id.

01:49.720 --> 01:52.150
We'll check to see if the product is equal to null.

01:52.420 --> 01:56.260
And if it is then we're simply going to return not found.

01:58.750 --> 02:04.240
And below this we'll use context dot products dot remove.

02:04.450 --> 02:06.370
And we'll pass in the product.

02:06.940 --> 02:16.630
And then we'll specify var results equals await context dot save changes async which is greater than

02:16.660 --> 02:17.590
zero.

02:17.590 --> 02:25.420
And if results equals true which we can effectively shorthand to if result then we're going to return

02:25.450 --> 02:25.840
okay.

02:25.870 --> 02:26.440
From this.

02:26.440 --> 02:32.170
That's the correct type of response to return for an HTTP delete method.

02:32.170 --> 02:40.240
And if it's not okay, we're just going to return a bad request and say problem deleting the product.

02:43.180 --> 02:46.150
And that's it for this method.

02:46.180 --> 02:49.630
So we'll just confirm that this functionality is working.

02:49.630 --> 02:52.270
So I'll just see if Hot Reload succeeded.

02:52.510 --> 02:53.560
And it said it did.

02:53.560 --> 02:55.840
And let's see if it really did.

02:55.840 --> 02:59.860
So I'll try and delete the product in the postman request here.

02:59.890 --> 03:05.950
I'll change 19 to 1002 because that's the product ID I have.

03:05.980 --> 03:08.170
I'll click send and we get the 200.

03:08.200 --> 03:10.690
Okay, so it worked.

03:10.720 --> 03:14.080
And if we try and get that product, we'd expect to see a not found this time.

03:14.080 --> 03:14.920
And we do.

03:14.950 --> 03:15.940
Perfect.

03:16.000 --> 03:17.950
So that's the delete operation.

03:17.950 --> 03:21.310
That's the code we need for this functionality.

03:21.310 --> 03:28.990
And what we'll take a look at next is because we have a picture URL property.

03:28.990 --> 03:35.020
We'll give our admins the ability to upload an image for a product as well.

03:35.020 --> 03:40.750
And that means we need to take a look at upload file and photo image functionality, and we'll start

03:40.750 --> 03:43.180
taking a look at that next.
