edit recipe

This commit is contained in:
fred 2025-10-20 11:43:29 -07:00
parent 798e879863
commit 8b52d31dcf
10 changed files with 389 additions and 217 deletions

View file

@ -50,6 +50,7 @@ export const addRecipe = async (req: Request, res: Response): Promise<void> => {
return;
}
try {
console.log(req.body);
const createdRecipe = await model.addRecipe(req.body);
res.status(201).json(createdRecipe);
} catch (error) {
@ -61,6 +62,27 @@ export const addRecipe = async (req: Request, res: Response): Promise<void> => {
}
};
export const updateRecipe = async (
req: Request,
res: Response,
): Promise<void> => {
console.log(req.body);
const id = parseInt(req.params.id, 10);
if (process.env.NODE_ENV === "demo") {
return;
}
try {
const updatedRecipe = await model.updateRecipe(req.body, id);
res.status(201).json(updatedRecipe);
} catch (error) {
res.status(500).json({
msg: "Failed to add recipe",
source: "recipeController",
error: error instanceof Error ? error.message : "Unknown error",
});
}
};
export const setStars = async (req: Request, res: Response): Promise<void> => {
if (process.env.NODE_ENV === "demo") {
return;
@ -104,6 +126,7 @@ export default {
getAllRecipes,
getRecipeById,
addRecipe,
updateRecipe,
setStars,
deleteRecipe,
};

View file

@ -12,7 +12,7 @@ class RecipeModel {
async getAllRecipes(): Promise<any[]> {
try {
logger.info("index page view")
logger.info("index page view");
return await this.prisma.recipes.findMany();
} catch (err) {
console.error("Error fetching all recipes:", err);
@ -46,7 +46,10 @@ class RecipeModel {
instruction: step.instruction,
})),
};
logger.info("recipe page view", { recipe_id: data.details.id, recipe_name: data.details.name })
logger.info("recipe page view", {
recipe_id: data.details.id,
recipe_name: data.details.name,
});
return data;
} catch (err) {
console.log("Error finding recipe:", err);
@ -58,25 +61,25 @@ class RecipeModel {
}
async addRecipe(recipeData: {
name: string;
author: string;
cuisine: string;
stars: number;
details: {
name: string;
author: string;
cuisine: string;
stars: number;
prep_minutes: number;
cook_minutes: number;
};
ingredients: string[];
steps: { [key: string]: string };
prep_minutes: number;
cook_minutes: number;
}): Promise<any> {
const {
name,
author,
cuisine,
stars,
ingredients,
steps,
prep_minutes,
cook_minutes,
} = recipeData;
const name = recipeData.details.name;
const author = recipeData.details.author;
const cuisine = recipeData.details.cuisine;
const stars = recipeData.details.stars;
const prep_minutes = recipeData.details.prep_minutes;
const cook_minutes = recipeData.details.cook_minutes;
const ingredients = recipeData.ingredients;
const steps = recipeData.steps;
try {
const createdRecipe = await this.prisma.recipes.create({
data: {
@ -90,10 +93,7 @@ class RecipeModel {
create: ingredients.map((ing) => ({ raw: ing })),
},
recipeSteps: {
create: Object.keys(steps).map((stepNumber) => ({
step_number: parseInt(stepNumber),
instruction: steps[stepNumber],
})),
create: steps,
},
},
});
@ -112,6 +112,67 @@ class RecipeModel {
}
}
async updateRecipe(
recipeData: {
details: {
name: string;
author: string;
cuisine: string;
stars: number;
prep_minutes: number;
cook_minutes: number;
};
ingredients: string[];
steps: { [key: string]: string };
},
id: number,
): Promise<any> {
const name = recipeData.details.name;
const author = recipeData.details.author;
const cuisine = recipeData.details.cuisine;
const stars = recipeData.details.stars;
const prep_minutes = recipeData.details.prep_minutes;
const cook_minutes = recipeData.details.cook_minutes;
const ingredients = recipeData.ingredients;
const steps = recipeData.steps;
try {
await this.deleteRecipeData(id);
const updatedRecipe = await this.prisma.recipes.update({
where: { id },
data: {
name,
author,
cuisine,
prep_minutes,
cook_minutes,
stars,
recipeIngredients: {
create: ingredients.map((ing) => ({ raw: ing })),
},
recipeSteps: {
create: steps,
},
},
});
if (!updatedRecipe) {
logger.warn(`Recipe with id ${id} cannot be found`);
return null;
}
logger.info("Updated Recipe", {
id: updatedRecipe.id,
name: updatedRecipe.name,
});
return updatedRecipe;
} catch (err) {
console.log("Error updating recipe:", err);
logger.error("Error updating recipe", {
message: err instanceof Error ? err.message : "Unknown error",
});
throw new Error("Failed to update recipe");
}
}
async setStars(id: number, stars: number): Promise<{ message: string }> {
try {
await this.prisma.recipes.update({
@ -130,13 +191,7 @@ class RecipeModel {
async deleteRecipe(id: number): Promise<{ message: string }> {
try {
await this.prisma.recipe_ingredients.deleteMany({
where: { recipe_id: id },
});
await this.prisma.recipe_steps.deleteMany({
where: { recipe_id: id },
});
this.deleteRecipeData(id);
const deletedRecipe = await this.prisma.recipes.delete({
where: { id },
});
@ -153,6 +208,25 @@ class RecipeModel {
throw new Error(err instanceof Error ? err.message : "Unknown error");
}
}
async deleteRecipeData(id: number): Promise<{ message: string }> {
try {
await this.prisma.recipe_ingredients.deleteMany({
where: { recipe_id: id },
});
await this.prisma.recipe_steps.deleteMany({
where: { recipe_id: id },
});
return { message: "Recipe data deleted successfully" };
} catch (err) {
console.error("Error deleting recipe:", err);
logger.error("Error deleting recipe", {
message: err instanceof Error ? err.message : "Unknown error",
});
throw new Error(err instanceof Error ? err.message : "Unknown error");
}
}
}
export default RecipeModel;

View file

@ -11,6 +11,8 @@ router.get("/recipe/:id", recipeController.getRecipeById);
router.post("/add-recipe", recipeController.addRecipe);
router.post("/update-recipe/:id", recipeController.updateRecipe);
router.post("/set-stars", recipeController.setStars);
router.delete("/delete-recipe", recipeController.deleteRecipe);