I am specifically stuck on Task 10: Adding to your starred restaurants.
The extra guidance is as follows:
Follow these steps for guidance in creating this endpoint:
- Find the restaurant in the list of starred restaurants.
- If the restaurant doesn’t exist, send a status code to the client to let it know the restaurant was not found.
- Otherwise, proceed with adding a restaurant to your starred restaurants list:
- Generate a unique id for the new starred restaurant.
- Create a record for the new starred restaurant.
- Push the new record into
STARRED_RESTAURANTS
. - Set a success status code and send the restaurant data to the front-end.
You’ll need to use the Express .post()
method to build this endpoint. Check out Feature 3 in the restaurants router in backend/routes/restaurants.js to see how we implemented a similar endpoint.
The code I have implemented is:
router.post("/", (req, res) => {
const { body } = req;
const { id, comment } = body;
//Find the restaurant in ALL_RESTAURANTS
const restaurant = ALL_RESTAURANTS.find(r => r.id === id);
if (!restaurant) {
res.status(404).send("Restaurant not found");
return;
};
// Check if the restaurant is already starred
const alreadyStarred = STARRED_RESTAURANTS.some(r => r.restaurantId === id);
if (alreadyStarred) {
res.status(400).send("Restaurant already starred");
return;
};
// Create a new starred restaurant object
const newStarredRestaurant = {
id: uuidv4(),
name: restaurant.name,
comment: comment || '',
};
// Add the new starred restaurant to the list of starred restaurants.
STARRED_RESTAURANTS.push(newStarredRestaurant);
res.status(201).json(newStarredRestaurant);
});
I believe this code meets the requirements, and the AI assistant even confirmed this for me. I also don’t understand the instructions, as the first step is to check if the restaurant exists in “starred restaurants” and to return an error and stop if it isn’t, which means it would fail unless it already exists, but I am supposed to be adding a new one to the list, so if it doesn’t exist it would just stop and not add it…?
But when I try to click the button to add a restaurant to the starred list, I get “Updating Failed” every time.