recipe_app/backend/backendServer.js

155 lines
4.2 KiB
JavaScript
Raw Normal View History

2025-07-08 10:40:49 -07:00
const express = require("express");
const db = require("./db");
2025-07-15 12:02:11 -07:00
const port = 3000;
2025-07-25 18:07:20 +00:00
const cors = require("cors");
2025-07-08 10:40:49 -07:00
const app = express();
2025-07-25 16:00:55 -07:00
app.use(cors());
2025-07-08 10:40:49 -07:00
app.use(express.json());
2025-07-09 14:43:45 -07:00
// ####### ROUTES #######
2025-07-25 18:07:20 +00:00
app.get("/backend/test", async (req, res) => {
console.log("test");
2025-08-05 18:11:03 +00:00
res.json({ env: process.env.NODE_ENV });
2025-07-15 12:02:11 -07:00
});
2025-07-09 14:43:45 -07:00
// ### GET ALL RECIPES ###
2025-07-25 18:07:20 +00:00
app.get("/backend/recipes", async (req, res) => {
2025-07-08 10:40:49 -07:00
try {
const recipes = await db("recipes").select("id", "name", "cuisine", "stars", "prep_minutes", "cook_minutes");
2025-07-08 10:40:49 -07:00
res.json(recipes);
} catch (err) {
console.log(err);
res.status(500).json({ error: err.message });
}
});
// ### GET ALL RECIPE_INGREDIENTS ###
2025-07-25 18:07:20 +00:00
app.get("/backend/recipe-ingredients", async (req, res) => {
try {
2025-07-25 18:07:20 +00:00
const recipe_ingredients = await db("recipe_ingredients").select("*");
res.json(recipe_ingredients);
} catch (err) {
console.log(err);
res.status(500).json({ error: err.message });
}
});
// ### GET ALL RECIPE_STEPS ###
2025-07-25 18:07:20 +00:00
app.get("/backend/recipe-steps", async (req, res) => {
try {
2025-07-25 18:07:20 +00:00
const recipe_steps = await db("recipe_steps").select("*");
res.json(recipe_steps);
} catch (err) {
console.log(err);
res.status(500).json({ error: err.message });
}
});
2025-07-08 10:40:49 -07:00
2025-07-09 14:43:45 -07:00
// ### GET RECIPE ###
2025-07-25 18:07:20 +00:00
app.get("/backend/recipe/:id", async (req, res) => {
const id = req.params.id;
2025-07-08 10:40:49 -07:00
try {
2025-07-25 18:07:20 +00:00
const recipeQuery = db("recipes")
.where("id", "=", id)
.select("id", "name", "author", "cuisine", "stars", "prep_minutes", "cook_minutes");
2025-07-25 18:07:20 +00:00
const ingredientsQuery = db
.from("recipe_ingredients")
.where("recipe_id", "=", id)
.select("raw");
2025-07-25 18:07:20 +00:00
const stepsQuery = db("recipe_steps")
.where("recipe_id", id)
.select("step_number", "instruction");
2025-07-25 18:07:20 +00:00
const [recipe, ingredients, steps] = await Promise.all([
recipeQuery,
ingredientsQuery,
stepsQuery,
]);
const result = {
2025-07-09 11:02:23 -07:00
details: recipe[0],
ingredients: ingredients,
2025-07-25 18:07:20 +00:00
steps: steps,
};
res.json(result);
2025-07-08 10:40:49 -07:00
} catch (err) {
console.log(err);
res.status(500).json({ error: err.message });
}
});
2025-07-09 14:43:45 -07:00
// ### ADD RECIPE ###
2025-07-25 18:07:20 +00:00
app.post("/backend/add-recipe", async (req, res) => {
2025-08-05 18:11:03 +00:00
if (process.env.NODE_ENV === 'demo') { return; };
const { name, author, cuisine, stars, ingredients, steps, prep_minutes, cook_minutes } = req.body;
2025-07-08 10:40:49 -07:00
try {
2025-07-25 18:07:20 +00:00
const [id] = await db("recipes").insert(
{
name: name,
author: author,
cuisine: cuisine,
prep_minutes: prep_minutes,
cook_minutes: cook_minutes,
2025-07-25 18:07:20 +00:00
stars: stars,
},
["id"],
);
const ingredientInserts = ingredients.map((ing) => ({
recipe_id: id.id,
2025-07-25 18:07:20 +00:00
raw: ing,
}));
//
2025-07-25 18:07:20 +00:00
await db("recipe_ingredients").insert(ingredientInserts);
2025-07-25 18:07:20 +00:00
const stepInserts = Object.keys(steps).map((stepNumber) => ({
recipe_id: id.id,
step_number: parseInt(stepNumber),
2025-07-25 18:07:20 +00:00
instruction: steps[stepNumber],
}));
2025-07-25 18:07:20 +00:00
await db("recipe_steps").insert(stepInserts);
2025-07-08 10:40:49 -07:00
res.status(200).send({ message: "Recipe added", id: id.id });
} catch (err) {
console.log(err);
res.status(500).json({ error: err.message });
}
});
// ### SET STARS ###
2025-07-25 18:07:20 +00:00
app.post("/backend/set-stars", async (req, res) => {
2025-08-05 18:11:03 +00:00
if (process.env.NODE_ENV === 'demo') { return; };
const { id, stars } = req.body;
try {
2025-07-25 18:07:20 +00:00
await db("recipes").where({ id: id }).update({ stars: stars });
res.status(200).send({ message: "stars updated" });
} catch (err) {
console.log(err);
res.status(500).json({ error: err.message });
}
});
2025-07-09 14:43:45 -07:00
// ### DELETE RECIPE ###
2025-07-25 18:07:20 +00:00
app.delete("/backend/delete-recipe", async (req, res) => {
2025-08-05 18:11:03 +00:00
if (process.env.NODE_ENV === 'demo') { return; };
2025-07-08 10:40:49 -07:00
const { id } = req.body;
try {
2025-07-25 18:07:20 +00:00
await db("recipe_steps").where({ recipe_id: id }).del();
await db("recipe_ingredients").where({ recipe_id: id }).del();
await db("recipes").where({ id: id }).del();
2025-07-08 10:40:49 -07:00
res.status(200).send({ message: "Recipe deleted" });
} catch (err) {
console.log(err);
res.status(500).json({ error: err.message });
}
});
app.listen(port, () => console.log(`Server has started on port: ${port}`));
2025-07-25 18:07:20 +00:00
process.on("SIGINT", async () => {
console.log("Closing database connection...");
2025-07-08 10:40:49 -07:00
await db.destroy();
process.exit(0);
});