WEBVTT

00:03.790 --> 00:05.140
Hey, did everyone share?

00:05.140 --> 00:08.120
And in this video, we're going to learn that how we can add a product.

00:08.140 --> 00:10.410
Now, adding a product shouldn't be a big deal.

00:10.420 --> 00:12.910
You have already seen how to create or register the user.

00:12.910 --> 00:14.020
It is almost same.

00:14.020 --> 00:18.280
The fields are definitely different, but we are going to have almost similar kind of strategy.

00:18.280 --> 00:22.540
The only thing different is this time we are allowing user to send us multiple images, but that shouldn't

00:22.540 --> 00:24.610
be an issue we have already worked through with that.

00:24.760 --> 00:29.740
So what we're asking in the product schema that hey, name, price description, an area of photos,

00:29.740 --> 00:35.350
maybe one or just many photos category and the brand are really important that you should send us apart

00:35.350 --> 00:36.910
from this rating number of reviews.

00:36.910 --> 00:40.270
Reviews, you shouldn't be bothered much about that user.

00:40.270 --> 00:44.710
Also, you shouldn't be bothered much about it because I can access the user from request or user or

00:44.710 --> 00:48.850
request user ID created that is automatically going to be taken care of.

00:49.090 --> 00:53.470
Now, in reality, this is not a big of a product schema as it was looking when we were designing the

00:53.470 --> 00:53.890
schema.

00:53.920 --> 00:57.730
Now imagine what it takes to design a schema of a course itself.

00:57.730 --> 01:02.350
So course itself, when you're designing an individual course, it has a photo name description, a

01:02.350 --> 01:07.480
longer description, pricing details the discount that you are giving and the final rate that's going

01:07.480 --> 01:11.500
to come up after discount and then a whole bunch of lectures and their links.

01:11.500 --> 01:14.110
There's a whole lot that goes on there, but not too much.

01:14.410 --> 01:15.340
Okay, moving on.

01:15.820 --> 01:17.530
So how we're going to do this one?

01:17.530 --> 01:22.360
So this is a product controller and we are going to keep this test product.

01:22.360 --> 01:23.770
We don't need to actually keep it.

01:23.770 --> 01:29.620
So let's go ahead and remove this one because honestly, we won't be using it even a little bit.

01:29.620 --> 01:32.500
So let's go ahead and yeah, it was fun.

01:32.500 --> 01:35.020
We have tested this out, so let's go ahead and remove this one.

01:35.890 --> 01:36.230
Okay.

01:36.280 --> 01:40.480
So we know that every single time we'll be creating a product we need a big promise around.

01:40.480 --> 01:44.590
So we're going to go ahead and say, hey, I need a big promise.

01:45.960 --> 01:47.070
Is it going to suggest me?

01:47.070 --> 01:48.300
No, it's not going to suggest me.

01:48.300 --> 01:52.860
I can actually borrow some stuff from here or be wrapping around everything with the big promise.

01:52.860 --> 01:54.650
Yes, we are going to need cloud energy.

01:54.660 --> 01:55.980
We are going to need big promise.

01:55.980 --> 02:00.930
So let's go ahead and grab a big promise from the product controller itself.

02:01.110 --> 02:02.940
We will need product itself.

02:02.940 --> 02:10.440
So let's go ahead and say, hey, product, you will be coming from require and this will be coming

02:10.440 --> 02:11.640
up from one directory back.

02:11.640 --> 02:14.880
Everything is always one directory back inside the controller and everything.

02:14.880 --> 02:17.070
That's the reason why we keep them in the folders.

02:17.070 --> 02:21.270
So let's go into the models and inside that we have this product.

02:21.270 --> 02:22.140
So there we go.

02:22.230 --> 02:27.480
So just like in the user controller, we had user and Big Promise, we will be working with the customers

02:27.480 --> 02:27.870
as well.

02:27.870 --> 02:29.910
So it makes sense to have it as well.

