recipe author and stars and a bit of cleanup
This commit is contained in:
parent
c47dac9986
commit
6f43d17ddd
21 changed files with 361 additions and 207 deletions
1
backend/.gitignore
vendored
1
backend/.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
node_modules
|
||||
scratch
|
||||
|
|
|
@ -22,17 +22,34 @@ app.get("/recipes", async (req, res) => {
|
|||
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', 'cuisine');
|
||||
const recipeQuery = db('recipes').where('id', '=', id).select('id', 'name', 'author', 'cuisine', 'stars');
|
||||
|
||||
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 ingredientsQuery = db.from('recipe_ingredients').where('recipe_id', '=', id).select('raw');
|
||||
|
||||
const stepsQuery = db('recipe_steps').where('recipe_id', id).select('step_number', 'instruction');
|
||||
|
||||
|
@ -40,15 +57,8 @@ app.get("/recipe/:id", async (req, res) => {
|
|||
|
||||
const result = {
|
||||
details: recipe[0],
|
||||
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;
|
||||
}, {})
|
||||
ingredients: ingredients,
|
||||
steps: steps
|
||||
};
|
||||
|
||||
res.json(result);
|
||||
|
@ -60,38 +70,22 @@ app.get("/recipe/:id", async (req, res) => {
|
|||
|
||||
// ### ADD RECIPE ###
|
||||
app.post("/add-recipe", async (req, res) => {
|
||||
const { name, cuisine, ingredients, steps } = req.body;
|
||||
const { name, author, cuisine, stars, ingredients, steps } = req.body;
|
||||
try {
|
||||
const [id] = await db('recipes').insert({
|
||||
name: name,
|
||||
cuisine: cuisine
|
||||
author: author,
|
||||
cuisine: cuisine,
|
||||
stars: stars
|
||||
}, ['id'])
|
||||
|
||||
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
|
||||
const [newIngredient] = await db('ingredients').insert({
|
||||
name: ingredient.name
|
||||
}, ['id']);
|
||||
ingredientData.push({ id: newIngredient.id, quantity: ingredient.quantity, unit: ingredient.unit });
|
||||
} 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
|
||||
const ingredientInserts = ingredients.map(ing => ({
|
||||
recipe_id: id.id,
|
||||
raw: ing
|
||||
}));
|
||||
//
|
||||
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),
|
||||
|
@ -106,10 +100,26 @@ app.post("/add-recipe", async (req, res) => {
|
|||
}
|
||||
});
|
||||
|
||||
// ### 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) {
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
/**
|
||||
* @param { import("knex").Knex } knex
|
||||
* @returns { Promise<void> }
|
||||
*/
|
||||
exports.up = function (knex) {
|
||||
return knex.schema.table('recipe_ingredients', table => {
|
||||
table.string('raw', 255).defaultTo('');
|
||||
table.integer('ingredient_id').nullable().alter();
|
||||
table.string('quantity').nullable().alter();
|
||||
table.string('unit').nullable().alter();
|
||||
}).table('recipe_steps', table => {
|
||||
table.string('instruction', 510).alter();
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @param { import("knex").Knex } knex
|
||||
* @returns { Promise<void> }
|
||||
*/
|
||||
exports.down = function (knex) {
|
||||
return knex.schema.table('recipe_ingredients', table => {
|
||||
table.dropColumn('raw');
|
||||
});
|
||||
};
|
|
@ -0,0 +1,22 @@
|
|||
/**
|
||||
* @param { import("knex").Knex } knex
|
||||
* @returns { Promise<void> }
|
||||
*/
|
||||
exports.up = function (knex) {
|
||||
|
||||
return knex.schema.table('recipes', table => {
|
||||
table.string('author');
|
||||
table.integer('stars');
|
||||
})
|
||||
};
|
||||
|
||||
/**
|
||||
* @param { import("knex").Knex } knex
|
||||
* @returns { Promise<void> }
|
||||
*/
|
||||
exports.down = function (knex) {
|
||||
return knex.schema.table('recipes', table => {
|
||||
table.dropColumn('author');
|
||||
table.dropColumn('stars')
|
||||
})
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue