Were you able to translate this ID
into a complete data object from that restaurants.json file?
In the end, what we gotta do
is we have to read that JSON file,
as we do it in other routes as well,
and then we have to search
for one specific restaurant by ID.
And we can do that
so that we find the restaurant with that ID.
And therefore, I'll start by reading that file.
For this, we can again copy that code,
which we use in other routes as well,
copy that, and add that here.
Now, we got access to the storedRestaurants.
And now, that will be an array
where only one restaurant has that ID.
So now, we wanna look at all the restaurants,
and once we've found the one restaurant with that ID,
we don't wanna continue looking at the others,
but instead, then we wanna return our response
with that restaurant data.
And to do this, we can again use a good old for loop,
what we did before in this course as well,
because we wanna look at all restaurants
and find one specific restaurant,
and whenever you wanna do something
on all the items in an array,
a for loop isn't a bad choice.
So here, I'll again then go through all my restaurants
in this storedRestaurants array
with a for of loop, as you learned it.
And then, in this for of loop,
I wanna have an if statement,
which is executed for every restaurant therefore,
where I compare the restaurant ID,
so of the restaurant
we are currently looking at in this loop,
with the restaurant ID I got in my URL.
So the restaurant ID I got in my path here.
So I'll compare this restaurantId, like this.
And if I do that,
then I know that if I make it into this if statement,
so if this condition is met,
then I've found the correct restaurant, otherwise I haven't.
Now, once I did find the correct restaurant,
I wanna render my response.
I wanna render the restaurant-detail template and finish.
Now, since I wanna finish at this point,
I will return here,
because we did learn that the return keyword
does return a value to the place
where a function was executed,
which is not too important for us here
because we are not executing this function on our own,
express does that for us instead,
but return also finishes this function execution.
So when this return statement here kicks in,
this loop execution will finish
and the overall function execution will finish.
And that's what I want.
I don't wanna look at the other restaurants as well
if I already found the restaurant I'm interested in.
That's why I add return here
so that I stop the function execution.
Not primarily because I wanna return a value,
but simply because return also stops function execution.
And then, to this restaurant detail template,
I wanna pass this restaurant, which we last looked at,
because that is the restaurant that has all the data
which I wanna show on this detail page.
So then, here, in this object which we pass to render,
I'll pass in a restaurant key.
Because in restaurant-detail.ejs,
I am referring to that restaurant key here.
If you chose a different name here,
then you also wanna pass a different name here.
But here, I did use restaurants, so I'll stick to it.
And on the right side, I also have restaurant.
Now, again, this can be confusing,
but this restaurant here is this constant restaurant.
So it's different for every loop iteration,
and the concrete restaurant I am passing in here
is the restaurant which we identified,
so which made it through this check here.
So it's the restaurant where the ID
matches the restaurant ID here, which we have in our URL.
This restaurant here, on the other hand,
on the left side of this colon,
that, as I just explained, is simply restaurant
because I chose the key restaurant in the template.
If I would've chosen a different name here,
then we also would've chosen a different key here.
So I'm just repeating this because it is important
that this is not confusing, but pretty clear instead.
Now, I'll auto format this code,
and with that, we're passing restaurant data
into the template.
So now, in the template, besides outputting the name,
we can also output all the other data
and output the restaurant cuisine here,
and the the restaurant address here,
and then here also the description,
with restaurant.description,
and all these keys, which I'm using here,
of course, are the value keys which you find here
in this JSON document.
So that's what you find here,
and that's what I'm accessing in restaurant-detail.ejs.
And therefore, here as a value for the ref attribute,
I also wanna have restaurant.website.
Now, with that, if you again save all the files,
you should be able to reload this page
and find the concrete restaurant data,
and that should then work for all the restaurants.
So if you access different restaurants,
you now see different data here.
You see the detailed data for that restaurant.
And now, the advantage
to this overall list which we have here
is that you can now view the details of a restaurant
on a standalone page,
and that you then could grab that link here,
if this would be running on a real server
exposed to the World Wide Web, which is not the case yet,
but which we can do later in the course,
then you could grab that link and share it with other people
to point them at one specific restaurant.
And this is possible
because of this dynamic route feature which we added
combined with a unique identification criteria,
in this case, this restaurant ID,
which we are also generating dynamically
whenever a new restaurant gets posted, gets added.