02:30.360 --> 02:35.280
And most of that stuff is we won't be working with cookie file upload.

02:35.310 --> 02:36.930
We actually don't need it here as well.

02:36.930 --> 02:40.260
We are not using it anyway, so let's cut this out and save that again.

02:40.350 --> 02:46.470
We will be definitely using cloud, so let's go ahead and copy that and I guess that's all what we need

02:46.470 --> 02:48.630
from this previous user controller.

02:48.630 --> 02:49.680
Yeah, pretty much it.

02:50.130 --> 02:55.440
Okay, now let's go ahead and use the big promise and create our very first method to add a product.

02:55.440 --> 03:01.800
So we're going to go ahead and say export dot, add product, feel free to name it if you like it a

03:01.800 --> 03:02.400
little bit different.

03:02.400 --> 03:04.080
That is all we are okay with that.

03:04.770 --> 03:06.720
That is going to be simple, a big promise.

03:06.720 --> 03:10.170
We are going to wrap everything around that inside the big promise.

03:10.170 --> 03:12.630
We'll be passing on our async function just like always.

03:12.630 --> 03:15.300
Why a sync database is another continent.

03:15.690 --> 03:21.990
Okay, so we're going to go ahead and say request response and and next, there we go further down the

03:21.990 --> 03:22.500
road.

03:23.520 --> 03:29.550
So how we are going to go ahead and create a product first, we are going to worry about the images.

03:29.550 --> 03:33.810
So a whole lot of images are going to come in and we need to take care of that part.

03:35.000 --> 03:35.250
Okay.

03:35.700 --> 03:38.460
So let's go ahead and first create a simple array.

03:38.460 --> 03:40.530
And in that area, we'll be pushing up the object.

03:40.530 --> 03:44.400
The object will contain ID and the security, just like we have done in the past.

03:44.640 --> 03:50.100
So let's go ahead and call this one as simply image array or images array, however you like to go ahead.

03:50.100 --> 03:55.440
And with that, this will be an empty array and this is the one where I will be pushing up all the values.

03:55.440 --> 03:58.740
So first, let's check out whether the images are available or not.

03:58.740 --> 04:02.310
So in this case, I'll check whether the request files exist or not.

04:02.430 --> 04:07.500
In case it doesn't exist, then I will be throwing up an error.

04:07.500 --> 04:11.580
So request dot files again.

04:11.580 --> 04:18.240
I will be kind of dependent onto the mongoose model in this case because if I check up the check of

04:18.240 --> 04:21.690
these photos, notice they are being sent as required.

04:21.690 --> 04:22.650
So this needs to be there.

04:22.650 --> 04:24.750
Otherwise it will be saying that hey, there is an error.

04:24.750 --> 04:30.030
So but you can actually go ahead and say, hey, if the request files are not there, then we can go

04:30.030 --> 04:32.550
ahead and simply raise an exception.

04:32.550 --> 04:37.170
So let's wrap it up around return and wrap it up around the next.

04:37.170 --> 04:45.510
We'll be saying new and let's throw up a custom error that will be simply saying that, hey, images

04:45.540 --> 04:49.230
are required, images or image.

04:49.230 --> 04:53.170
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

04:53.170 --> 04:53.430
1.

04:53.790 --> 04:54.270
Okay.

04:54.810 --> 04:55.770
Now let's go ahead.

04:55.770 --> 05:01.680
And it is always a safe bet, although we have checked it in the past, but still kind of to make it

05:01.680 --> 05:02.220
simpler.

05:02.220 --> 05:06.750
I'm going to go ahead and say if request files exist, then let's go ahead and try to upload them.

05:06.750 --> 05:10.380
Since this is this time, we're expecting that there can be more than one file.

05:10.380 --> 05:13.230
So let's handle that via simple loop here.

05:13.230 --> 05:16.380
So I'm going to go ahead and say, run this loop index is fine.

