const express = require("express"); const db = require("./db"); const port = 3000; const cors = require('cors') const app = express(); app.use(cors()); app.use(express.json()); // ####### ROUTES ####### app.get("/test", async (req, res) => { console.log('test') res.json({ test: 'test' }) }); // ### GET ALL RECIPES ### app.get("/recipes", async (req, res) => { try { const recipes = await db('recipes').select('id', 'name', 'cuisine'); res.json(recipes); } catch (err) { console.log(err); res.status(500).json({ error: err.message }); } }); // ### GET ALL RECIPE_INGREDIENTS ### app.get("/recipe-ingredients", async (req, res) => { try { 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 ### app.get("/recipe-steps", async (req, res) => { try { const recipe_steps = await db('recipe_steps').select('*'); res.json(recipe_steps); } catch (err) { console.log(err); res.status(500).json({ error: err.message }); } }); // ### GET RECIPE ### app.get("/recipe/:id", async (req, res) => { const id = req.params.id try { const recipeQuery = db('recipes').where('id', '=', id).select('id', 'name', 'author', 'cuisine', 'stars'); const ingredientsQuery = db.from('recipe_ingredients').where('recipe_id', '=', id).select('raw'); 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 = { details: recipe[0], ingredients: ingredients, steps: steps }; res.json(result); } catch (err) { console.log(err); res.status(500).json({ error: err.message }); } }); // ### ADD RECIPE ### app.post("/add-recipe", async (req, res) => { const { name, author, cuisine, stars, ingredients, steps } = req.body; try { const [id] = await db('recipes').insert({ name: name, author: author, cuisine: cuisine, stars: stars }, ['id']) const ingredientInserts = ingredients.map(ing => ({ recipe_id: id.id, raw: ing })); // await db('recipe_ingredients').insert(ingredientInserts); const stepInserts = Object.keys(steps).map(stepNumber => ({ recipe_id: id.id, step_number: parseInt(stepNumber), instruction: steps[stepNumber] })); await db('recipe_steps').insert(stepInserts); res.status(200).send({ message: "Recipe added", id: id.id }); } catch (err) { console.log(err); res.status(500).json({ error: err.message }); } }); // ### SET STARS ### app.post("/set-stars", async (req, res) => { const { id, stars } = req.body; try { 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 }); } }); // ### DELETE RECIPE ### app.delete("/delete-recipe", async (req, res) => { const { id } = req.body; try { 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(); 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); });