diff --git a/api/src/index.js b/api/src/index.js index 6a16b71..3875600 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(); @@ -52,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); } @@ -67,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); } @@ -75,17 +79,21 @@ 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 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 diff --git a/api/src/routers/meals.js b/api/src/routers/meals.js new file mode 100644 index 0000000..b2d6b04 --- /dev/null +++ b/api/src/routers/meals.js @@ -0,0 +1,73 @@ +import express from "express"; +import knex from "../database_client.js"; + +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); + } +}); + +// 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 router; \ No newline at end of file diff --git a/api/src/routers/reservations.js b/api/src/routers/reservations.js new file mode 100644 index 0000000..e66612d --- /dev/null +++ b/api/src/routers/reservations.js @@ -0,0 +1,79 @@ +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); + 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;