Okay.
Our next task will be to delete an entity from our database or delete a resource from our database.
So let's go back to VS code.
Pretty straightforward this one.
And what I would suggest if you're feeling confident at this stage of the course then you don't really
need to watch me demonstrate this.
I'm pretty sure you'd be able to cope with this based on what we've covered so far.
I'm going to demonstrate it anyway, but feel free to pause the video and tackle this yourself, and
then just compare your version of what you've done with the version I'm about to demonstrate, and I'll
put a timestamp of where you can skip to, just to take a look at a completed code for this method that's
going to come up in this lesson.
But anyway, I'm going to demonstrate this now.
So I'm going to use Authorize and roles as equal to admin again just as I've done before.
And this is going to be an HTTP delete type of method.
And we're going to take a parameter inside here inside quotes inside curly brackets.
We're going to take the id parameter.
Feel free to use it if you prefer inside here as well.
And like I mentioned before, I don't really see much benefit in that, but it makes it clear what we're
expecting I guess at least.
So I'll use public async task and a delete method doesn't return anything apart from the HTTP response
saying that the request was okay.
So we'll use task of action result again without a type parameter.
And we'll call this method delete products and we'll pass in the int and the id inside.
Here we first need to get hold of our product.
So I'll say our product equals await context dot products.
And we'll use the find async method and pass in the id.
We'll check to see if the product is equal to null.
And if it is then we're simply going to return not found.
And below this we'll use context dot products dot remove.
And we'll pass in the product.
And then we'll specify var results equals await context dot save changes async which is greater than
zero.
And if results equals true which we can effectively shorthand to if result then we're going to return
okay.
From this.
That's the correct type of response to return for an HTTP delete method.
And if it's not okay, we're just going to return a bad request and say problem deleting the product.
And that's it for this method.
So we'll just confirm that this functionality is working.
So I'll just see if Hot Reload succeeded.
And it said it did.
And let's see if it really did.
So I'll try and delete the product in the postman request here.
I'll change 19 to 1002 because that's the product ID I have.
I'll click send and we get the 200.
Okay, so it worked.
And if we try and get that product, we'd expect to see a not found this time.
And we do.
Perfect.
So that's the delete operation.
That's the code we need for this functionality.
And what we'll take a look at next is because we have a picture URL property.
We'll give our admins the ability to upload an image for a product as well.
And that means we need to take a look at upload file and photo image functionality, and we'll start
taking a look at that next.