SHARE
TWEET

Untitled

a guest Aug 23rd, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const executeQuery = (query, res) => new Promise((resolve, reject) => {
  2.     mysql.execute(query, (err, result) => {
  3.         if (err) {
  4.             res.status(500).send(err.message)
  5.         }
  6.  
  7.         resolve(result)
  8.     })
  9. })
  10.  
  11. app.post('/recipes-by-ingredients', async (req, res) => {
  12.     const { name, description, ingredients } = req.body
  13.  
  14.     const recipeQuery = `
  15.         INSERT INTO recipes (name, description)
  16.         VALUES ('${name}', '${description}')
  17.         RETURNING id
  18.     `
  19.  
  20.     const recipe = await executeQuery(recipeQuery, res);
  21.  
  22.     const ingredientsQuery = `
  23.         INSERT INTO recipes_to_ingredients (recipe_id, ingredient_id)
  24.         VALUES
  25.             (${recipe.id}, 20),
  26.             (${recipe.id}, 21),
  27.             (${recipe.id}, 22),
  28.             (${recipe.id}, 23),
  29.             (${recipe.id}, 24)
  30.     `;
  31.  
  32.     res.json(await executeQuery(ingredientsQuery, res))
  33. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top