Hey, did everyone and they share?
And in this video, we're going to work on two different routes now.
One of them is optional.
If you want to work with that, that's fine.
If you don't want to work with that, that's also absolutely fine.
Now, what we have designed so far is get all product.
Now this get all product is actually open to everyone.
So if I go ahead and we can close the models, if I go ahead and work on with this one, notice here
this admin route is handled by this not admin route.
This user route is handled by get all products.
Now if admin also want to get all the product, he can just use a slash admin slash products and can
be defined with the same route.
Since we are not passing on any information, it will just give us all the products here.
Now, this is definitely one of the thing that we can actually use this route, but in majority of the
cases you are going to see that such situation.
We actually create a different route.
Now, personally, I would say, hey, this one is actually admin route, so I'm going to export admin
get all products.
This actually precisely defined that this route is created only for the admin, so please use that cautiously.
So we're going to go ahead and wrap it around with the big promise, just like always.
And we are going to go ahead and say, hey, this one is a sync.
Let's go ahead and say request response and the next, just like that.
There we go.
Now, this shouldn't be a big deal and you should be able to all you had to do is simply get all the
products and throw them in the response just like that.
So let's go ahead and say, hey, products and not products.
Actually, product, the model I'm trying to call here and we are going to go ahead and simply send,
find and know parameters inside that.
And let's hold this into a product's available since this is going to be an array, since this is a
database call, we are going to go ahead and say, hey, this is going to await there.
Now, once this is all done, we are going to go ahead and say, hey, resort status and we are going
to go ahead and say, hey, this is the status that we got.
So 200 and further down the road, let's throw up a JSON file which is going to say success is true.
And once we got the success, let's go ahead and send all the products.
So that's it.
Now take a moment.
Take a moment and think when you started this course, this was all too difficult for you.
And now, without even thinking, you are throwing up all the values that you are receiving from the
database and you are absolutely 100% understanding this is a moment of self appreciation, so give yourself
a pat on the back.
Now, if we don't find any product, let's handle all that case as well just in case.
So we're going to go ahead and say if there are no products, then hopefully we can raise an error or
we can just throw a different response that the product we didn't found.
But again, if the product count is going to be empty, then this is going to be all good.
But I don't think we need actually to handle this situation because even this is going to give us an
array.
So if there are no products inside that, it will give an empty response or a zero valued array.
But in case you want to have the situation being handled like this, go ahead and handle that.
It's up to you.
You totally know and understand this one.
Let's go ahead and define a product root for this one.
So this one is going to be admin root.
So let's go ahead and copy this one here.
Not like that and let's go ahead and create another one actually.
So this one is going to be simply router dot root and what is the root that you are looking for?
I'm going to go and say this is going to be slash admin, slash products and this is going to be a root
on which simply get method should be fine in this case, but this should be actually logged in user.
So is logged in.
We should also have a custom role being defined which should be admin and after that we can actually
run this method.
We need to call this method first inside the file.
So get admin, get all product.
Copy that and I should place it here.
There we go.
And now this can be used up here.
So there we go.
Okay, so this is all good and this is all nice.
Told you.
This is really one of the simplest one.
Now, another method that we are going to work on with is okay, so that was fun again, let's move
on.
So this is the admin route.
But again, if I look into this get admin product now imagine how this application is going to look
like.
We'll have on the home page all the description about basic description card about what this product
is going to be and all of that.
But as soon as a user clicks on that card, he gets more detail about it.
Probably you have more fields in there, colours and sizes and brands and all that information.
So we need to also create a route in which user can get individual product.
Now this is going to be really simple.
You click on the card itself, it will give you the ID in the URL itself, get the ID from the URL and
this and the find the product based on that ID and just send the response.
A really simple one here.
So I'm going to go ahead and say, hey, let's go ahead and copy this actually this time.
So let's go ahead and not the add.
So let's go ahead and copy this one and we can actually minimize this one and open this one.
So this one is going to be let's.
Open this one.
First, I don't want all of this information, so I'm going to go ahead and remove this and let's go
ahead and call this one as get single products because that's what it is doing.
Get single product or get one product, whatever you like.
Let's call it.
Let's get one product.
Makes more sense.
One single doesn't really bother us much.
It's almost one are the same.
So let's go ahead and say I want to find a product.
So let's go ahead and say product.
This is a controller.
So this is going to be defined by ID and the ID we are going to grab from the URL.
So request dot params dot per ramp per ramp.
Come on, params dot ID.
So we need to take care of that inside the root here as well.
Let's go ahead and hold that into available.
So we're going to call this one as product, not products because this is a singular one.
And since this is going to come from the database itself, let's go ahead and hold that.
Okay.
Now, all we have to do is first check out in this case, I really want to check out because maybe the
params ID might be malicious or maybe wrong itself.
So we are going to go ahead and check this one that if there is no product in this case, then I want
to definitely raise the exception here.
So I'm going to go ahead and say let's go ahead and wrap this around into and next, create a new object
from our custom error class.
And there we go.
And we are going to say no product found with this ID or we can actually throw back the ID itself.
And let's go ahead and classic 401 error.
And if all this is good, then we can actually go ahead and copy the response from here and send it
back just like that instead of the products.
This time we are going to send the product because this is a similar one.
So that is it.
Now let's go ahead and create a route for this one.
So let's go ahead and work here.
So these are all admin routes, these are your user routes.
So we're going to go ahead and work on that.
So this is going to be slash product, slash and colon ID, make sure you call it as ID because in the
params we had actually expecting that ID should be coming up here.
So requested params dot id.
Let's go ahead and grab this get one product should be really simple.
First make sure you import that and after that you go ahead and paste it up here again.
This is going to be classic get request.
There shouldn't be much bother and we don't need to protect this route because everybody should be able
to see that what kind of products we have got in our inventory or on our website.
So told you really easy.
Let's go ahead and catch up in the next video.