From bf5c30733c3e5096398f5923de1e8b4a183fbb6e Mon Sep 17 00:00:00 2001 From: abuniatov Date: Sat, 5 Oct 2024 22:39:19 +0200 Subject: [PATCH 1/6] added meals router reservations router files --- api/src/routers/meals.js | 0 api/src/routers/reservations.js | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 api/src/routers/meals.js create mode 100644 api/src/routers/reservations.js diff --git a/api/src/routers/meals.js b/api/src/routers/meals.js new file mode 100644 index 0000000..e69de29 diff --git a/api/src/routers/reservations.js b/api/src/routers/reservations.js new file mode 100644 index 0000000..e69de29 From 4bb98be6e43232e18f6f9f302febe589d7be0869 Mon Sep 17 00:00:00 2001 From: abuniatov Date: Sat, 5 Oct 2024 23:17:51 +0200 Subject: [PATCH 2/6] created meals routes --- api/src/routers/meals.js | 71 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/api/src/routers/meals.js b/api/src/routers/meals.js index e69de29..65972a3 100644 --- a/api/src/routers/meals.js +++ b/api/src/routers/meals.js @@ -0,0 +1,71 @@ +import express from "express"; +import knex from "../database_client.js"; + +const mealsRouter = express.Router(); + +// Returns all meals +router.get("/", async (req, res, next) => { + try { + const allMeals = await knex("meal"); + res.json(allMeals); + } catch (error) { + next(error); + } +}); + +// Insert new meal +router.post("/", async (req, res, next) => { + try { + const newMeal = await knex("meal").insert(req.body); + res.json(newMeal); + } catch (error) { + next(error); + } +}); + +// Return the meal by id +router.get("/:id", async (req, res, next) => { + try { + const meal = await knex("meal").where("id", req.params.id).first(); + if (!meal) { + const error = new Error("Meal not found"); + error.status = 404; + return next(error); + } + res.json(meal); + } catch (error) { + next(error); + } +}); + +// Update the meal by id +router.put("/:id", async (req, res, next) => { + try { + const updatedMeal = await knex("meal").where("id", req.params.id).update(req.body); + if (!updatedMeal) { + const error = new Error("Meal not found"); + error.status = 404; + return next(error); + } + res.status(200).json({ message: "Meal updated successfully" }); + } catch (error) { + next(error); + } +}); + +// Delete the meal by id +router.delete("/:id", async (req, res, next) => { + try { + const deletedMeal = await knex("meal").where("id", req.params.id).del(); + if (!deletedMeal) { + const error = new Error("Meal not found"); + error.status = 404; + return next(error); + } + res.status(200).json({ message: "Meal deleted successfully" }); + } catch (error) { + next(error); + } +}); + +export default mealsRouter; \ No newline at end of file From 49355ae038949a11d74adccb5a20c88234496fff Mon Sep 17 00:00:00 2001 From: abuniatov Date: Sat, 5 Oct 2024 23:40:54 +0200 Subject: [PATCH 3/6] fixed router name --- api/src/routers/meals.js | 90 ++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 44 deletions(-) diff --git a/api/src/routers/meals.js b/api/src/routers/meals.js index 65972a3..b2d6b04 100644 --- a/api/src/routers/meals.js +++ b/api/src/routers/meals.js @@ -1,71 +1,73 @@ import express from "express"; import knex from "../database_client.js"; -const mealsRouter = express.Router(); +const router = express.Router(); // Returns all meals router.get("/", async (req, res, next) => { - try { - const allMeals = await knex("meal"); - res.json(allMeals); - } catch (error) { - next(error); - } + try { + const allMeals = await knex("meal"); + res.json(allMeals); + } catch (error) { + next(error); + } }); // Insert new meal router.post("/", async (req, res, next) => { - try { - const newMeal = await knex("meal").insert(req.body); - res.json(newMeal); - } catch (error) { - next(error); - } + try { + const newMeal = await knex("meal").insert(req.body); + res.json(newMeal); + } catch (error) { + next(error); + } }); // Return the meal by id router.get("/:id", async (req, res, next) => { - try { - const meal = await knex("meal").where("id", req.params.id).first(); - if (!meal) { - const error = new Error("Meal not found"); - error.status = 404; - return next(error); - } - res.json(meal); - } catch (error) { - next(error); + try { + const meal = await knex("meal").where("id", req.params.id).first(); + if (!meal) { + const error = new Error("Meal not found"); + error.status = 404; + return next(error); } + res.json(meal); + } catch (error) { + next(error); + } }); // Update the meal by id router.put("/:id", async (req, res, next) => { - try { - const updatedMeal = await knex("meal").where("id", req.params.id).update(req.body); - if (!updatedMeal) { - const error = new Error("Meal not found"); - error.status = 404; - return next(error); - } - res.status(200).json({ message: "Meal updated successfully" }); - } catch (error) { - next(error); + try { + const updatedMeal = await knex("meal") + .where("id", req.params.id) + .update(req.body); + if (!updatedMeal) { + const error = new Error("Meal not found"); + error.status = 404; + return next(error); } + res.status(200).json({ message: "Meal updated successfully" }); + } catch (error) { + next(error); + } }); // Delete the meal by id router.delete("/:id", async (req, res, next) => { - try { - const deletedMeal = await knex("meal").where("id", req.params.id).del(); - if (!deletedMeal) { - const error = new Error("Meal not found"); - error.status = 404; - return next(error); - } - res.status(200).json({ message: "Meal deleted successfully" }); - } catch (error) { - next(error); + try { + const deletedMeal = await knex("meal").where("id", req.params.id).del(); + if (!deletedMeal) { + const error = new Error("Meal not found"); + error.status = 404; + return next(error); } + res.status(200).json({ message: "Meal deleted successfully" }); + } catch (error) { + next(error); + } }); -export default mealsRouter; \ No newline at end of file +export default router; \ No newline at end of file From 541adff0db69e989e14bba0709f73b9a2b488944 Mon Sep 17 00:00:00 2001 From: abuniatov Date: Sun, 6 Oct 2024 01:59:08 +0200 Subject: [PATCH 4/6] added reservation routers and updated index.js --- api/src/index.js | 5 ++++- api/src/routers/reservations.js | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/api/src/index.js b/api/src/index.js index 6a16b71..8451a03 100644 --- a/api/src/index.js +++ b/api/src/index.js @@ -4,10 +4,14 @@ import cors from "cors"; import bodyParser from "body-parser"; import knex from "./database_client.js"; import nestedRouter from "./routers/nested.js"; +import mealsRouter from "./routers/meals.js"; +import reservationsRouter from "./routers/reservations.js"; const app = express(); app.use(cors()); app.use(bodyParser.json()); +app.use("/api/meals", mealsRouter); +app.use("/api/reservations", reservationsRouter); const apiRouter = express.Router(); @@ -75,7 +79,6 @@ app.get("/meals/last-meal", async (req, res, next) => { // This nested router example can also be replaced with your own sub-router apiRouter.use("/nested", nestedRouter); - app.use("/api", apiRouter); // Error handling middleware diff --git a/api/src/routers/reservations.js b/api/src/routers/reservations.js index e69de29..4607193 100644 --- a/api/src/routers/reservations.js +++ b/api/src/routers/reservations.js @@ -0,0 +1,25 @@ +import express from "express"; +import knex from "../database_client.js"; + +const router = express.Router(); + +// Return all reservations +router.get("/", async (req, res, next) => { + try { + const allReservations = await knex("reservation"); + res.json(allReservations); + } catch (error) { + next(error); + } +}); + +// Insert new reservation +router.post("/", async (req, res, next) => { + try { + const newReservation = await knex("reservation").insert(req.body); + res.json(newReservation); + } catch (error) { + next(error); + } +}); + From 45c578ef0f14d450a9ae04d16839be8757c13d7e Mon Sep 17 00:00:00 2001 From: abuniatov Date: Sun, 6 Oct 2024 02:31:18 +0200 Subject: [PATCH 5/6] created endpoint for reservations --- api/src/routers/reservations.js | 76 ++++++++++++++++++++++++++++----- 1 file changed, 65 insertions(+), 11 deletions(-) diff --git a/api/src/routers/reservations.js b/api/src/routers/reservations.js index 4607193..e66612d 100644 --- a/api/src/routers/reservations.js +++ b/api/src/routers/reservations.js @@ -5,21 +5,75 @@ const router = express.Router(); // Return all reservations router.get("/", async (req, res, next) => { - try { - const allReservations = await knex("reservation"); - res.json(allReservations); - } catch (error) { - next(error); - } + try { + const allReservations = await knex("reservation"); + res.json(allReservations); + } catch (error) { + next(error); + } }); // Insert new reservation router.post("/", async (req, res, next) => { - try { - const newReservation = await knex("reservation").insert(req.body); - res.json(newReservation); - } catch (error) { - next(error); + try { + const newReservation = await knex("reservation").insert(req.body); + const [reservationId] = newReservation; + res.status(201).json({ message: "Reservation created", id: reservationId }); + } catch (error) { + next(error); + } +}); + +// Return the reservation by id +router.get("/:id", async (req, res, next) => { + try { + const reservation = await knex("reservation") + .where("id", req.params.id) + .first(); + if (reservation) { + res.json(reservation); + } else { + res.status(404).json({ message: "Reservation not found" }); + } + } catch (error) { + next(error); + } +}); + +// Update the reservation by id +router.put("/:id", async (req, res, next) => { + try { + const id = req.params.id; + const updatedReservation = req.body; + const result = await knex("Reservation") + .where("id", id) + .update(updatedReservation); + + if (result) { + res.status(200).json({ message: "Reservation updated successfully" }); + } else { + res.status(404).json({ message: "Reservation not found" }); + } + } catch (error) { + next(error); + } +}); + +// Delete the reservation by id +router.delete("/:id", async (req, res, next) => { + try { + const deletedReservation = await knex("reservation") + .where("id", req.params.id) + .del(); + if (!deletedReservation) { + const error = new Error("Reservation not found"); + error.status = 404; + return next(error); } + res.status(200).json({ message: "Reservation deleted successfully" }); + } catch (error) { + next(error); + } }); +export default router; From 4e619681e7025c812d22fd3ace3699420a0f5975 Mon Sep 17 00:00:00 2001 From: abuniatov Date: Wed, 23 Oct 2024 19:13:30 +0200 Subject: [PATCH 6/6] adressed mentor's comments: updated firstMeal and lastMeal to correctly return the meal objects. Updated error handling middleware. --- api/src/index.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/api/src/index.js b/api/src/index.js index 8451a03..3875600 100644 --- a/api/src/index.js +++ b/api/src/index.js @@ -56,7 +56,7 @@ app.get("/meals/first-meal", async (req, res, next) => { error.status = 404; return next(error); } - res.json(meal); + res.json(firstMeal); } catch (error) { next(error); } @@ -71,7 +71,7 @@ app.get("/meals/last-meal", async (req, res, next) => { error.status = 404; return next(error); } - res.json(meal); + res.json(lastMeal); } catch (error) { next(error); } @@ -84,11 +84,16 @@ app.use("/api", apiRouter); // Error handling middleware app.use((err, req, res, next) => { console.error(err.stack); + + if (err.status === 404) { + return res.status(404).json({ error: "Resource not found" }); + } + res.status(err.status || 500).json({ - error: err.message || "Internal Server Error", + error: "An error occurred. Please try again later.", }); }); app.listen(process.env.PORT, () => { console.log(`API listening on port ${process.env.PORT}`); -}); +}); \ No newline at end of file