135 lines
3.4 KiB
JavaScript
135 lines
3.4 KiB
JavaScript
|
const { PrismaClient } = require("@prisma/client");
|
||
|
const Logger = require("../utils/logger.js");
|
||
|
const logger = new Logger();
|
||
|
|
||
|
class recipeModel {
|
||
|
constructor() {
|
||
|
this.prisma = new PrismaClient();
|
||
|
}
|
||
|
|
||
|
async getAllRecipes() {
|
||
|
try {
|
||
|
return await this.prisma.recipes.findMany();
|
||
|
} catch (err) {
|
||
|
console.error("Error fetching all recipies:", err);
|
||
|
throw new Error(err.message);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async findById(id) {
|
||
|
try {
|
||
|
const recipe = await this.prisma.recipes.findUnique({
|
||
|
where: { id },
|
||
|
include: { recipeSteps: true, recipeIngredients: true },
|
||
|
});
|
||
|
if (!recipe) {
|
||
|
logger.warn(`recipe with id ${id} cannot be found`);
|
||
|
return null;
|
||
|
}
|
||
|
const data = {
|
||
|
details: {
|
||
|
id: recipe.id,
|
||
|
name: recipe.name,
|
||
|
author: recipe.author,
|
||
|
cuisine: recipe.cuisine,
|
||
|
stars: recipe.stars,
|
||
|
prep_minutes: recipe.prep_minutes,
|
||
|
cook_minutes: recipe.cook_minutes,
|
||
|
},
|
||
|
ingredients: recipe.recipeIngredients.map((ing) => ing.raw),
|
||
|
steps: recipe.recipeSteps.map((step, idx) => ({
|
||
|
step_number: step.step_number,
|
||
|
instruction: step.instruction,
|
||
|
})),
|
||
|
};
|
||
|
return data;
|
||
|
} catch (err) {
|
||
|
console.log("Error finding recipe:", err);
|
||
|
logger.error("error finding recipe", err);
|
||
|
throw new Error(err.message);
|
||
|
}
|
||
|
}
|
||
|
async addRecipe(recipeData) {
|
||
|
const {
|
||
|
name,
|
||
|
author,
|
||
|
cuisine,
|
||
|
stars,
|
||
|
ingredients,
|
||
|
steps,
|
||
|
prep_minutes,
|
||
|
cook_minutes,
|
||
|
} = recipeData;
|
||
|
try {
|
||
|
const createdRecipe = await this.prisma.recipes.create({
|
||
|
data: {
|
||
|
name,
|
||
|
author,
|
||
|
cuisine,
|
||
|
prep_minutes,
|
||
|
cook_minutes,
|
||
|
stars,
|
||
|
recipeIngredients: {
|
||
|
create: ingredients.map((ing) => ({ raw: ing })),
|
||
|
},
|
||
|
recipeSteps: {
|
||
|
create: Object.keys(steps).map((stepNumber) => ({
|
||
|
step_number: parseInt(stepNumber),
|
||
|
instruction: steps[stepNumber],
|
||
|
})),
|
||
|
},
|
||
|
},
|
||
|
});
|
||
|
|
||
|
logger.info("new recipe created", {
|
||
|
id: createdRecipe.id,
|
||
|
name: createdRecipe.name,
|
||
|
});
|
||
|
return createdRecipe;
|
||
|
} catch (error) {
|
||
|
console.log(error);
|
||
|
logger.error("error creating recipe", err);
|
||
|
throw new Error("Failed to add recipe");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async setStars(id, stars) {
|
||
|
try {
|
||
|
await this.prisma.recipes.update({
|
||
|
where: { id },
|
||
|
data: { stars },
|
||
|
});
|
||
|
return { message: "stars updated" };
|
||
|
} catch (err) {
|
||
|
console.error("Error updating stars:", err);
|
||
|
logger.error("error setting stars", err);
|
||
|
throw new Error(err.message);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async deleteRecipe(id) {
|
||
|
try {
|
||
|
await this.prisma.recipe_ingredients.deleteMany({
|
||
|
where: { recipe_id: id },
|
||
|
});
|
||
|
|
||
|
await this.prisma.recipe_steps.deleteMany({
|
||
|
where: { recipe_id: id },
|
||
|
});
|
||
|
const deletedRecipe = await this.prisma.recipes.delete({
|
||
|
where: { id },
|
||
|
});
|
||
|
logger.info("recipe deleted", {
|
||
|
id: deletedRecipe.id,
|
||
|
name: deletedRecipe.name,
|
||
|
});
|
||
|
return { message: "Recipe deleted successfully" };
|
||
|
} catch (err) {
|
||
|
console.error("Error deleting recipe:", err);
|
||
|
logger.error("error deleting recipe", err);
|
||
|
throw new Error(err.message);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
module.exports = recipeModel;
|