1
00:00:02,120 --> 00:00:04,770
Were you able to translate this ID

2
00:00:04,770 --> 00:00:08,993
into a complete data object from that restaurants.json file?

3
00:00:09,920 --> 00:00:11,530
In the end, what we gotta do

4
00:00:11,530 --> 00:00:15,740
is we have to read that JSON file,

5
00:00:15,740 --> 00:00:18,600
as we do it in other routes as well,

6
00:00:18,600 --> 00:00:20,430
and then we have to search

7
00:00:20,430 --> 00:00:24,460
for one specific restaurant by ID.

8
00:00:24,460 --> 00:00:26,550
And we can do that

9
00:00:26,550 --> 00:00:29,560
so that we find the restaurant with that ID.

10
00:00:29,560 --> 00:00:31,960
And therefore, I'll start by reading that file.

11
00:00:31,960 --> 00:00:34,860
For this, we can again copy that code,

12
00:00:34,860 --> 00:00:37,460
which we use in other routes as well,

13
00:00:37,460 --> 00:00:41,153
copy that, and add that here.

14
00:00:42,960 --> 00:00:46,070
Now, we got access to the storedRestaurants.

15
00:00:46,070 --> 00:00:47,860
And now, that will be an array

16
00:00:47,860 --> 00:00:51,713
where only one restaurant has that ID.

17
00:00:52,900 --> 00:00:55,280
So now, we wanna look at all the restaurants,

18
00:00:55,280 --> 00:00:58,870
and once we've found the one restaurant with that ID,

19
00:00:58,870 --> 00:01:01,920
we don't wanna continue looking at the others,

20
00:01:01,920 --> 00:01:05,519
but instead, then we wanna return our response

21
00:01:05,519 --> 00:01:07,423
with that restaurant data.

22
00:01:08,890 --> 00:01:12,720
And to do this, we can again use a good old for loop,

23
00:01:12,720 --> 00:01:15,170
what we did before in this course as well,

24
00:01:15,170 --> 00:01:17,290
because we wanna look at all restaurants

25
00:01:17,290 --> 00:01:19,290
and find one specific restaurant,

26
00:01:19,290 --> 00:01:21,120
and whenever you wanna do something

27
00:01:21,120 --> 00:01:23,880
on all the items in an array,

28
00:01:23,880 --> 00:01:26,673
a for loop isn't a bad choice.

29
00:01:27,920 --> 00:01:31,180
So here, I'll again then go through all my restaurants

30
00:01:31,180 --> 00:01:33,880
in this storedRestaurants array

31
00:01:33,880 --> 00:01:36,993
with a for of loop, as you learned it.

32
00:01:38,000 --> 00:01:40,360
And then, in this for of loop,

33
00:01:40,360 --> 00:01:43,010
I wanna have an if statement,

34
00:01:43,010 --> 00:01:47,100
which is executed for every restaurant therefore,

35
00:01:47,100 --> 00:01:50,810
where I compare the restaurant ID,

36
00:01:50,810 --> 00:01:52,210
so of the restaurant

37
00:01:52,210 --> 00:01:55,123
we are currently looking at in this loop,

38
00:01:55,960 --> 00:01:59,800
with the restaurant ID I got in my URL.

39
00:01:59,800 --> 00:02:02,773
So the restaurant ID I got in my path here.

40
00:02:04,270 --> 00:02:09,133
So I'll compare this restaurantId, like this.

41
00:02:10,320 --> 00:02:11,700
And if I do that,

42
00:02:11,700 --> 00:02:14,900
then I know that if I make it into this if statement,

43
00:02:14,900 --> 00:02:16,650
so if this condition is met,

44
00:02:16,650 --> 00:02:21,163
then I've found the correct restaurant, otherwise I haven't.

45
00:02:22,810 --> 00:02:25,520
Now, once I did find the correct restaurant,

46
00:02:25,520 --> 00:02:29,450
I wanna render my response.

47
00:02:29,450 --> 00:02:34,153
I wanna render the restaurant-detail template and finish.

48
00:02:35,230 --> 00:02:38,320
Now, since I wanna finish at this point,

49
00:02:38,320 --> 00:02:40,660
I will return here,

50
00:02:40,660 --> 00:02:44,400
because we did learn that the return keyword

51
00:02:44,400 --> 00:02:47,730
does return a value to the place

52
00:02:47,730 --> 00:02:50,100
where a function was executed,

53
00:02:50,100 --> 00:02:52,460
which is not too important for us here

54
00:02:52,460 --> 00:02:56,210
because we are not executing this function on our own,

55
00:02:56,210 --> 00:02:58,313
express does that for us instead,

56
00:02:59,310 --> 00:03:04,230
but return also finishes this function execution.

57
00:03:04,230 --> 00:03:07,540
So when this return statement here kicks in,

58
00:03:07,540 --> 00:03:09,930
this loop execution will finish

59
00:03:09,930 --> 00:03:12,760
and the overall function execution will finish.

60
00:03:12,760 --> 00:03:15,000
And that's what I want.

61
00:03:15,000 --> 00:03:18,150
I don't wanna look at the other restaurants as well

62
00:03:18,150 --> 00:03:21,640
if I already found the restaurant I'm interested in.

63
00:03:21,640 --> 00:03:23,680
That's why I add return here

64
00:03:23,680 --> 00:03:26,670
so that I stop the function execution.

65
00:03:26,670 --> 00:03:29,820
Not primarily because I wanna return a value,

66
00:03:29,820 --> 00:03:33,993
but simply because return also stops function execution.

67
00:03:35,810 --> 00:03:38,470
And then, to this restaurant detail template,

