2025-07-08 10:40:49 -07:00
|
|
|
const express = require("express");
|
|
|
|
const db = require("./db");
|
|
|
|
const port = 6063;
|
|
|
|
const cors = require('cors'); // to remove cors origin error in dev TODO: remove when dockerized
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
app.use(cors()); // to remove cors origin error in dev TODO: remove when dockerized
|
|
|
|
app.use(express.json());
|
|
|
|
|
2025-07-09 14:43:45 -07:00
|
|
|
// ####### ROUTES #######
|
|
|
|
|
|
|
|
// ### GET ALL RECIPES ###
|
2025-07-08 10:40:49 -07:00
|
|
|
app.get("/recipes", async (req, res) => {
|
|
|
|
try {
|
2025-07-11 17:06:41 -07:00
|
|
|
const recipes = await db('recipes').select('id', 'name', 'cuisine');
|
2025-07-08 10:40:49 -07:00
|
|
|
res.json(recipes);
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
res.status(500).json({ error: err.message });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2025-07-09 14:43:45 -07:00
|
|
|
// ### GET RECIPE ###
|
2025-07-08 10:40:49 -07:00
|
|
|
app.get("/recipe/:id", async (req, res) => {
|
|
|
|
const id = req.params.id
|
|
|
|
try {
|
2025-07-08 17:26:02 -07:00
|
|
|
const recipeQuery = db('recipes').where('id', '=', id).select('id', 'name', 'cuisine');
|
|
|
|
|
|
|
|
const ingredientsQuery = db.from('recipe_ingredients as ri')
|
|
|
|
.join('ingredients as i', 'ri.ingredient_id', 'i.id')
|
|
|
|
.where('ri.recipe_id', id)
|
|
|
|
.select('i.name', 'ri.quantity', 'ri.unit');
|
|
|
|
|
|
|
|
const stepsQuery = db('recipe_steps').where('recipe_id', id).select('step_number', 'instruction');
|
|
|
|
|
|
|
|
const [recipe, ingredients, steps] = await Promise.all([recipeQuery, ingredientsQuery, stepsQuery]);
|
|
|
|
|
|
|
|
const result = {
|
2025-07-09 11:02:23 -07:00
|
|
|
details: recipe[0],
|
2025-07-08 17:26:02 -07:00
|
|
|
ingredients: ingredients.map(ingredient => ({
|
|
|
|
name: ingredient.name,
|
|
|
|
quantity: ingredient.quantity,
|
|
|
|
unit: ingredient.unit
|
|
|
|
})),
|
|
|
|
steps: steps.reduce((acc, step) => {
|
|
|
|
acc[step.step_number] = step.instruction;
|
|
|
|
return acc;
|
|
|
|
}, {})
|
|
|
|
};
|
|
|
|
|
|
|
|
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-08 10:40:49 -07:00
|
|
|
app.post("/add-recipe", async (req, res) => {
|
2025-07-08 17:26:02 -07:00
|
|
|
const { name, cuisine, ingredients, steps } = req.body;
|
2025-07-08 10:40:49 -07:00
|
|
|
try {
|
|
|
|
const [id] = await db('recipes').insert({
|
|
|
|
name: name,
|
|
|
|
cuisine: cuisine
|
|
|
|
}, ['id'])
|
2025-07-08 17:26:02 -07:00
|
|
|
|
|
|
|
const existingIngredients = await db('ingredients').whereIn('name', ingredients.map(ing => ing.name));
|
|
|
|
let ingredientData = [];
|
|
|
|
for (let ingredient of ingredients) {
|
|
|
|
const existingIngredient = existingIngredients.find(ing => ing.name === ingredient.name);
|
|
|
|
if (!existingIngredient) {
|
|
|
|
// create the ingredient if there is no entry
|
2025-07-09 17:06:40 -07:00
|
|
|
const [newIngredient] = await db('ingredients').insert({
|
2025-07-08 17:26:02 -07:00
|
|
|
name: ingredient.name
|
|
|
|
}, ['id']);
|
2025-07-09 17:06:40 -07:00
|
|
|
ingredientData.push({ id: newIngredient.id, quantity: ingredient.quantity, unit: ingredient.unit });
|
2025-07-08 17:26:02 -07:00
|
|
|
} else {
|
|
|
|
// if the ingredient exists use existing entry
|
|
|
|
ingredientData.push({ id: existingIngredient.id, quantity: ingredient.quantity, unit: ingredient.unit });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ingredientInserts = ingredientData.map(ing => ({
|
|
|
|
ingredient_id: ing.id,
|
|
|
|
quantity: ing.quantity,
|
|
|
|
unit: ing.unit,
|
|
|
|
recipe_id: id.id
|
|
|
|
}));
|
|
|
|
await db('recipe_ingredients').insert(ingredientInserts);
|
|
|
|
|
|
|
|
// Step 4: Insert steps into recipe_steps
|
|
|
|
const stepInserts = Object.keys(steps).map(stepNumber => ({
|
|
|
|
recipe_id: id.id,
|
|
|
|
step_number: parseInt(stepNumber),
|
|
|
|
instruction: steps[stepNumber]
|
|
|
|
}));
|
|
|
|
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 });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2025-07-09 14:43:45 -07:00
|
|
|
// ### DELETE RECIPE ###
|
2025-07-08 10:40:49 -07:00
|
|
|
app.delete("/delete-recipe", async (req, res) => {
|
|
|
|
const { id } = req.body;
|
|
|
|
try {
|
|
|
|
await db('recipes').where({ id: id }).del();
|
|
|
|
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}`));
|
|
|
|
|
|
|
|
process.on('SIGINT', async () => {
|
|
|
|
console.log('Closing database connection...');
|
|
|
|
await db.destroy();
|
|
|
|
process.exit(0);
|
|
|
|
});
|