Hey, did everyone share?
And in this video, we're going to go ahead and see that if somebody gives me this object, ID, this
ID of the order, I need to search this object.
And again, it can come up from the params URL, maybe a query format, maybe a body format.
However you are going to design, the front end guy is going to follow along.
So we expect that this comes up into your URL, let just say.
So let's go ahead and design the method according to that.
So we're going to go ahead and say export and this one is going to be get one order.
And that's exactly what we are doing.
And in fact, I would love to remove this and copy this because this is what I love.
Okay, there we go.
We can actually shrink this one again.
I just I'm just doing it so that I don't have to write this big promise and everything.
Let's remove everything inside this and not everything we can keep this status.
Okay.
So the goal is really simple.
Let's go ahead and find a particular order based on something is coming in the URL.
So all we got to do is order dot find by ID and find by ID will say request dot, params dot and let's
call this one as simply ID and that's pretty much it.
Let's go ahead and say const and this one is going to be order.
And by the way, this is okay.
Yeah.
Just thinking ahead of myself sometime it happens to all the programmers that they try to think a little
bit ahead of the future versions and then they get lost.
It happens to me also.
Okay, so this is all and let's go ahead and check if something is returning or not, because in this
case, the ID in the URL itself can be polluted.
So in this save, if we don't receive any order, let's go ahead and simply return and close this method
right here.
Return.
Let's wrap it up.
In the next, create a new custom error.
Custom error.
There we go.
Didn't we brought in the customer in the first place?
No, we didn't.
So this is the custom error that we got.
And this custom error takes parameter of saying.
Please check order ID and we're going to go ahead and say classic 401.
And in this one, we are going to send the response as ordered back.
This is already going in so it looks like all OC.
Let's go ahead and see that how this is going to work in.
Let's go ahead and export this create order.
Now, this is not create order.
This is get one order.
Save that.
Let's copy this.
Go into the roots and say that I need another root, which is going to be simple.
Get root and user should be logged in and then we are going to just grab this one and let's go ahead
and say a comma here, get one order.
This one will be managed by get one order.
So this is all pretty much it if somebody wants to get that, hey, what is the order that I'm having?
We're going to grab the order here again.
This is something how we are going to get the ID.
I think this is all okay.
Let me just quickly check it one more time.
So in the order controller, we are expecting that in the parameters it will come.
We'll check that.
Okay.
Nice and easy here.
We are saying that slash order slash ID, give me that ID and I will give you back.
You should be logged in to do so.
So it looks like all is okay.
Let's go ahead and test out this route.
So I will go up here.
Let's go ahead and save this one.
And this one is going to be get one order by ID and save that.
Is it a get request?
Yeah, this is a get request.
Let's go ahead and move it to get save that.
And now all we've got to do is make sure we in the previous one when we were creating the order and
again this get order by ID should not be here.
It should be here.
There we go.
So this is the one.
Okay.
Nice and easy.
When we were creating the order itself.
We had the object being received, but again, we don't have it.
Now, let's go ahead and say that this is order slash and can we get the order I.D. from here?
This is the ID exact.
So we expect that all of this data should come in.
Let's go ahead and paste this and send a get request.
And there we go.
Success.
True.
And order is coming in.
Now, I wanted to show you something really interesting here.
Now, notice here the user is coming in as this ID, so which is F one.
So we can actually go back up here and check out that.
Who is the user up here?
So in this case, if I check one, there is a last F one here and I can go into my user model and check
out who plays this one.
So this was placed by two, but maybe sometimes you actually want more information to be coming up as
because the guy who is at the front end might want to check that who is the order or who is the user
who has placed this order, his name and email and all that information.
So he might have to take this request or take this user and have to send another request to our back
end.
We can actually make the life a little bit easier.
So for this one, what we can do is just need to go into the order controller and the place where we
are having this order.
We know that there is something, a situation similar to this.
So what we can do is remove this one and use a method which is populate there we go.
In the populate you just pass on two parameter.
The first one is which field do you want to further down drill down there?
And you can see that we have a lot of such fields available in this in this case, which is the orders.
So all we want to do is drill down this user field here.
So this is the user.
Remember, it's not the model that I'm referring, it's the property of the model that I'm referring
here.
So this is the one.
So I want to drill down for that user and this is reliable that will automatically be evaluated.
And what are the information that you want to grab?
Maybe I want to grab name.
Maybe I want to grab email now.
Yes.
I'm not a big fan of this syntax that you have to pass on the properties separated from the space.
This should be comma or something else, or at least an array.
But this is how it works.
Now, if I go ahead and save this one, you are going to see that.
Let's go ahead and make the same request again.
I send it up and now if I go ahead and check this one.
So notice here, this user field is now expanded and this is now coming up as a name and email.
Now, I'll tell you very interesting stuff about this.
This populate is really, really the simplest feel.
It just allows you to drill down further, expand if you want to get more of the information, maybe
role of the user even you just have to say space and a role.
That's it.
Now in one of my course, earlier course, I asked the user that, hey, go read the documentation of
populate and just let me know how it works.
And to your surprise, a lot of people, more than ten people reached out to me and say that, hey,
you should make a video of the on the populate we are not able to understand.
Let me break it to you very clearly.
I am not able to explain everything in the course.
There is no chance that in any course in the world that you will get all the information sometimes you
need to work.
This is something that the habit should be there.
And most of the international universities, when lecturers and professors give you the assignment,
they don't give you the spoon feeding.
They gives you something which you have to dig up and you have to find it on your own.
These are not assignments that you have to just see the book and write down on the notebook.
This is not how it works, so expect such things from my courses as well.
I will push you towards your limit that you should also explore a little bit.
Learn to read the documentation and there are still so many of the hidden things that you can explore
further down the road.
So really interesting stuff and populate is that's it there is nothing more more into this to explore
further down the road.
So that's it for this one.
Let's go ahead and catch up in the next one.