Okay, let's next take a look at editing a product in this lesson.
And we'll head back to VS code.
And we're going to create a DTO for editing of a product.
So I'll just close all of this stuff down for the time being.
We'll go to our DTO folder and we'll create a new file that's going to be a class.
And we'll call it Update Product DTO.
And what do we need inside here for the properties?
Well, we need all of this effectively.
And we can also include the ID with this as well.
So to save a bit of time and duplication we can come to our update product DTA.
And we can just derive from our create product DTO.
And then we can just specify a prop int of ID inside here.
And this is what we'll use for updating.
So when we do update a product we're going to send the ID along with the product and use that to go
and get the entity, the product entity from our database.
So we need a new mapping inside our mapping profiles to go from an update product DTO to a product.
So I'll just copy this line down and swap create product DTO for update product DTO going into a product.
And then we can go to our product controller and we'll create a new endpoint for updating.
So below the HTTP post I'll create an HTTP put which is what we use for updating a resource.
And we're going to make this authorized once again for roles and specify admin.
And we'll use public async task.
And what do we return from an update.
Well nothing because our client is sending us the information about the thing that we're updating.
So we expect the client to have all the details about the updated product.
So there's no point in sending the updated product back to the client.
So we're just going to use Actionresult result here without returning a type.
And we'll call this update products.
And we're going to take in our update products DTO and call it Update product DTO.
And inside here we're going to get hold of our product first of all from the database.
So we'll use var products equals await context dot products.
And we can use the find async because we do have the ID and we can specify the update product dto.id.
And we need to check to make sure we have the product.
So we're going to say if product is equal to null then we're going to return not found from this method.
And then we can use our Automapper to map from the update product DTO into the product, and effectively
updates the product entity that we're now tracking in memory.
So we can use mapper dot map and we'll pass in the product DTO or the update product DTO where we're
going to map from, and then where we're going to map to, which in this case is going to be the product.
So our product at this stage in memory Entity Framework's tracking, this is going to have the updated
properties from this object populated inside this object.
And then we can just save our changes into the database.
So I'll just copy this line from the method above.
Paste that in and then we can check to make sure we have the result.
So if we have the result then we can return from this method no content which is a successful request,
but we're just explicitly stating that there's no content inside this request because it's an update
and we do not have to send anything back.
Otherwise we're going to return a bad request and just say A problem updating products and that's what
we need for our update method.
So let's check the terminal.
And this time it's telling us we do need to restart our API.
So let's go ahead and do just that.
And once that has restarted we'll just go across to postman and we'll do another test.
So we've got an edit product method here.
We're sending up the name description price etc. but I have updated the name to whatever that is updated,
which will allow us to confirm that the product has indeed been updated.
And I've got the URL wrong inside here that needs to be not that one, and I'm missing the ID inside
here, so we need the ID property.
The way that we're doing this.
Obviously I've changed my mind about how this is going to work since I originally did the testing,
and we need ID of 1002, the ID of the product that we did just create.
So I'll save this, but do double check the ID that you're using here and make sure it matches whatever
the ID of the product you've got back.
Or if you just want to update one of the original seeded products, you can do that too.
But I'm going for this particular update, the one that I've just created.
And let's send this request and see what happens.
And we get the picture URL field is required.
So I also need to specify this because of the way that we've configured the DTO.
We also need the picture URL in here.
Again you won't need to do this yourself because I will have saved these changes into the version of
the postman collection that I'm giving you.
So I'm just going to go back to the create product, and I'll just use the same picture URL I had there
and go back to the edit products and paste that in there.
Add the comma and great.
Okay.
So I'll click send again.
And we've got an issue with the price I haven't met the validation of this, so I can presume that the
random int here created a random int that was less than a hundred, because this creates a random integer
between 0 and 100.
I'm just going to hardcode a value inside here so that I don't have to mess around with this.
So I'm just going to say 2000 in this case for the price of whatever this is.
And I'll click send again.
And this time we get the 204 no content.
So if I go back to the, well, let's get the product and we'll get the product with the ID of 1002.
And I'll click send and we get the updated version of this because we can see the updated word there.
So our updating of an entity is working as well.
And next we'll take a look at the other option we've not looked at in Crud operations which is the D.
And that means deleting an entity.