All right, guys.
To delete a product.
So we have the delete button here, but we need the functionality on the back end and the front end.
So let's start with the back.
We'll go to our product controller and the last route we created was the update.
So let's, uh, I'll just copy this.
Entire function here.
And let's change this.
So this is going to be to delete a product and it's going to be a delete request to API products, ID,
private admin.
Let's change the name of the function to delete product.
And let's see what we're going to do here.
So it's going to be much less than this.
We do need to find the product.
So let's get rid of that because we don't need anything from the body.
We do want to get the product and then we do want to check for it.
So in here, what we'll do is I don't want to call.
Actually don't want to do that.
So we'll get rid of that.
I want to do a wait and.
Await.
And then we're going to use the product model and we're going to use the delete one.
Method.
So delete one and then pass in an object.
And we want to find where the underscore ID is equal to the product that we got.
Dot underscore id.
And then once we do that, we're just going to simply do a rez status of 200 and dot Json and we're
just going to say product deleted.
And then if there's no product, send a 404 product not found.
So very simple.
And then we're going to export delete product.
So in many cases, in my opinion, I think backend development is easier than front end development
just looking at this course.
I mean, you guys, obviously you can't I guess you could let me know through like comments and stuff.
Well, yeah, let me know in the comments if you're watching this on Travis Media.com, if you think
backend development in the case of building APIs and so on is easier than front end because with front
end you're dealing with the user interface and you're dealing with state.
And to me it's much more complicated.
With projects like this, there are cases where the back end is very, very complicated, but not so
much when it's just a simple API and simple authentication.
So that should do it for the controller.
Now we do have to add that route.
So in routes, product routes, let's see, we're going to add or we're going to first bring in delete
product and we're going to add that to, let's see to the ID right here.
So we already have a get and we have a put.
Now we're going to add a dot delete.
And we're going to add the protect middleware, the admin middleware, and then the function delete
product.
And that should do it for the back end.
So now let's go into the front end and we're first going to go into the slice and you can see kind of
the workflow for for every piece of functionality, you add the back end route with the controller dealing
with the database, whatever it is you're going to do, dealing with Mongoose, then you go to the front
end and you start with the state and at least this is what we're doing, starting with the global state
inside the slice and the actions.
And then we go into the screen and we implement the functionality, the user events and so on.
So let's go to Products API Slice and let's see, we're going to add right under upload product image.
Let's add delete product, which is going to be a mutation and we're going to have a query with the
product ID, The URL is going to be our product URL slash product ID, it's going to be a delete.
And that should do it.
And then we just need to.
Use delete product mutation.
We're going to export that so we can use it in our screen.
Now let's go to our product list screen.
Which is in our admin folder.
Product list screen.
We're already bringing in a couple of things from our API slice.
Now we want to bring in the use delete product mutation and then we're going to use that down here,
let's say.
Const.
And we're going to have this function called delete product.
We have this is loading, which we're going to rename to loading delete, and then we're going to need
a delete handler.
Let's see.
Do we already?
Wait.
We have our delete handler, right?
Yeah, it's right here.
So we already have that and it's taking in the ID, so let's just make it a synchronous.
And then we can get rid of that.
And let's let's do a quick confirm.
So we'll say if window dot.
Confirm.
And we'll just say, Are you sure?
And if they click yes, then we're going to try catch.
And let's see.
In the try.
We're going to await on our delete product and we're going to Refetch.
We brought Refetch in from the query, right?
Yeah, right here.
So we'll refetch.
And then if there's an error, we're going to toast.
Toast dot error.
And we'll pass in here.
E rr dot data.
Yeah.
So that should do it.
And for the loading.
Let's go down here.
And where do we have the loading?
For right here.
So we have loading create.
Let's go ahead and just add loading.
Delete.
And we'll save that.
Let's try it out.
Come over here.
I'm going to just reload.
And this test product that I created, I'm going to click, delete and click.
Okay.
And there it goes.
Cool.
And we could do a toast.
Success.
We might as well do that.
So let's go right here and just say toast.
Success.
Product deleted.
And I don't want to delete any of these, so I'm going to just create another dummy product and delete.
And now we get the little toast message as well.
All right, cool.
So next, we're going to do the we're going to start on the users, right?
Because as an admin, we want to be able to to see our users and be able to delete them and edit and
so on.
So we're going to do that next.