-
Notifications
You must be signed in to change notification settings - Fork 0
Nodejs week3 #3
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
Open
siderdk
wants to merge
3
commits into
main
Choose a base branch
from
nodejs-week3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Nodejs week3 #3
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,146 @@ | ||
| import express from "express"; | ||
| import knex from "../database_client.js"; | ||
| import bodyParser from "body-parser"; | ||
|
|
||
| const mealsRouter = express.Router(); | ||
|
|
||
| mealsRouter.use(express.json()); | ||
| mealsRouter.use(bodyParser.json()); | ||
| mealsRouter.use(bodyParser.urlencoded({ extended: true })); | ||
|
|
||
| // Get all meals | ||
| mealsRouter.get("/", async (req, res) => { | ||
| let query = knex("meal"); | ||
|
|
||
| const { maxPrice, availableReservations, title, dateAfter, dateBefore, limit, sortKey, sortDir } = req.query; | ||
|
|
||
| try { | ||
| if (maxPrice) { | ||
| query = query.where("price", "<=", maxPrice); | ||
| } | ||
|
|
||
| if (title) { | ||
| query = query.where("title", "like", `%${title}%`); | ||
| } | ||
|
|
||
| if (dateAfter) { | ||
| query = query.where("when", ">=", dateAfter); | ||
| } | ||
|
|
||
| if (dateBefore) { | ||
| query = query.where("when", "<=", dateBefore); | ||
| } | ||
|
|
||
| if (availableReservations) { | ||
| if (availableReservations === "true") { | ||
| query = query.whereExists( | ||
| knex("reservation") | ||
| .select("meal_id") | ||
| .whereRaw("meal.id = reservation.meal_id") | ||
| .groupBy("reservation.meal_id") | ||
| .havingRaw("meal.max_reservations > COUNT(reservation.id)") | ||
| ); | ||
| } else { | ||
| query = query.whereNotExists( | ||
| knex("reservation") | ||
| .select("meal_id") | ||
| .whereRaw("meal.id = reservation.meal_id") | ||
| .groupBy("reservation.meal_id") | ||
| .havingRaw("meal.max_reservations > COUNT(reservation.id)") | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| if (sortKey && ["when", "max_reservations", "price"].includes(sortKey)) { | ||
| query = query.orderBy(sortKey, sortDir === "desc" ? "desc" : "asc"); | ||
| } | ||
|
|
||
| if (limit) { | ||
| query = query.limit(Number(limit)); | ||
| } | ||
|
|
||
| const meals = await query; | ||
| res.json({ meals }); | ||
| } catch (error) { | ||
| console.error("Error fetching meals:", error); | ||
| res.status(500).json({ message: "Error fetching meals" }); | ||
| } | ||
| }); | ||
|
|
||
|
|
||
| //Adds a new meal to the database | ||
| mealsRouter.post("/", async (req, res) => { | ||
| const { title, description, location, when, max_reservations, price, created_date } = req.body; | ||
| if (!title || !description || !when || !max_reservations || !price || !created_date) { | ||
| return res.status(400).json({ message: "Please provide all fields" }); | ||
| } | ||
| try { | ||
| // Insert the new meal | ||
| const insertQuery = ` | ||
| INSERT INTO meal (title, description, location, when, max_reservations, price, created_date) | ||
| VALUES (?, ?, ?, ?, ?, ?, ?) | ||
| `; | ||
|
|
||
| await knex.raw(insertQuery, [title, description, location, when, max_reservations, price, created_date]); | ||
|
|
||
| // Get the ID of the last inserted record using LAST_INSERT_ID() | ||
| const result = await knex.raw('SELECT LAST_INSERT_ID() AS meal_id'); | ||
| const mealId = result[0][0].meal_id; | ||
|
|
||
| // Retrieve the inserted meal using the mealId | ||
| const meal = await knex.raw('SELECT * FROM meal WHERE id = ?', [mealId]); | ||
|
|
||
|
|
||
| return res.status(201).json({ meal }); | ||
| } catch (error) { | ||
| console.error("Error inserting meal:", error); | ||
| return res.status(500).json({ message: "Error adding meal" }); | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| //get meal by id | ||
| mealsRouter.get("/:id", async (req, res) => { | ||
| const { id } = req.params; | ||
| const meal = await knex.raw("SELECT * FROM meal WHERE id = ?", [id]); | ||
| if (meal[0].length === 0) { | ||
| return res.status(404).json({ message: "Meal not found" }); | ||
| } | ||
| res.json({ meal: meal[0][0] }); | ||
| }); | ||
|
|
||
| // update the meal by id | ||
| mealsRouter.put("/:id", async (req, res) => { | ||
| const { id } = req.params; | ||
| const { title, description, location, when, max_reservations, price, created_date } = req.body; | ||
| if (!title || !description || !when || !max_reservations || !price || !created_date) { | ||
| return res.status(400).json({ message: "Please provide all fields" }); | ||
| } | ||
| try { | ||
| const updateQuery = ` | ||
| UPDATE meal | ||
| SET title = ?, description = ?, location = ?, when = ?, max_reservations = ?, price = ?, created_date = ? | ||
| WHERE id = ? | ||
| `; | ||
| await knex.raw(updateQuery, [title, description, location, when, max_reservations, price, created_date, id]); | ||
| const meal = await knex.raw("SELECT * FROM meal WHERE id = ?", [id]); | ||
| res.json({ meal: meal[0][0] }); | ||
| } catch (error) { | ||
| console.error("Error updating meal:", error); | ||
| return res.status(500).json({ message: "Error updating meal" }); | ||
| } | ||
| }); | ||
|
|
||
| //delete meal by id | ||
| mealsRouter.delete("/:id", async (req, res) => { | ||
| const { id } = req.params; | ||
| const meal = await knex.raw("SELECT * FROM meal WHERE id = ?", [id]); | ||
| if (meal[0].length === 0) { | ||
| return res.status(404).json({ message: "Meal not found" }); | ||
| } | ||
| await knex.raw("DELETE FROM meal WHERE id = ?", [id]); | ||
| res.json({ message: "Meal deleted" }); | ||
| }); | ||
|
|
||
|
|
||
| export default mealsRouter; | ||
This file contains hidden or 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,112 @@ | ||
| import express from "express"; | ||
| import knex from "../database_client.js"; | ||
| import bodyParser from "body-parser"; | ||
|
|
||
|
|
||
| const reservationsRouter = express.Router(); | ||
| reservationsRouter.use(express.json()); | ||
| reservationsRouter.use(bodyParser.json()); | ||
| reservationsRouter.use(bodyParser.urlencoded({ extended: true })); | ||
|
|
||
| // Get all reservations | ||
| reservationsRouter.get("/", async (req, res) => { | ||
| const reservations = await knex.select("*").from("reservation"); | ||
| res.json({ reservations }); | ||
| }); | ||
|
|
||
| //Adds a new reservation to the database | ||
| reservationsRouter.post("/", async (req, res) => { | ||
| const { number_of_guests, meal_id, contact_phone_number, contact_name, contact_email } = req.body; | ||
|
|
||
| if (!number_of_guests || !meal_id || !contact_phone_number || !contact_name || !contact_email) { | ||
| return res.status(400).json({ message: "Please provide all fields" }); | ||
| } | ||
| try { | ||
| const created_date = new Date().toISOString().slice(0, 10); | ||
| // Insert the new reservation | ||
| const [reservation] = await knex('reservation') | ||
| .insert({ | ||
| number_of_guests, | ||
| meal_id, | ||
| contact_phone_number, | ||
| contact_name, | ||
| contact_email, | ||
| created_date | ||
| }) | ||
| .returning('*'); | ||
|
|
||
| return res.status(201).json({ reservation }); | ||
|
|
||
| } catch (error) { | ||
| console.error("Error inserting reservation:", error); | ||
| return res.status(500).json({ message: "Error adding reservation" }); | ||
| } | ||
|
|
||
| }); | ||
|
|
||
|
|
||
| //get reservation by id | ||
| reservationsRouter.get("/:id", async (req, res) => { | ||
| const { id } = req.params; | ||
| const reservation = await knex('reservation').where({ id }); | ||
| if (reservation.length === 0) { | ||
| return res.status(404).json({ message: "Reservation not found" }); | ||
| } | ||
| res.json({ reservation: reservation[0] }); | ||
| }); | ||
|
|
||
| // update the reservation by id | ||
| reservationsRouter.put("/:id", async (req, res) => { | ||
| const { id } = req.params; | ||
| const { number_of_guests, meal_id, contact_phone_number, contact_name, contact_email } = req.body; | ||
|
|
||
| if (!number_of_guests || !meal_id || !contact_phone_number || !contact_name || !contact_email) { | ||
| return res.status(400).json({ message: "Please provide all fields" }); | ||
| } | ||
|
|
||
| try { | ||
| const updated_date = new Date().toISOString().slice(0, 10); | ||
| // Update the reservation | ||
| const [reservation] = await knex('reservation') | ||
| .where({ id }) | ||
| .update({ | ||
| number_of_guests, | ||
| meal_id, | ||
| contact_phone_number, | ||
| contact_name, | ||
| contact_email, | ||
| updated_date | ||
| }) | ||
| .returning('*'); | ||
|
|
||
| res.json({ reservation }); | ||
|
|
||
| } catch (error) { | ||
| console.error("Error updating reservation:", error); | ||
| return res.status(500).json({ message: "Error updating reservation" }); | ||
| } | ||
| }); | ||
|
|
||
| //delete reservation by id | ||
| reservationsRouter.delete("/:id", async (req, res) => { | ||
| const { id } = req.params; | ||
| try { | ||
| const reservation = await knex('reservation').where({ id }).first(); | ||
|
|
||
| if (!reservation) { | ||
| return res.status(404).json({ message: "Reservation not found" }); | ||
| } | ||
|
|
||
| await knex('reservation').where({ id }).del(); | ||
| res.json({ message: "Reservation deleted successfully" }); | ||
|
|
||
| } catch (error) { | ||
| console.error("Error deleting reservation:", error); | ||
| res.status(500).json({ message: "Error deleting reservation" }); | ||
| } | ||
| }); | ||
|
|
||
|
|
||
|
|
||
|
|
||
| export default reservationsRouter; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Hell yeah!