68
00:03:38,470 --> 00:03:42,740
I wanna pass this restaurant, which we last looked at,

69
00:03:42,740 --> 00:03:47,220
because that is the restaurant that has all the data

70
00:03:47,220 --> 00:03:49,323
which I wanna show on this detail page.

71
00:03:50,200 --> 00:03:54,930
So then, here, in this object which we pass to render,

72
00:03:54,930 --> 00:03:57,113
I'll pass in a restaurant key.

73
00:03:58,160 --> 00:04:01,620
Because in restaurant-detail.ejs,

74
00:04:01,620 --> 00:04:05,260
I am referring to that restaurant key here.

75
00:04:05,260 --> 00:04:07,890
If you chose a different name here,

76
00:04:07,890 --> 00:04:11,463
then you also wanna pass a different name here.

77
00:04:12,380 --> 00:04:14,890
But here, I did use restaurants, so I'll stick to it.

78
00:04:14,890 --> 00:04:17,892
And on the right side, I also have restaurant.

79
00:04:19,980 --> 00:04:21,620
Now, again, this can be confusing,

80
00:04:21,620 --> 00:04:26,620
but this restaurant here is this constant restaurant.

81
00:04:26,710 --> 00:04:29,770
So it's different for every loop iteration,

82
00:04:29,770 --> 00:04:34,210
and the concrete restaurant I am passing in here

83
00:04:34,210 --> 00:04:36,900
is the restaurant which we identified,

84
00:04:36,900 --> 00:04:40,230
so which made it through this check here.

85
00:04:40,230 --> 00:04:42,530
So it's the restaurant where the ID

86
00:04:42,530 --> 00:04:47,077
matches the restaurant ID here, which we have in our URL.

87
00:04:50,440 --> 00:04:52,640
This restaurant here, on the other hand,

88
00:04:52,640 --> 00:04:54,640
on the left side of this colon,

89
00:04:54,640 --> 00:04:58,450
that, as I just explained, is simply restaurant

90
00:04:58,450 --> 00:05:02,300
because I chose the key restaurant in the template.

91
00:05:02,300 --> 00:05:04,740
If I would've chosen a different name here,

92
00:05:04,740 --> 00:05:08,343
then we also would've chosen a different key here.

93
00:05:09,440 --> 00:05:12,520
So I'm just repeating this because it is important

94
00:05:12,520 --> 00:05:16,173
that this is not confusing, but pretty clear instead.

95
00:05:17,680 --> 00:05:19,770
Now, I'll auto format this code,

96
00:05:19,770 --> 00:05:22,050
and with that, we're passing restaurant data

97
00:05:22,050 --> 00:05:23,143
into the template.

98
00:05:24,020 --> 00:05:27,330
So now, in the template, besides outputting the name,

99
00:05:27,330 --> 00:05:29,520
we can also output all the other data

100
00:05:29,520 --> 00:05:32,460
and output the restaurant cuisine here,

101
00:05:32,460 --> 00:05:35,810
and the the restaurant address here,

102
00:05:35,810 --> 00:05:38,670
and then here also the description,

103
00:05:38,670 --> 00:05:40,803
with restaurant.description,

104
00:05:41,740 --> 00:05:44,360
and all these keys, which I'm using here,

105
00:05:44,360 --> 00:05:47,660
of course, are the value keys which you find here

106
00:05:47,660 --> 00:05:49,823
in this JSON document.

107
00:05:50,680 --> 00:05:52,240
So that's what you find here,

108
00:05:52,240 --> 00:05:55,493
and that's what I'm accessing in restaurant-detail.ejs.

109
00:05:56,340 --> 00:05:59,640
And therefore, here as a value for the ref attribute,

110
00:05:59,640 --> 00:06:02,253
I also wanna have restaurant.website.

111
00:06:04,750 --> 00:06:07,790
Now, with that, if you again save all the files,

112
00:06:07,790 --> 00:06:10,960
you should be able to reload this page

113
00:06:10,960 --> 00:06:13,473
and find the concrete restaurant data,

114
00:06:14,740 --> 00:06:17,930
and that should then work for all the restaurants.

115
00:06:17,930 --> 00:06:19,930
So if you access different restaurants,

116
00:06:19,930 --> 00:06:21,960
you now see different data here.

117
00:06:21,960 --> 00:06:24,523
You see the detailed data for that restaurant.

118
00:06:25,450 --> 00:06:27,410
And now, the advantage

119
00:06:27,410 --> 00:06:29,780
to this overall list which we have here

120
00:06:29,780 --> 00:06:32,820
is that you can now view the details of a restaurant

121
00:06:32,820 --> 00:06:34,520
on a standalone page,

122
00:06:34,520 --> 00:06:37,400
and that you then could grab that link here,

123
00:06:37,400 --> 00:06:39,570
if this would be running on a real server

124
00:06:39,570 --> 00:06:43,170
exposed to the World Wide Web, which is not the case yet,

125
00:06:43,170 --> 00:06:45,680
but which we can do later in the course,

126
00:06:45,680 --> 00:06:48,990
then you could grab that link and share it with other people

127
00:06:48,990 --> 00:06:51,383
to point them at one specific restaurant.

128
00:06:52,290 --> 00:06:53,780
And this is possible

129
00:06:53,780 --> 00:06:57,360
because of this dynamic route feature which we added

130
00:06:57,360 --> 00:07:01,040
combined with a unique identification criteria,

131
00:07:01,040 --> 00:07:03,530
in this case, this restaurant ID,

132
00:07:03,530 --> 00:07:06,570
which we are also generating dynamically

133
00:07:06,570 --> 00:07:10,123
whenever a new restaurant gets posted, gets added.