05:16.380 --> 05:22.080
And as an element for me, I'm going to go ahead and say error is going to be my request to dot files

05:22.110 --> 05:25.080
dot and I'm going to call this one as photos this time.

05:25.080 --> 05:28.680
Make sure you convey this thing to the front end developer that, hey, we are calling this element

05:28.680 --> 05:30.430
as photos, okay?

05:31.230 --> 05:34.650
So a whole lot of photos are going to come up into this one.

05:34.650 --> 05:35.610
So that is nice.

05:35.610 --> 05:39.690
And I'm going to go ahead and say that, hey, instead of doing all of this, we are going to use the

05:39.690 --> 05:40.160
cloud native.

05:40.170 --> 05:43.860
So let me go ahead and remove entirely of this one.

05:44.340 --> 05:44.700
Okay.

05:44.700 --> 05:49.230
First thing is, let's go ahead and craft or get a result just like we always do.

05:49.230 --> 05:52.050
So result from where this result will be coming up.

05:52.050 --> 05:52.920
Cloud ordinary.

05:53.520 --> 05:56.670
Cloud ordinary dot v two, of course.

05:56.670 --> 05:59.310
And in the cloud native we have a method of uploader.

05:59.310 --> 06:01.710
You will be helping me to upload a photo.

06:02.610 --> 06:05.220
And what is the photo that you will be uploading?

06:05.220 --> 06:11.280
That photo will be coming from request, dot files, dot photos.

06:12.090 --> 06:14.400
And this photos I know is an error.

06:14.400 --> 06:20.010
So I'm going to go ahead and throw up an index here so that the that particular index, you check it

06:20.010 --> 06:20.220
out.

06:20.220 --> 06:22.410
And then I will not forget this time.

06:22.410 --> 06:25.080
The temp file path.

06:25.110 --> 06:25.770
There we go.

06:27.330 --> 06:27.900
Okay.

06:27.990 --> 06:30.030
So this is going to get uploaded.

06:30.030 --> 06:34.890
And also I want to pass on some of the objects here, so I'm going to go ahead and pass on the objects.

06:35.010 --> 06:41.160
Now, in this case, I'm going to just provide the folder that is going to be simply let's call this

06:41.160 --> 06:44.970
one as products, although I don't have it as of now, but I'll create that very soon.

06:44.970 --> 06:46.560
So this is all what we got.

06:46.560 --> 06:49.800
I don't want to kind of shrink the product or something.

06:49.800 --> 06:53.130
I'll handle that or I'll let the front end developer actually handle that.

06:53.130 --> 06:56.430
Whatever the products are coming in as the photo, I'll pass it on to you as it is.

06:56.430 --> 06:58.920
You just make sure that they look good on the front end.

06:58.920 --> 07:02.760
Otherwise I can always go ahead and provide the option of scale and crop and all of that.

07:02.760 --> 07:04.380
But this time I won't be doing that.

07:04.470 --> 07:05.940
Okay, moving on.

07:06.600 --> 07:10.260
If I have this I have received this result in this particular array.

07:10.260 --> 07:13.440
I'm going to go up next and fill up my error.

07:13.470 --> 07:15.630
This is images error, image error.

07:15.660 --> 07:17.940
So let's go ahead and push the values into this one.

07:17.940 --> 07:21.180
So I'm going to go ahead and say image array, dot, push.

07:21.180 --> 07:22.440
What do you want to push?

07:22.440 --> 07:23.970
I want to push an object.

07:23.970 --> 07:29.880
This object is going to contain an ID which will be coming to me as result.

07:30.570 --> 07:31.410
Dot.

07:32.950 --> 07:34.000
Public ID.

07:35.290 --> 07:36.880
Come on, don't do this.

07:37.660 --> 07:41.170
Public underscore ID again.

07:41.830 --> 07:43.330
Public underscore ID.

