Hated everyone they shared.
And in this video, we are going to create a couple of admin routes.
In this precise video, we're going to create just one, which is update one product.
Now, obviously, admin will be allowed to update the product itself.
So let's go ahead and work on with that.
Now the only thing that you have to take care is if admin sends the photo as an update part.
Now if admin sends the photos as the updation part, then you have to delete the existing photos and
have to upload the photos and send them into the database entry wherever you are storing that.
And that's that's pretty much it.
Just all the JSON updates shouldn't bother you much because we have studied so much on that part.
So let's go ahead and rather actually go ahead and copy this.
So let's go ahead and work with that to remove this e and we have this.
Let's minimize this and let's open this one.
Now we are keeping all the admin products all together, but again, not really necessary.
So we're going to say admin update one product.
So how we're going to go ahead and do this one.
So first is we need to find a product and we'll take care of the response later on.
First, let's go ahead and work on with this one.
So this is not going to be products.
It's going to be a product because only one product is getting updated.
How user will be sending me the response?
Simply not the response.
The idea of the product.
Usually it's in the params, but if it has a query or something you know how to handle that shouldn't
be a big deal.
So request dot params dot id.
Now hopefully this has given me all the details that I want, but again, this can actually create an
issue.
So I'm going to go ahead and open this one, copy this line again, saving some time, because we have
already studied this thing quite in depth.
So there we go.
Now, if I don't find a product, I will go ahead and simply create an issue that, hey, I couldn't
find the product with this ID.
Now moving on, I will first look that if we are receiving any files or not, so we're going to go ahead
and check with the if that if the request has files or not.
Now if the request has file, then we have to do a couple of things.
First, destroy the existing images and then upload and save the images.
So these are the two tasks in front of us that we have to do.
Okay, so how we're going to destroy the images?
I can access the existing images by this product because this product has product photos and I can actually
find the length of the area so that how many times I have to run the loop and then simply use the cloud
to destroy the images.
So let's go ahead and see that how we can work on with that.
So let's go ahead and simply run a simple for loop for doing that.
I'm going to go ahead and take help of these four.
There we go.
Index is fine.
The index is going to run till the length of of photos, not photos, products.
Product, not products, product dot photos, dot length.
Now this is all good.
So this will keep on running the loop till we have the products, the length of the photos itself.
Now, let's go ahead and remove this one.
Now we want to destroy the photo here.
We can destroy the photo by accessing the product photos dot ID.
Let's go ahead and do that.
So how we actually delete that, we have to first access the cloud net.
E So let's go ahead and say cloud entry and we will get some result as well.
So we're going to go ahead and say const rest a result for this one since we won't be using it.
But still, let's go ahead and hold that maybe later on you want to get some information from that or
something or you can just use the cloud native feature of that.
We're going to go ahead and say V two dot uploader, dot destroy.
There we go.
Now this destroy obviously takes a parameter that how you're going to destroy the images.
Give me that ID.
I can give that ID by simply saying, hey, product, we are having a dot photos, dot photos and since
we are looping through that inside the photos itself, let's go ahead and have this square bracket and
this will be governed by index.
Now, each of that photo's first element, second element and third element will having a dot ID.
So there we go.
So hopefully now this will be able to access the ID and we'll be able to delete the photos.
Once this is all done, this loop is all done, we can actually get a response or something.
Now we need to upload and save the new photos that we got.
Since the requested file is there.
We can actually access all of that up here.
So first let's go ahead and call this one as images.
Edit In fact, we can design this images at the very top so that we can access everywhere.
So we're going to go ahead and say images array just like the previous one.
We are going to do exactly same.
We are going to push all the results into this images area.
So we don't need to create it up here.
Let's go ahead and work on with that.
Again, we are going to need a loop because there are a lot of photos that are coming in.
So let's go ahead.
In fact, what we can do is instead of doing like this, we can actually copy this because we have already
done that.
This is exactly the same.
So we are having this images array and we are having this request files actually from here request files.
We are uploading that into the photos and they will go and everything is coming in.
So let's go ahead and copy this and get back up here.
And inside this one, we can actually go ahead and paste everything.
Now we are already checking this request file, so we don't need to check this up here.
So I'm going to go ahead and remove this one and the last parenthesis.
There we go and save that.
And we are calling this as image array, image array.
So this should be same.
Let's go ahead and copy this and let's throw that all the result into images array this time.
So again, not really a big deal.
Let's quickly analyze what is the code that we are brought in because it's always good to have this
one.
So we are saying that, hey, if the requested files exist, let's go ahead and run a simple loop.
This will be saying requested file, photos, length, it will run the loop and we are getting all the
files.
So we are saying, hey, hold the result into cloud ready and cloud uploader will upload the photos
from request file dot photos index of it and then we are going to say dot temp file path and we'll be
uploading that product.
Now here I would like to put up a comment.
There's this folder name.
This folder name can be trouble.
So folder name can actually go into dot E and B that would make sense in this case or a longer run of
the product itself, because this folder, even if you change it like products to product, then it's
going to create an issue.
So this should really come from the E and B file.
And then we are pushing this all the values into this one and that is all good.
So now we have access to this image is Eddy.
So that's fantastic.
Now all we got to do is update this stuff here.
So let let's go up here.
I do have access of this images area at the top.
I'm going to go ahead and minimize this.
So now I have this access of images area.
All I have to do is add that to the body itself.
So I'm going to say request, dot, body, dot, whatever the photos you are bringing to me is going
to be updated by this image is array.
Images.
Edit So this images array is now properly designed the way we want it to be.
ETA Which to object object with two properties ID and the security URL.
So that is all good.
That is all nice.
Now what we need to do further down the road now the image is parted, handled.
All we need to do is update the information.
So we're going to go ahead and use this product and we are going to go ahead and say, hey, await and
we are going to use the same model product dot find by ID and update.
So let's go ahead and see how we're going to give that request dot params dot ID.
So this is the ID of the one and now we can say request dot body.
This is the entirety of the body that you have sent it to me to update.
So what are the values you are giving me to update, name, price, stock, whatever you want to update,
you can just go ahead and send me up there and further down the road I have to provide you a couple
of objects.
So first new is true.
That will make sure that I get the response as whatever the new values are, not the old values.
And we are going to go ahead and say, Hey, I want to run the validators as well run validators so
that everything is being taken care by my models itself.
Now, optionally, we would like to go ahead and use, use, find and modify all that.
We don't need to do it now, but we would still like to turn the flag on now by default in the modern
mongoose, this is already off, but still just to make sure that it is not being used.
Now this is all good.
Now all we got to do is send the success through and let's also send this product that we have recently
designed.
So hopefully this is all good and should be working absolutely fine.
Okay, so now we can actually minimize this one and hopefully things are okay and we will still test
it out and we'll check this out.
All we need is a parameters ID.
Let's go ahead and see that how this update one product is going to work on.
So copy that.
Go into the model itself and let's get this one here which is admin update one product.
So this is going and becoming a part of the admin itself.
So we can actually go ahead and copy this route, copy that and paste it up here.
Move that a little bit.
There we go.
Should be all good.
Now, what we can do is we can define that slash admin, slash product and slash.
Then we can go ahead and provide an ID here.
And he is going to be logged in custom role as admin.
Let's go ahead and grab this one and paste it up here.
Now we have a route on which we can update the product by providing slash admin, slash product, slash
ID and this event will take care of that.
So hopefully things are actually making sense.
The most important thing is not to just write code but actually understand what is going to happen,
how I'm going to handle that, how am I going to provide the response for that?
Let's go ahead and catch up in the next video.