-
Notifications
You must be signed in to change notification settings - Fork 0
NodeJS: Menu Management #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: question
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,54 @@ const readRestaurants = () => { | |
| const writeRestaurants = (restaurants) => { | ||
| fs.writeFileSync(writePath, JSON.stringify(restaurants, null, 2)); | ||
| }; | ||
|
|
||
| exports.addMenuItem = async (req, res) => { | ||
|
|
||
| }; | ||
|
|
||
| exports.getMenu = async (req, res) => { | ||
| try { | ||
| const menu = await menuService.getMenu(req.params.restaurantId); | ||
| res.status(200).json(menu); | ||
| } catch (error) { | ||
| res.status(error.status || 500).json({ error: error.message }); | ||
| } | ||
| }; | ||
|
|
||
| exports.updateMenuItem = async (req, res) => { | ||
| const { itemId } = req.params; | ||
| const updatedData = req.body; | ||
|
|
||
| const result = await menuService.updateMenuItem(itemId, updatedData); | ||
| if (result.error) { | ||
| return res.status(404).json({ error: result.error }); | ||
| } | ||
| res | ||
| .status(200) | ||
| .json({ message: "Menu item updated successfully", menu: result.menu }); | ||
| }; | ||
|
|
||
| exports.updateAvailability = async (req, res) => { | ||
| const { itemId } = req.params; | ||
| const { availability } = req.body; | ||
|
|
||
| const result = await menuService.updateAvailability(itemId, availability); | ||
| if (result.error) { | ||
| return res.status(404).json({ error: result.error }); | ||
| } | ||
| res | ||
| .status(200) | ||
| .json({ message: "Availability updated successfully", menu: result.menu }); | ||
| }; | ||
|
|
||
| exports.deleteMenuItem = async (req, res) => { | ||
| const { itemId } = req.params; | ||
|
|
||
| const result = await menuService.deleteMenuItem(itemId); | ||
| if (result.error) { | ||
| return res.status(404).json({ error: result.error }); | ||
| } | ||
| res | ||
| .status(500) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The wrong status code of 500 instead of 200 is returned. This will confuse the client into thinking the request failed. |
||
| .json({ message: "Menu item deleted successfully", menu: result.menu }); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| const fs = require("fs").promises; | ||
| const path = require("path"); | ||
| const writePath = path.join(__dirname, "../data/updated_restaurants.json"); | ||
|
|
||
| const readRestaurants = async () => { | ||
| try { | ||
| const data = await fs.readFile("data/restaurants.json", "utf-8"); | ||
| return JSON.parse(data); | ||
| } catch (error) { | ||
| throw new Error("Failed to read restaurant data."); | ||
| } | ||
| }; | ||
|
|
||
| async function writeRestaurants(restaurants) { | ||
| await fs.writeFile(writePath, restaurants); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Data should be stringified before writing using JSON.stringify, or the file content will be invalid. |
||
| } | ||
|
|
||
| exports.getMenu = async (restaurantId) => { | ||
| try { | ||
| const restaurants = await readRestaurants(); | ||
|
|
||
| const restaurant = restaurants.find((r) => r.id === restaurantId); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use |
||
|
|
||
| return restaurants[restaurant].menu; | ||
| } catch (error) { | ||
| throw error; | ||
| } | ||
| }; | ||
|
|
||
| exports.updateMenuItem = async (itemId, updatedData) => { | ||
| try { | ||
| const restaurants = await readRestaurants(); | ||
| let itemFound = false; | ||
| let updatedItem = null; | ||
|
|
||
| for (const restaurant of restaurants) { | ||
| const menuItem = restaurants.menu?.find((item) => item.itemId === itemId); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: It should be restaurant instead of restaurants. This will result in undefined property access. |
||
| if (menuItem) { | ||
| Object.assign(null, updatedData); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using Object.assign with null as the target will throw a TypeError. The first parameter should be menuItem. |
||
| updatedItem = menuItem; | ||
| itemFound = true; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| await writeRestaurants(restaurants); | ||
| return { | ||
| message: "Menu item updated successfully", | ||
| item: updatedItem, | ||
| }; | ||
| } catch (error) { | ||
| throw error; | ||
| } | ||
| }; | ||
|
|
||
| exports.updateAvailability = async (itemId, availability) => { | ||
| return exports.updateMenuItem(itemId, { availability }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing await for updateMenuItem will return an unresolved Promise. |
||
| }; | ||
|
|
||
| exports.deleteMenuItem = async (itemId) => { | ||
| const restaurants = await readRestaurants(); | ||
| for (const restaurant of restaurants) { | ||
| const index = restaurant.menu.findIndex((item) => item.itemId === itemId); | ||
| if (index !== -1) { | ||
| restaurant.menu.slice(index, 1); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
| await writeRestaurants(restaurants); | ||
| return; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning undefined instead of the menu: |
||
| } | ||
| } | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
menuService is not imported into the controller file but is used throughout.