WEBVTT

00:03.850 --> 00:05.230
Hey, did everyone and they share?

00:05.230 --> 00:08.230
And in this video, we're going to work on two different routes now.

00:08.230 --> 00:09.310
One of them is optional.

00:09.310 --> 00:11.020
If you want to work with that, that's fine.

00:11.020 --> 00:14.410
If you don't want to work with that, that's also absolutely fine.

00:14.680 --> 00:17.140
Now, what we have designed so far is get all product.

00:17.140 --> 00:19.840
Now this get all product is actually open to everyone.

00:19.840 --> 00:24.730
So if I go ahead and we can close the models, if I go ahead and work on with this one, notice here

00:24.730 --> 00:27.990
this admin route is handled by this not admin route.

00:28.000 --> 00:30.190
This user route is handled by get all products.

00:30.190 --> 00:35.860
Now if admin also want to get all the product, he can just use a slash admin slash products and can

00:35.860 --> 00:37.090
be defined with the same route.

00:37.100 --> 00:40.870
Since we are not passing on any information, it will just give us all the products here.

00:40.960 --> 00:45.040
Now, this is definitely one of the thing that we can actually use this route, but in majority of the

00:45.040 --> 00:47.290
cases you are going to see that such situation.

00:47.290 --> 00:49.420
We actually create a different route.

00:49.450 --> 00:54.580
Now, personally, I would say, hey, this one is actually admin route, so I'm going to export admin

00:55.000 --> 00:56.800
get all products.

00:57.070 --> 01:02.650
This actually precisely defined that this route is created only for the admin, so please use that cautiously.

01:02.650 --> 01:06.610
So we're going to go ahead and wrap it around with the big promise, just like always.

01:06.610 --> 01:09.610
And we are going to go ahead and say, hey, this one is a sync.

01:09.730 --> 01:13.960
Let's go ahead and say request response and the next, just like that.

01:13.960 --> 01:14.740
There we go.

01:14.890 --> 01:19.690
Now, this shouldn't be a big deal and you should be able to all you had to do is simply get all the

01:19.690 --> 01:22.390
products and throw them in the response just like that.

01:22.390 --> 01:26.380
So let's go ahead and say, hey, products and not products.

01:26.380 --> 01:30.610
Actually, product, the model I'm trying to call here and we are going to go ahead and simply send,

01:30.610 --> 01:32.680
find and know parameters inside that.

01:32.680 --> 01:37.570
And let's hold this into a product's available since this is going to be an array, since this is a

01:37.570 --> 01:41.020
database call, we are going to go ahead and say, hey, this is going to await there.

01:41.170 --> 01:47.140
Now, once this is all done, we are going to go ahead and say, hey, resort status and we are going

01:47.140 --> 01:49.420
to go ahead and say, hey, this is the status that we got.

01:49.420 --> 01:57.040
So 200 and further down the road, let's throw up a JSON file which is going to say success is true.

01:57.130 --> 02:00.610
And once we got the success, let's go ahead and send all the products.

02:00.610 --> 02:01.510
So that's it.

02:01.510 --> 02:02.680
Now take a moment.

02:02.680 --> 02:07.180
Take a moment and think when you started this course, this was all too difficult for you.

02:07.180 --> 02:11.500
And now, without even thinking, you are throwing up all the values that you are receiving from the

02:11.500 --> 02:17.080
database and you are absolutely 100% understanding this is a moment of self appreciation, so give yourself

02:17.080 --> 02:18.130
a pat on the back.

02:18.220 --> 02:23.260
Now, if we don't find any product, let's handle all that case as well just in case.

02:23.440 --> 02:32.230
So we're going to go ahead and say if there are no products, then hopefully we can raise an error or

02:32.230 --> 02:35.620
we can just throw a different response that the product we didn't found.

02:35.620 --> 02:39.550
But again, if the product count is going to be empty, then this is going to be all good.

02:39.550 --> 02:45.220
But I don't think we need actually to handle this situation because even this is going to give us an

02:45.220 --> 02:45.520
array.

02:45.520 --> 02:50.590
So if there are no products inside that, it will give an empty response or a zero valued array.

02:50.920 --> 02:55.030
But in case you want to have the situation being handled like this, go ahead and handle that.

02:55.030 --> 02:55.840
It's up to you.

02:56.020 --> 02:58.030
You totally know and understand this one.

02:58.030 --> 03:00.670
Let's go ahead and define a product root for this one.

03:00.670 --> 03:02.740
So this one is going to be admin root.

03:02.740 --> 03:05.770
So let's go ahead and copy this one here.

03:05.770 --> 03:11.170
Not like that and let's go ahead and create another one actually.

03:11.500 --> 03:19.180
So this one is going to be simply router dot root and what is the root that you are looking for?

03:19.480 --> 03:27.010
I'm going to go and say this is going to be slash admin, slash products and this is going to be a root

03:27.010 --> 03:33.310
on which simply get method should be fine in this case, but this should be actually logged in user.

03:33.310 --> 03:34.420
So is logged in.

03:34.450 --> 03:40.390
We should also have a custom role being defined which should be admin and after that we can actually

03:40.390 --> 03:41.110
run this method.

03:41.110 --> 03:44.050
We need to call this method first inside the file.

03:44.050 --> 03:46.270
So get admin, get all product.

03:46.270 --> 03:50.410
Copy that and I should place it here.

03:51.790 --> 03:52.450
There we go.

03:52.450 --> 03:54.640
And now this can be used up here.

