Hey, did everyone share?
And in this video, we're going to learn that how we can add a product.
Now, adding a product shouldn't be a big deal.
You have already seen how to create or register the user.
It is almost same.
The fields are definitely different, but we are going to have almost similar kind of strategy.
The only thing different is this time we are allowing user to send us multiple images, but that shouldn't
be an issue we have already worked through with that.
So what we're asking in the product schema that hey, name, price description, an area of photos,
maybe one or just many photos category and the brand are really important that you should send us apart
from this rating number of reviews.
Reviews, you shouldn't be bothered much about that user.
Also, you shouldn't be bothered much about it because I can access the user from request or user or
request user ID created that is automatically going to be taken care of.
Now, in reality, this is not a big of a product schema as it was looking when we were designing the
schema.
Now imagine what it takes to design a schema of a course itself.
So course itself, when you're designing an individual course, it has a photo name description, a
longer description, pricing details the discount that you are giving and the final rate that's going
to come up after discount and then a whole bunch of lectures and their links.
There's a whole lot that goes on there, but not too much.
Okay, moving on.
So how we're going to do this one?
So this is a product controller and we are going to keep this test product.
We don't need to actually keep it.
So let's go ahead and remove this one because honestly, we won't be using it even a little bit.
So let's go ahead and yeah, it was fun.
We have tested this out, so let's go ahead and remove this one.
Okay.
So we know that every single time we'll be creating a product we need a big promise around.
So we're going to go ahead and say, hey, I need a big promise.
Is it going to suggest me?
No, it's not going to suggest me.
I can actually borrow some stuff from here or be wrapping around everything with the big promise.
Yes, we are going to need cloud energy.
We are going to need big promise.
So let's go ahead and grab a big promise from the product controller itself.
We will need product itself.
So let's go ahead and say, hey, product, you will be coming from require and this will be coming
up from one directory back.
Everything is always one directory back inside the controller and everything.
That's the reason why we keep them in the folders.
So let's go into the models and inside that we have this product.
So there we go.
So just like in the user controller, we had user and Big Promise, we will be working with the customers
as well.
So it makes sense to have it as well.
And most of that stuff is we won't be working with cookie file upload.
We actually don't need it here as well.
We are not using it anyway, so let's cut this out and save that again.
We will be definitely using cloud, so let's go ahead and copy that and I guess that's all what we need
from this previous user controller.
Yeah, pretty much it.
Okay, now let's go ahead and use the big promise and create our very first method to add a product.
So we're going to go ahead and say export dot, add product, feel free to name it if you like it a
little bit different.
That is all we are okay with that.
That is going to be simple, a big promise.
We are going to wrap everything around that inside the big promise.
We'll be passing on our async function just like always.
Why a sync database is another continent.
Okay, so we're going to go ahead and say request response and and next, there we go further down the
road.
So how we are going to go ahead and create a product first, we are going to worry about the images.
So a whole lot of images are going to come in and we need to take care of that part.
Okay.
So let's go ahead and first create a simple array.
And in that area, we'll be pushing up the object.
The object will contain ID and the security, just like we have done in the past.
So let's go ahead and call this one as simply image array or images array, however you like to go ahead.
And with that, this will be an empty array and this is the one where I will be pushing up all the values.
So first, let's check out whether the images are available or not.
So in this case, I'll check whether the request files exist or not.
In case it doesn't exist, then I will be throwing up an error.
So request dot files again.
I will be kind of dependent onto the mongoose model in this case because if I check up the check of
these photos, notice they are being sent as required.
So this needs to be there.
Otherwise it will be saying that hey, there is an error.
So but you can actually go ahead and say, hey, if the request files are not there, then we can go
ahead and simply raise an exception.
So let's wrap it up around return and wrap it up around the next.
We'll be saying new and let's throw up a custom error that will be simply saying that, hey, images
are required, images or image.
However, we'd like to go ahead and say that I'm going to go ahead and throw back an error of a 4 to
1.
Okay.
Now let's go ahead.
And it is always a safe bet, although we have checked it in the past, but still kind of to make it
simpler.
I'm going to go ahead and say if request files exist, then let's go ahead and try to upload them.
Since this is this time, we're expecting that there can be more than one file.
So let's handle that via simple loop here.
So I'm going to go ahead and say, run this loop index is fine.
And as an element for me, I'm going to go ahead and say error is going to be my request to dot files
dot and I'm going to call this one as photos this time.
Make sure you convey this thing to the front end developer that, hey, we are calling this element
as photos, okay?
So a whole lot of photos are going to come up into this one.
So that is nice.
And I'm going to go ahead and say that, hey, instead of doing all of this, we are going to use the
cloud native.
So let me go ahead and remove entirely of this one.
Okay.
First thing is, let's go ahead and craft or get a result just like we always do.
So result from where this result will be coming up.
Cloud ordinary.
Cloud ordinary dot v two, of course.
And in the cloud native we have a method of uploader.
You will be helping me to upload a photo.
And what is the photo that you will be uploading?
That photo will be coming from request, dot files, dot photos.
And this photos I know is an error.
So I'm going to go ahead and throw up an index here so that the that particular index, you check it
out.
And then I will not forget this time.
The temp file path.
There we go.
Okay.
So this is going to get uploaded.
And also I want to pass on some of the objects here, so I'm going to go ahead and pass on the objects.
Now, in this case, I'm going to just provide the folder that is going to be simply let's call this
one as products, although I don't have it as of now, but I'll create that very soon.
So this is all what we got.
I don't want to kind of shrink the product or something.
I'll handle that or I'll let the front end developer actually handle that.
Whatever the products are coming in as the photo, I'll pass it on to you as it is.
You just make sure that they look good on the front end.
Otherwise I can always go ahead and provide the option of scale and crop and all of that.
But this time I won't be doing that.
Okay, moving on.
If I have this I have received this result in this particular array.
I'm going to go up next and fill up my error.
This is images error, image error.
So let's go ahead and push the values into this one.
So I'm going to go ahead and say image array, dot, push.
What do you want to push?
I want to push an object.
This object is going to contain an ID which will be coming to me as result.
Dot.
Public ID.
Come on, don't do this.
Public underscore ID again.
Public underscore ID.
Okay, so this is what we are pushing up.
The first object.
The second object is going to be secure URL.
Secure underscore URL.
And that one also will be coming up from the result itself and result will be giving me.
Why is it doing that?
I guess I forgot to add a weight here.
That is it.
Yelling at me too much.
Thank you so much, by the way.
So I guess now you should be all happy with Secure.
Now it is happy.
I love via score that sometimes it saves so much of that stuff.
So cloud net obviously will be going up there and obviously this is going to crash because we are not
done with the stuff.
Why is it yelling me the type?
Product is.
Okay.
This one is model.
And in the model itself, we have some issue.
So let's go into the model at around line number 52.
So there is a number so small, but there should be all that be okay now that we are importing this,
that is why it is having the issue.
Okay.
Let's go ahead and see that are having some issues.
We are having some issues in the route that is absolutely fine.
I'm not worried about the routes itself, which is the product route, because obviously this this test
stuff doesn't exist.
So you can go ahead and remove that and remove that.
Save that and everything is happy now.
Okay, so one thing is done that now this array exists, which is image array, and this is responsible
for holding all the images asset that we need according to the model.
So this part is all done.
Let me go ahead and see where you are ending.
So this is request file.
This is up here.
So image is part is all done.
Okay.
Now let's worry about what more we have got to worry about in this one first.
We will be receiving everything into the body, obviously.
So we'll have something like request body.
Now, in this body, now we have this dot photos.
Remember, this doesn't come in like as a default one.
You are sending me as just like photos or something like that.
And we'll be adding this field up here.
So we'll be calling this one as requests, photos or whatever that was existing.
We are going to override that one.
This one is going to be overwritten by this image array.
So let's go ahead and have this one.
So I know that previously you were carrying the photos in kind of at this syntax and now we are having
it.
In fact, what I can do is I can call this one as simply product photos or the sample photos, something
like that.
But anyways, we are overwriting it so we didn't bother as much anyway, so we can come back and change
this stuff in the in the back end.
Another thing inside this one, which is worth noticing is this request dot user.
Now I'm going to say that this request dot body is also going to have a property of dot user.
So how this is going to work, this will coming up from request dot user.
ID remember at the time of creating the model, we say that this user property here is going to be filled
up only by the user who is already logged in, so we can access that by user request dot user.
So that is it.
Now if you can look at the request dot body, it has everything that we need to save.
We expect that you'll be passing on to me this name price description from your end, photos, whatever
you are sending it.
I have already crafted it and kind of overwritten whatever you're sending it to me.
So that is fine category.
You'll be sending me that brand you will be sending me from the front end rating.
You don't need to send a number of ratings.
You also don't need to send reviews.
You also don't need to send request this user.
I have already grabbed it from request dot user and created that will automatically come in.
So that is all what we got.
Okay.
Now the most important part is let's go ahead and create our first product.
So we're going to go ahead and use the model and we are going to use the same property of create.
And inside that we're going to pass this body.
You can store this body into a variable as well.
That is fine.
Now let's go ahead and hold this into a variable.
We're going to go ahead and say const product.
And since the database is in another continent, we are going to say a weight like this and at the end
we are going to say rez dot status.
Status is going to be 200.
Then further down the road JSON and in the JSON, we are going to obviously send a success of true because
it has been uploaded now and let's also send this product.
Maybe he wants to do something with whatever he has created.
So that is also fine.
Okay.
So really interesting.
Very easy.
We just need it to handle the images.
But optionally what you can do is whenever somebody actually sends you these photos, you can actually
call them something different.
And at the time of the back end you can just have it.
But I think this consistency makes sense here.
Otherwise we can always come back and check some of the editing efforts here.
Okay, so that's it you have gone through with that.
Remember, majorly we are dependent in this case the models that they will be throwing the error in
case something is missing.
If you want to go ahead and check each field individually by putting up an or or checking individually
each field that is also absolutely fine.
Let's go ahead and catch up in the next video.