Instructor: So, in the last two videos,
we created a nested POST endpoint
in order to create new reviews on a certain tour.
So, let's now build upon that,
and also create a nested GET endpoint.
So we already have our getAllReviews
handler function implemented, right?
But right now, all it does is to basically get an array
of all the reviews in the review collection.
Now, a common use case for your API
might be to get an array of all the reviews
of one particular tour, so very similar
to the createReview, right?
So basically similar to this route here,
but except with GET.
So, basically something like this, okay?
And so, all we need to do in order to implement this
is to do some simple changes
to our getAllReviews handler function, right?
Because right now, thanks to the merge params,
and this kind of redirecting that we implemented here
in the last video, so thanks to all that,
this getAllReviews handler function will now automatically
get called whenever there is a GET request
for a URL that looks like this,
and will also get access to the tourId,
and again, thanks to mergeParams set to true.
All right?
So let's just do some very simple changes here, all right?
And what we're going to do here is to check
if there is a tourId, and if there is one, well,
then we're only going to search for reviews
where the tour is equal to that tourId, okay?
So, that's something that's very simple
to implement using find, right?
So basically, what we're doing is something like this.
So, if there is request.params.tourId,
then we want to create a filter object,
which we will then later use right here.
Let's actually put it here right away,
and we also need to kind of initialize it.
So, let filter, because we want to then mutate this, okay?
So, if there is a tourId, then this filter
should be equal to
tour: req.params.tourId.
Okay, and so, again, if there is a tourId,
then basically, this object here is what will be here.
And so then only the reviews where the tour
matches the ID are going to be found.
So if it's all regular API call without nested route,
well then that filter will simply be this empty object,
and so then we're gonna find all the reviews, okay?
And, so, let's actually test this,
because this is all we need to change here, all right?
So, let's save this one,
this one as well, all right?
And so, to start, let's test if our getAllReviews,
so the regular one, still works as intended, okay?
So we get all the four reviews that are in our collection.
Let's just verify that,
and indeed there are four documents, okay?
But now, let's actually create
our nested getAllReviews route, okay?
So, what I'm going to do is to copy this one.
Okay, and then simply add reviews, okay?
And so this tour is the City Wanderer,
and it actually has one review.
And so, we expect now to get an array
with one object in there, okay?
So, it just had one review.
And indeed, that is exactly what we get.
So these are all the reviews that are present on this tour,
so, the tour with this ID.
And, actually, that's pretty correct, right?
Let's try another one, and I remember that this one,
so the Forest Hiker, actually had I think two reviews.
So let's see if that works as well.
And so yeah, indeed.
Here there are.
All right?
So let's save that, still in that tours/review folder,
and so getAllReviews on tour.
Okay, great.
That's actually all I had to show you about nested routes.
So next up, let's continue filling up our review controller,
because right now we really only have getAllReviews
and createReview.
And so we also want to update,
to delete, and all that stuff,
and so let's take care of that in the next lecture.