'add simple logging'
This commit is contained in:
parent
5dc89497c6
commit
6169274fe1
9 changed files with 140 additions and 70 deletions
|
@ -78,7 +78,7 @@ exports.deleteRecipe = async (req, res) => {
|
|||
const id = parseInt(req.body.id, 10);
|
||||
try {
|
||||
await model.deleteRecipe(id);
|
||||
res.status(204).send();
|
||||
res.json({ success: "true" });
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
msg: "Failed to delete recipe",
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
const { PrismaClient } = require("@prisma/client");
|
||||
const Logger = require("../utils/logger.js");
|
||||
const logger = new Logger();
|
||||
|
||||
class recipeModel {
|
||||
constructor() {
|
||||
|
@ -21,6 +23,7 @@ class recipeModel {
|
|||
include: { recipeSteps: true, recipeIngredients: true },
|
||||
});
|
||||
if (!recipe) {
|
||||
logger.warn(`recipe with id ${id} cannot be found`);
|
||||
return null;
|
||||
}
|
||||
const data = {
|
||||
|
@ -41,7 +44,8 @@ class recipeModel {
|
|||
};
|
||||
return data;
|
||||
} catch (err) {
|
||||
console.error("Error finding recipe:", err);
|
||||
console.log("Error finding recipe:", err);
|
||||
logger.error("error finding recipe", err);
|
||||
throw new Error(err.message);
|
||||
}
|
||||
}
|
||||
|
@ -77,9 +81,14 @@ class recipeModel {
|
|||
},
|
||||
});
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
@ -93,6 +102,7 @@ class recipeModel {
|
|||
return { message: "stars updated" };
|
||||
} catch (err) {
|
||||
console.error("Error updating stars:", err);
|
||||
logger.error("error setting stars", err);
|
||||
throw new Error(err.message);
|
||||
}
|
||||
}
|
||||
|
@ -100,18 +110,23 @@ class recipeModel {
|
|||
async deleteRecipe(id) {
|
||||
try {
|
||||
await this.prisma.recipe_ingredients.deleteMany({
|
||||
where: { recipe_id: id }, // Ensure you have the right foreign key relation
|
||||
where: { recipe_id: id },
|
||||
});
|
||||
|
||||
await this.prisma.recipe_steps.deleteMany({
|
||||
where: { recipe_id: id }, // Ensure you have the right foreign key relation
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
33
backend/src/utils/logger.js
Normal file
33
backend/src/utils/logger.js
Normal file
|
@ -0,0 +1,33 @@
|
|||
const fs = require("fs");
|
||||
|
||||
class Logger {
|
||||
constructor(filePath) {
|
||||
this.filePath = "/logs/app.log";
|
||||
}
|
||||
|
||||
log(level, message, params) {
|
||||
const logEntry = {
|
||||
timestamp: new Date().toISOString(),
|
||||
level: level,
|
||||
message: message,
|
||||
params: params,
|
||||
};
|
||||
fs.appendFile(this.filePath, JSON.stringify(logEntry) + "\n", (err) => {
|
||||
if (err) throw err;
|
||||
});
|
||||
}
|
||||
|
||||
info(message, params = {}) {
|
||||
this.log("info", message, params);
|
||||
}
|
||||
|
||||
warn(message, params = {}) {
|
||||
this.log("warn", message, params);
|
||||
}
|
||||
|
||||
error(message, params = {}) {
|
||||
this.log("error", message, params);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Logger;
|
|
@ -1 +0,0 @@
|
|||
// todo
|
Loading…
Add table
Add a link
Reference in a new issue