1 00:00:01,290 --> 00:00:02,850 So, in the last two videos, 2 00:00:02,850 --> 00:00:05,580 we created a nested POST endpoint 3 00:00:05,580 --> 00:00:08,910 in order to create new reviews on a certain tour. 4 00:00:08,910 --> 00:00:10,420 So, let's now build upon that, 5 00:00:10,420 --> 00:00:13,113 and also create a nested GET endpoint. 6 00:00:14,860 --> 00:00:17,290 So we already have our getAllReviews 7 00:00:17,290 --> 00:00:19,570 handler function implemented, right? 8 00:00:19,570 --> 00:00:22,940 But right now, all it does is to basically get an array 9 00:00:22,940 --> 00:00:26,040 of all the reviews in the review collection. 10 00:00:26,040 --> 00:00:29,020 Now, a common use case for your API 11 00:00:29,020 --> 00:00:32,030 might be to get an array of all the reviews 12 00:00:32,030 --> 00:00:34,500 of one particular tour, so very similar 13 00:00:34,500 --> 00:00:37,080 to the createReview, right? 14 00:00:37,080 --> 00:00:39,750 So basically similar to this route here, 15 00:00:39,750 --> 00:00:41,840 but except with GET. 16 00:00:41,840 --> 00:00:46,590 So, basically something like this, okay? 17 00:00:46,590 --> 00:00:49,330 And so, all we need to do in order to implement this 18 00:00:49,330 --> 00:00:51,640 is to do some simple changes 19 00:00:51,640 --> 00:00:54,880 to our getAllReviews handler function, right? 20 00:00:54,880 --> 00:00:57,760 Because right now, thanks to the merge params, 21 00:00:57,760 --> 00:01:02,290 and this kind of redirecting that we implemented here 22 00:01:02,290 --> 00:01:05,510 in the last video, so thanks to all that, 23 00:01:05,510 --> 00:01:09,070 this getAllReviews handler function will now automatically 24 00:01:09,070 --> 00:01:11,660 get called whenever there is a GET request 25 00:01:11,660 --> 00:01:14,020 for a URL that looks like this, 26 00:01:14,020 --> 00:01:17,060 and will also get access to the tourId, 27 00:01:17,060 --> 00:01:20,950 and again, thanks to mergeParams set to true. 28 00:01:20,950 --> 00:01:21,783 All right? 29 00:01:21,783 --> 00:01:26,783 So let's just do some very simple changes here, all right? 30 00:01:27,080 --> 00:01:29,080 And what we're going to do here is to check 31 00:01:29,080 --> 00:01:32,450 if there is a tourId, and if there is one, well, 32 00:01:32,450 --> 00:01:34,540 then we're only going to search for reviews 33 00:01:34,540 --> 00:01:38,260 where the tour is equal to that tourId, okay? 34 00:01:38,260 --> 00:01:39,530 So, that's something that's very simple 35 00:01:39,530 --> 00:01:42,800 to implement using find, right? 36 00:01:42,800 --> 00:01:46,600 So basically, what we're doing is something like this. 37 00:01:46,600 --> 00:01:51,127 So, if there is request.params.tourId, 38 00:01:53,690 --> 00:01:56,230 then we want to create a filter object, 39 00:01:56,230 --> 00:01:59,100 which we will then later use right here. 40 00:01:59,100 --> 00:02:02,580 Let's actually put it here right away, 41 00:02:02,580 --> 00:02:05,820 and we also need to kind of initialize it. 42 00:02:05,820 --> 00:02:10,820 So, let filter, because we want to then mutate this, okay? 43 00:02:11,530 --> 00:02:14,460 So, if there is a tourId, then this filter 44 00:02:14,460 --> 00:02:15,710 should be equal to 45 00:02:17,880 --> 00:02:20,087 tour: req.params.tourId. 46 00:02:25,570 --> 00:02:28,740 Okay, and so, again, if there is a tourId, 47 00:02:28,740 --> 00:02:32,520 then basically, this object here is what will be here. 48 00:02:32,520 --> 00:02:34,610 And so then only the reviews where the tour 49 00:02:34,610 --> 00:02:37,090 matches the ID are going to be found. 50 00:02:37,090 --> 00:02:40,610 So if it's all regular API call without nested route, 51 00:02:40,610 --> 00:02:44,450 well then that filter will simply be this empty object, 52 00:02:44,450 --> 00:02:48,130 and so then we're gonna find all the reviews, okay? 53 00:02:48,130 --> 00:02:50,140 And, so, let's actually test this, 54 00:02:50,140 --> 00:02:54,423 because this is all we need to change here, all right? 55 00:02:56,030 --> 00:02:57,913 So, let's save this one, 56 00:02:59,100 --> 00:03:03,680 this one as well, all right? 57 00:03:03,680 --> 00:03:07,300 And so, to start, let's test if our getAllReviews, 58 00:03:07,300 --> 00:03:12,060 so the regular one, still works as intended, okay? 59 00:03:12,060 --> 00:03:15,830 So we get all the four reviews that are in our collection. 60 00:03:15,830 --> 00:03:17,373 Let's just verify that, 61 00:03:19,320 --> 00:03:22,803 and indeed there are four documents, okay? 62 00:03:23,800 --> 00:03:25,220 But now, let's actually create 63 00:03:25,220 --> 00:03:29,510 our nested getAllReviews route, okay? 64 00:03:29,510 --> 00:03:32,433 So, what I'm going to do is to copy this one. 65 00:03:35,270 --> 00:03:40,270 Okay, and then simply add reviews, okay? 66 00:03:40,460 --> 00:03:43,670 And so this tour is the City Wanderer, 67 00:03:43,670 --> 00:03:46,020 and it actually has one review. 68 00:03:46,020 --> 00:03:48,530 And so, we expect now to get an array 69 00:03:48,530 --> 00:03:51,340 with one object in there, okay? 70 00:03:51,340 --> 00:03:53,110 So, it just had one review. 71 00:03:53,110 --> 00:03:55,770 And indeed, that is exactly what we get. 72 00:03:55,770 --> 00:04:00,430 So these are all the reviews that are present on this tour, 73 00:04:00,430 --> 00:04:02,860 so, the tour with this ID. 74 00:04:02,860 --> 00:04:06,323 And, actually, that's pretty correct, right? 75 00:04:07,210 --> 00:04:11,220 Let's try another one, and I remember that this one, 76 00:04:11,220 --> 00:04:14,743 so the Forest Hiker, actually had I think two reviews. 77 00:04:15,990 --> 00:04:19,173 So let's see if that works as well. 78 00:04:21,000 --> 00:04:22,690 And so yeah, indeed. 79 00:04:22,690 --> 00:04:23,563 Here there are. 80 00:04:24,930 --> 00:04:26,340 All right? 81 00:04:26,340 --> 00:04:30,830 So let's save that, still in that tours/review folder, 82 00:04:30,830 --> 00:04:33,387 and so getAllReviews on tour. 83 00:04:39,840 --> 00:04:41,540 Okay, great. 84 00:04:41,540 --> 00:04:45,710 That's actually all I had to show you about nested routes. 85 00:04:45,710 --> 00:04:49,610 So next up, let's continue filling up our review controller, 86 00:04:49,610 --> 00:04:52,462 because right now we really only have getAllReviews 87 00:04:52,462 --> 00:04:54,350 and createReview. 88 00:04:54,350 --> 00:04:56,380 And so we also want to update, 89 00:04:56,380 --> 00:04:58,540 to delete, and all that stuff, 90 00:04:58,540 --> 00:05:01,253 and so let's take care of that in the next lecture.