07:43.740 --> 07:45.490
Okay, so this is what we are pushing up.

07:45.490 --> 07:46.330
The first object.

07:46.330 --> 07:49.060
The second object is going to be secure URL.

07:50.430 --> 07:52.290
Secure underscore URL.

07:54.230 --> 07:59.510
And that one also will be coming up from the result itself and result will be giving me.

08:00.740 --> 08:02.350
Why is it doing that?

08:02.690 --> 08:05.060
I guess I forgot to add a weight here.

08:05.060 --> 08:05.660
That is it.

08:05.930 --> 08:06.920
Yelling at me too much.

08:06.920 --> 08:08.240
Thank you so much, by the way.

08:08.300 --> 08:11.510
So I guess now you should be all happy with Secure.

08:11.540 --> 08:12.290
Now it is happy.

08:12.290 --> 08:15.770
I love via score that sometimes it saves so much of that stuff.

08:15.770 --> 08:20.000
So cloud net obviously will be going up there and obviously this is going to crash because we are not

08:20.000 --> 08:21.110
done with the stuff.

08:21.110 --> 08:24.050
Why is it yelling me the type?

08:24.950 --> 08:26.300
Product is.

08:27.710 --> 08:27.950
Okay.

08:27.950 --> 08:29.150
This one is model.

08:29.150 --> 08:32.450
And in the model itself, we have some issue.

08:32.450 --> 08:35.900
So let's go into the model at around line number 52.

08:37.640 --> 08:43.580
So there is a number so small, but there should be all that be okay now that we are importing this,

08:43.580 --> 08:45.410
that is why it is having the issue.

08:46.370 --> 08:47.000
Okay.

08:48.600 --> 08:51.150
Let's go ahead and see that are having some issues.

08:51.150 --> 08:53.760
We are having some issues in the route that is absolutely fine.

08:53.760 --> 08:59.090
I'm not worried about the routes itself, which is the product route, because obviously this this test

08:59.100 --> 09:00.240
stuff doesn't exist.

09:00.240 --> 09:03.240
So you can go ahead and remove that and remove that.

09:04.070 --> 09:06.740
Save that and everything is happy now.

09:07.140 --> 09:13.910
Okay, so one thing is done that now this array exists, which is image array, and this is responsible

09:13.910 --> 09:17.210
for holding all the images asset that we need according to the model.

09:17.870 --> 09:19.290
So this part is all done.

09:19.310 --> 09:21.650
Let me go ahead and see where you are ending.

09:21.770 --> 09:23.640
So this is request file.

09:23.660 --> 09:24.650
This is up here.

09:24.650 --> 09:26.370
So image is part is all done.

09:26.390 --> 09:26.990
Okay.

09:27.020 --> 09:31.460
Now let's worry about what more we have got to worry about in this one first.

09:32.060 --> 09:34.460
We will be receiving everything into the body, obviously.

09:34.460 --> 09:36.620
So we'll have something like request body.

09:36.650 --> 09:40.170
Now, in this body, now we have this dot photos.

09:40.190 --> 09:42.910
Remember, this doesn't come in like as a default one.

09:42.920 --> 09:46.340
You are sending me as just like photos or something like that.

09:46.340 --> 09:48.320
And we'll be adding this field up here.

09:48.330 --> 09:52.940
So we'll be calling this one as requests, photos or whatever that was existing.

09:52.940 --> 09:54.600
We are going to override that one.

09:54.620 --> 10:00.070
This one is going to be overwritten by this image array.

10:00.080 --> 10:01.910
So let's go ahead and have this one.

10:02.630 --> 10:08.720
So I know that previously you were carrying the photos in kind of at this syntax and now we are having

10:08.720 --> 10:08.840
it.

10:08.840 --> 10:15.410
In fact, what I can do is I can call this one as simply product photos or the sample photos, something

10:15.410 --> 10:15.970
like that.

