WEBVTT

00:00.000 --> 00:04.110
Okay, let's next take a look at editing a product in this lesson.

00:04.110 --> 00:06.060
And we'll head back to VS code.

00:06.060 --> 00:10.620
And we're going to create a DTO for editing of a product.

00:10.620 --> 00:13.410
So I'll just close all of this stuff down for the time being.

00:13.410 --> 00:18.810
We'll go to our DTO folder and we'll create a new file that's going to be a class.

00:18.810 --> 00:22.560
And we'll call it Update Product DTO.

00:23.040 --> 00:25.680
And what do we need inside here for the properties?

00:25.680 --> 00:28.020
Well, we need all of this effectively.

00:28.020 --> 00:32.040
And we can also include the ID with this as well.

00:32.040 --> 00:36.450
So to save a bit of time and duplication we can come to our update product DTA.

00:36.450 --> 00:40.170
And we can just derive from our create product DTO.

00:40.170 --> 00:45.780
And then we can just specify a prop int of ID inside here.

00:45.780 --> 00:48.060
And this is what we'll use for updating.

00:48.060 --> 00:54.810
So when we do update a product we're going to send the ID along with the product and use that to go

00:54.810 --> 00:59.370
and get the entity, the product entity from our database.

00:59.370 --> 01:05.680
So we need a new mapping inside our mapping profiles to go from an update product DTO to a product.

01:05.680 --> 01:13.870
So I'll just copy this line down and swap create product DTO for update product DTO going into a product.

01:13.870 --> 01:20.830
And then we can go to our product controller and we'll create a new endpoint for updating.

01:21.520 --> 01:29.020
So below the HTTP post I'll create an HTTP put which is what we use for updating a resource.

01:29.020 --> 01:35.860
And we're going to make this authorized once again for roles and specify admin.

01:36.940 --> 01:40.840
And we'll use public async task.

01:41.230 --> 01:43.690
And what do we return from an update.

01:43.720 --> 01:49.780
Well nothing because our client is sending us the information about the thing that we're updating.

01:49.780 --> 01:54.280
So we expect the client to have all the details about the updated product.

01:54.280 --> 01:58.420
So there's no point in sending the updated product back to the client.

01:58.420 --> 02:03.150
So we're just going to use Actionresult result here without returning a type.

02:03.720 --> 02:05.910
And we'll call this update products.

02:05.910 --> 02:13.800
And we're going to take in our update products DTO and call it Update product DTO.

02:14.730 --> 02:19.410
And inside here we're going to get hold of our product first of all from the database.

02:19.410 --> 02:25.500
So we'll use var products equals await context dot products.

02:25.500 --> 02:33.900
And we can use the find async because we do have the ID and we can specify the update product dto.id.

02:33.900 --> 02:36.360
And we need to check to make sure we have the product.

02:36.360 --> 02:43.230
So we're going to say if product is equal to null then we're going to return not found from this method.

02:43.290 --> 02:53.280
And then we can use our Automapper to map from the update product DTO into the product, and effectively

02:53.280 --> 02:59.220
updates the product entity that we're now tracking in memory.

02:59.220 --> 03:06.710
So we can use mapper dot map and we'll pass in the product DTO or the update product DTO where we're

03:06.710 --> 03:13.190
going to map from, and then where we're going to map to, which in this case is going to be the product.

03:13.190 --> 03:19.460
So our product at this stage in memory Entity Framework's tracking, this is going to have the updated

03:19.460 --> 03:24.530
properties from this object populated inside this object.

03:24.530 --> 03:27.740
And then we can just save our changes into the database.

03:27.740 --> 03:31.340
So I'll just copy this line from the method above.

03:31.340 --> 03:37.430
Paste that in and then we can check to make sure we have the result.

03:37.430 --> 03:44.090
So if we have the result then we can return from this method no content which is a successful request,

03:44.090 --> 03:49.790
but we're just explicitly stating that there's no content inside this request because it's an update

03:49.790 --> 03:52.010
and we do not have to send anything back.

03:52.130 --> 04:04.340
Otherwise we're going to return a bad request and just say A problem updating products and that's what

04:04.340 --> 04:05.600
we need for our update method.

04:05.600 --> 04:06.980
So let's check the terminal.

04:06.980 --> 04:10.280
And this time it's telling us we do need to restart our API.

04:10.310 --> 04:13.220
So let's go ahead and do just that.

04:13.220 --> 04:18.290
And once that has restarted we'll just go across to postman and we'll do another test.

04:18.290 --> 04:20.990
So we've got an edit product method here.

04:20.990 --> 04:28.610
We're sending up the name description price etc. but I have updated the name to whatever that is updated,

04:28.640 --> 04:33.830
which will allow us to confirm that the product has indeed been updated.

04:33.830 --> 04:41.840
And I've got the URL wrong inside here that needs to be not that one, and I'm missing the ID inside

04:41.840 --> 04:44.330
here, so we need the ID property.

04:44.360 --> 04:45.710
The way that we're doing this.

04:45.740 --> 04:50.900
Obviously I've changed my mind about how this is going to work since I originally did the testing,

04:50.900 --> 04:56.480
and we need ID of 1002, the ID of the product that we did just create.

04:56.480 --> 05:02.360
So I'll save this, but do double check the ID that you're using here and make sure it matches whatever

05:02.360 --> 05:04.190
the ID of the product you've got back.

05:04.190 --> 05:08.030
Or if you just want to update one of the original seeded products, you can do that too.

05:08.030 --> 05:12.140
But I'm going for this particular update, the one that I've just created.

05:12.320 --> 05:16.730
And let's send this request and see what happens.

05:17.180 --> 05:20.270
And we get the picture URL field is required.

05:20.270 --> 05:25.280
So I also need to specify this because of the way that we've configured the DTO.

05:25.310 --> 05:27.800
We also need the picture URL in here.

05:27.830 --> 05:34.460
Again you won't need to do this yourself because I will have saved these changes into the version of

05:34.460 --> 05:36.710
the postman collection that I'm giving you.

05:36.710 --> 05:44.390
So I'm just going to go back to the create product, and I'll just use the same picture URL I had there

05:44.930 --> 05:50.000
and go back to the edit products and paste that in there.

05:50.030 --> 05:52.130
Add the comma and great.

05:52.160 --> 05:52.910
Okay.

05:52.940 --> 05:55.880
So I'll click send again.

05:56.930 --> 06:02.640
And we've got an issue with the price I haven't met the validation of this, so I can presume that the

06:02.640 --> 06:10.560
random int here created a random int that was less than a hundred, because this creates a random integer

06:10.560 --> 06:12.180
between 0 and 100.

06:12.210 --> 06:16.980
I'm just going to hardcode a value inside here so that I don't have to mess around with this.

06:16.980 --> 06:22.440
So I'm just going to say 2000 in this case for the price of whatever this is.

06:22.470 --> 06:24.120
And I'll click send again.

06:24.120 --> 06:27.120
And this time we get the 204 no content.

06:27.150 --> 06:36.480
So if I go back to the, well, let's get the product and we'll get the product with the ID of 1002.

06:36.510 --> 06:44.880
And I'll click send and we get the updated version of this because we can see the updated word there.

06:44.880 --> 06:50.310
So our updating of an entity is working as well.

06:50.310 --> 06:56.610
And next we'll take a look at the other option we've not looked at in Crud operations which is the D.

06:56.850 --> 06:59.310
And that means deleting an entity.