03:54.670 --> 03:55.900
So there we go.

03:56.630 --> 03:59.470
Okay, so this is all good and this is all nice.

03:59.470 --> 03:59.860
Told you.

03:59.860 --> 04:01.390
This is really one of the simplest one.

04:01.390 --> 04:09.220
Now, another method that we are going to work on with is okay, so that was fun again, let's move

04:09.220 --> 04:09.430
on.

04:09.430 --> 04:10.630
So this is the admin route.

04:10.630 --> 04:15.430
But again, if I look into this get admin product now imagine how this application is going to look

04:15.430 --> 04:15.670
like.

04:15.670 --> 04:20.770
We'll have on the home page all the description about basic description card about what this product

04:20.770 --> 04:22.090
is going to be and all of that.

04:22.090 --> 04:25.270
But as soon as a user clicks on that card, he gets more detail about it.

04:25.270 --> 04:29.770
Probably you have more fields in there, colours and sizes and brands and all that information.

04:29.770 --> 04:33.700
So we need to also create a route in which user can get individual product.

04:33.700 --> 04:35.050
Now this is going to be really simple.

04:35.050 --> 04:40.510
You click on the card itself, it will give you the ID in the URL itself, get the ID from the URL and

04:40.510 --> 04:44.460
this and the find the product based on that ID and just send the response.

04:44.470 --> 04:46.180
A really simple one here.

04:46.240 --> 04:49.990
So I'm going to go ahead and say, hey, let's go ahead and copy this actually this time.

04:49.990 --> 04:52.960
So let's go ahead and not the add.

04:52.960 --> 05:00.100
So let's go ahead and copy this one and we can actually minimize this one and open this one.

05:00.100 --> 05:01.750
So this one is going to be let's.

05:01.830 --> 05:02.850
Open this one.

05:02.910 --> 05:08.220
First, I don't want all of this information, so I'm going to go ahead and remove this and let's go

05:08.220 --> 05:11.740
ahead and call this one as get single products because that's what it is doing.

05:11.760 --> 05:16.770
Get single product or get one product, whatever you like.

05:16.920 --> 05:17.410
Let's call it.

05:17.430 --> 05:18.650
Let's get one product.

05:18.660 --> 05:19.700
Makes more sense.

05:19.710 --> 05:22.410
One single doesn't really bother us much.

05:22.410 --> 05:23.940
It's almost one are the same.

05:24.030 --> 05:26.460
So let's go ahead and say I want to find a product.

05:26.460 --> 05:28.320
So let's go ahead and say product.

05:28.980 --> 05:30.000
This is a controller.

05:30.000 --> 05:35.910
So this is going to be defined by ID and the ID we are going to grab from the URL.

05:35.910 --> 05:41.970
So request dot params dot per ramp per ramp.

05:41.970 --> 05:45.210
Come on, params dot ID.

05:45.240 --> 05:48.090
So we need to take care of that inside the root here as well.

05:48.090 --> 05:50.100
Let's go ahead and hold that into available.

05:50.100 --> 05:54.180
So we're going to call this one as product, not products because this is a singular one.

05:54.180 --> 05:58.140
And since this is going to come from the database itself, let's go ahead and hold that.

05:58.350 --> 05:58.680
Okay.

05:58.680 --> 06:03.120
Now, all we have to do is first check out in this case, I really want to check out because maybe the

06:03.120 --> 06:06.990
params ID might be malicious or maybe wrong itself.

06:07.110 --> 06:13.410
So we are going to go ahead and check this one that if there is no product in this case, then I want

06:13.410 --> 06:15.450
to definitely raise the exception here.

06:15.450 --> 06:21.810
So I'm going to go ahead and say let's go ahead and wrap this around into and next, create a new object

06:21.810 --> 06:23.940
from our custom error class.

06:24.600 --> 06:25.680
And there we go.

06:26.460 --> 06:35.880
And we are going to say no product found with this ID or we can actually throw back the ID itself.

06:35.880 --> 06:38.520
And let's go ahead and classic 401 error.

06:38.550 --> 06:44.190
And if all this is good, then we can actually go ahead and copy the response from here and send it

06:44.190 --> 06:46.530
back just like that instead of the products.

06:46.530 --> 06:49.500
This time we are going to send the product because this is a similar one.

06:49.830 --> 06:51.150
So that is it.

06:51.150 --> 06:53.760
Now let's go ahead and create a route for this one.

06:53.760 --> 06:55.080
So let's go ahead and work here.

06:55.080 --> 06:57.870
So these are all admin routes, these are your user routes.

06:58.230 --> 07:00.240
So we're going to go ahead and work on that.

07:00.240 --> 07:05.970
So this is going to be slash product, slash and colon ID, make sure you call it as ID because in the

07:05.970 --> 07:09.120
params we had actually expecting that ID should be coming up here.

07:09.120 --> 07:11.040
So requested params dot id.

07:11.070 --> 07:14.670
Let's go ahead and grab this get one product should be really simple.

07:14.820 --> 07:19.770
First make sure you import that and after that you go ahead and paste it up here again.

07:19.770 --> 07:21.420
This is going to be classic get request.

07:21.420 --> 07:25.440
There shouldn't be much bother and we don't need to protect this route because everybody should be able

07:25.440 --> 07:30.540
to see that what kind of products we have got in our inventory or on our website.

07:30.540 --> 07:31.860
So told you really easy.

07:31.860 --> 07:33.750
Let's go ahead and catch up in the next video.