10:15.980 --> 10:20.600
But anyways, we are overwriting it so we didn't bother as much anyway, so we can come back and change

10:20.600 --> 10:22.340
this stuff in the in the back end.

10:22.460 --> 10:26.900
Another thing inside this one, which is worth noticing is this request dot user.

10:26.930 --> 10:35.120
Now I'm going to say that this request dot body is also going to have a property of dot user.

10:35.210 --> 10:40.550
So how this is going to work, this will coming up from request dot user.

10:41.000 --> 10:46.700
ID remember at the time of creating the model, we say that this user property here is going to be filled

10:46.700 --> 10:51.110
up only by the user who is already logged in, so we can access that by user request dot user.

10:51.110 --> 10:51.860
So that is it.

10:52.130 --> 10:56.680
Now if you can look at the request dot body, it has everything that we need to save.

10:56.690 --> 11:03.500
We expect that you'll be passing on to me this name price description from your end, photos, whatever

11:03.500 --> 11:04.100
you are sending it.

11:04.100 --> 11:08.780
I have already crafted it and kind of overwritten whatever you're sending it to me.

11:08.780 --> 11:10.430
So that is fine category.

11:10.430 --> 11:14.930
You'll be sending me that brand you will be sending me from the front end rating.

11:14.930 --> 11:16.520
You don't need to send a number of ratings.

11:16.520 --> 11:18.650
You also don't need to send reviews.

11:18.650 --> 11:20.990
You also don't need to send request this user.

11:20.990 --> 11:24.800
I have already grabbed it from request dot user and created that will automatically come in.

11:24.800 --> 11:26.330
So that is all what we got.

11:27.040 --> 11:27.520
Okay.

11:27.530 --> 11:30.950
Now the most important part is let's go ahead and create our first product.

11:30.950 --> 11:35.180
So we're going to go ahead and use the model and we are going to use the same property of create.

11:35.180 --> 11:37.310
And inside that we're going to pass this body.

11:37.310 --> 11:39.560
You can store this body into a variable as well.

11:39.560 --> 11:40.420
That is fine.

11:40.430 --> 11:42.620
Now let's go ahead and hold this into a variable.

11:42.620 --> 11:45.590
We're going to go ahead and say const product.

11:45.590 --> 11:51.140
And since the database is in another continent, we are going to say a weight like this and at the end

11:51.140 --> 11:53.840
we are going to say rez dot status.

11:54.590 --> 11:56.390
Status is going to be 200.

11:56.390 --> 12:02.540
Then further down the road JSON and in the JSON, we are going to obviously send a success of true because

12:02.540 --> 12:06.470
it has been uploaded now and let's also send this product.

12:06.470 --> 12:09.110
Maybe he wants to do something with whatever he has created.

12:09.110 --> 12:10.490
So that is also fine.

12:10.880 --> 12:11.140
Okay.

12:11.210 --> 12:12.260
So really interesting.

12:12.260 --> 12:12.740
Very easy.

12:12.740 --> 12:14.270
We just need it to handle the images.

12:14.270 --> 12:19.910
But optionally what you can do is whenever somebody actually sends you these photos, you can actually

12:19.910 --> 12:20.960
call them something different.

12:20.960 --> 12:22.640
And at the time of the back end you can just have it.

12:22.640 --> 12:25.580
But I think this consistency makes sense here.

12:25.910 --> 12:29.480
Otherwise we can always come back and check some of the editing efforts here.

12:29.570 --> 12:31.820
Okay, so that's it you have gone through with that.

12:31.820 --> 12:36.260
Remember, majorly we are dependent in this case the models that they will be throwing the error in

12:36.260 --> 12:37.430
case something is missing.

12:37.430 --> 12:42.140
If you want to go ahead and check each field individually by putting up an or or checking individually

12:42.140 --> 12:44.600
each field that is also absolutely fine.

12:44.600 --> 12:46.430
Let's go ahead and catch up in the next video.
