Skip to content

Commit

Permalink
Merge pull request #2 from abuniatov/nodejs-week2
Browse files Browse the repository at this point in the history
Nodejs week2
  • Loading branch information
abuniatov authored Oct 23, 2024
2 parents e6d4a88 + 4e61968 commit 453dbde
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 5 deletions.
18 changes: 13 additions & 5 deletions api/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
}
Expand All @@ -67,25 +71,29 @@ 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);
}
});

// 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}`);
});
});
73 changes: 73 additions & 0 deletions api/src/routers/meals.js
Original file line number Diff line number Diff line change
@@ -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;
79 changes: 79 additions & 0 deletions api/src/routers/reservations.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 453dbde

Please sign in to comment.