Skip to content
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

All endpoints implemented #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 84 additions & 2 deletions src/backend/api/meals.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,97 @@

const express = require("express");
const router = express.Router();
const knex = require("../database");

//GET - Returns all meals
router.get("/", async (request, response) => {
try {
// knex syntax for selecting things. Look up the documentation for knex for further info
const titles = await knex("meals").select("title");
response.json(titles);
const allMeals = await knex("meal").select("*");
response.json(allMeals);
} catch (error) {
throw error;
}
});

//Post - Adds a new meal to the database
router.post("/", async (request, response) => {
const addMeal = request.body;
addMeal.created_date = new Date();

try {
await knex("meal").insert(addMeal);
response.status(201).json("New meal has been added");
} catch (error) {
console.error(error);
response.status(500).json({ error: "Failed to add a new meal" });
}
});

// Get - /api/meals/:id- Returns the meal by id


router.get("/:id", async (request, response) => {

const { id } = request.params;

try {
// knex syntax for selecting things. Look up the documentation for knex for further info
const meal = await knex("meal").select("*").where({ id }).first();
if (meal) {
response.json(meal);
} else {
response.status(404).json({ error: "Meal not found" });
}
} catch (error) {
console.error(error);
response.status(500).json({ error: "Failed to retrieve the meal" });
}
});

//PUT - /api/meals/:id Updates the meal by id

router.put("/:id", async (request, response) => {
try {
const { id } = request.params;
const updatedMeal = request.body;

const results = await knex("meal")
.update(updatedMeal).where({ id });

if (results) {
return response.json({ message: "Meal updated successfully" });
} else {
return response.status(404).json({ error: "Meal not found" });
}
} catch (error) {
console.log(error);
return response.status(500).json({ error: "Error updating meal" });
}
});


//DELETE - /api/meals/:id Deletes the meal by id

router.delete("/:id", async (request, response) => {
try {
const { id } = request.params;

const meal = await knex("meal").select("*").where({ id }).first();

if (!meal) {
return response.status(404).json({ error: "Meal not found" });
}

await knex("meal").where({ id }).del();

response.json({ message: "Meal deleted successfully" });
} catch (error) {
console.error(error);
response.status(500).json({ error: "Error deleting meal" });
}
});



module.exports = router;
88 changes: 88 additions & 0 deletions src/backend/api/reservations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const express = require("express");
const router = express.Router();
const knex = require("../database");

// GET - Returns all reservations
router.get("/", async (request, response) => {
try {
const allReservations = await knex("reservation").select("*");
response.json(allReservations);
} catch (error) {
console.error(error);
response.status(500).json({ error: "Failed to retrieve reservations" });
}
});

// POST - Adds a new reservation to the database
router.post("/", async (request, response) => {
const addReservation = request.body;
addReservation.created_date = new Date();

try {
await knex("reservation").insert(addReservation);
response.status(201).json("New reservation has been added");
} catch (error) {
console.error(error);
response.status(500).json({ error: "Failed to add a new reservation" });
}
});

// GET - Returns the reservation by id
router.get("/:id", async (request, response) => {
const { id } = request.params;

try {
const reservation = await knex("reservation").select("*").where({ id }).first();
if (reservation) {
response.json(reservation);
} else {
response.status(404).json({ error: "Reservation not found" });
}
} catch (error) {
console.error(error);
response.status(500).json({ error: "Failed to retrieve the reservation" });
}
});

// PUT - Updates the reservation by id
router.put("/:id", async (request, response) => {
const { id } = request.params;

try {
const reservation = await knex("reservation").select("*").where({ id }).first();
if (reservation) {
const updatedReservation = request.body;
const results = await knex("reservation").update(updatedReservation).where({ id });
if (results) {
return response.json({ message: "Reservation updated successfully" });
} else {
return response.status(500).json({ error: "Failed to update reservation" });
}
} else {
response.status(404).json({ error: "Reservation not found" });
}
} catch (error) {
console.error(error);
response.status(500).json({ error: "Failed to update reservation" });
}
});

// DELETE - Deletes the reservation by id
router.delete("/:id", async (request, response) => {
const { id } = request.params;

try {
const reservation = await knex("reservation").select("*").where({ id }).first();
if (!reservation) {
return response.status(404).json({ error: "Reservation not found" });
}

await knex("reservation").where({ id }).del();
response.json({ message: "Reservation deleted successfully" });
} catch (error) {
console.error(error);
response.status(500).json({ error: "Error deleting reservation" });
}
});

module.exports = router;
10 changes: 9 additions & 1 deletion src/backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ const express = require("express");
const app = express();
const router = express.Router();
const path = require("path");

const mealsRouter = require("./api/meals");
const reservationsRouter = require("./api/reservations");
//The body function has changed after version 15 while I am using 16. If put request doesnt work then install bodyparser
//const bodyOarser = require("body-parser");
//npm install --save body-parser


const buildPath = path.join(__dirname, "../../dist");
const port = process.env.PORT || 3000;
const cors = require("cors");
Expand All @@ -20,6 +25,7 @@ app.use(express.json());
app.use(cors());

router.use("/meals", mealsRouter);
router.use("/reservations", reservationsRouter);

if (process.env.API_PATH) {
app.use(process.env.API_PATH, router);
Expand All @@ -32,4 +38,6 @@ app.use("*", (req, res) => {
res.sendFile(path.join(`${buildPath}/index.html`));
});


module.exports = app;