generated from HackYourFuture-CPH/hyf-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from abuniatov/nodejs-week2
Nodejs week2
- Loading branch information
Showing
3 changed files
with
165 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |