diff --git a/backend/database/sample_data_injection.sql b/backend/database/sample_data_injection.sql index 75bb0b2..e9cf12c 100644 --- a/backend/database/sample_data_injection.sql +++ b/backend/database/sample_data_injection.sql @@ -11,11 +11,11 @@ VALUES ('hugoD','hugo.durand@gmail.com','Hugo','Durand','1989-02-05','test5',0) ; -INSERT INTO recipe (name, user_ID, prep_time, nb_people, difficulty,tag1, tag2 ) +INSERT INTO recipe (name,titre, user_ID, prep_time, nb_people, difficulty,tag1, tag2 ) VALUES - ('oeufs au plat',1, 15, 1, 'facile', 'végétarien', 'sans gluten'), - ('pâtes au beurre', 2, 25, 2, 'facile', 'végétarien', NULL), - ('religieuse au chocolat', 5, 90, 2, 'difficile', 'végétarien', 'gourmand') + ('oeufs au plat', 'simple comme bonjour',1, 15, 1, 'facile', 'végétarien', 'sans gluten'), + ('pâtes au beurre','la spécialité des étudiants!', 2, 25, 2, 'facile', 'végétarien', NULL), + ('religieuse au chocolat','pour les experts en pâtisserie', 5, 90, 2, 'difficile', 'végétarien', 'gourmand') ; INSERT INTO ingredient (name, unit, kcal) diff --git a/backend/database/schema.sql b/backend/database/schema.sql index 30f2dbf..9c013ed 100644 --- a/backend/database/schema.sql +++ b/backend/database/schema.sql @@ -18,6 +18,7 @@ CREATE TABLE recipe ( ID INT PRIMARY KEY NOT NULL AUTO_INCREMENT, user_ID INT NOT NULL, name VARCHAR(80) NOT NULL, + titre VARCHAR(100) NOT NULL, prep_time INT NOT NULL, nb_people INT NOT NULL, difficulty VARCHAR(30) NOT NULL, diff --git a/backend/src/controllers/commentControllers.js b/backend/src/controllers/commentControllers.js index ed6210e..8d18a9a 100644 --- a/backend/src/controllers/commentControllers.js +++ b/backend/src/controllers/commentControllers.js @@ -25,6 +25,19 @@ const readByRecipe = async (req, res, next) => { } }; +const readByUser = async (req, res, next) => { + try { + const comment = await tables.comment.readByUser(req.params.id); + if (comment == null) { + res.sendStatus(404); + } else { + res.json(comment); + } + } catch (err) { + next(err); + } +}; + // The E of BREAD - Edit (Update) operation // This operation is not yet implemented @@ -48,6 +61,7 @@ const add = async (req, res, next) => { module.exports = { browse, readByRecipe, + readByUser, // edit, add, // destroy, diff --git a/backend/src/models/CommentManager.js b/backend/src/models/CommentManager.js index c766308..a9d1bdd 100644 --- a/backend/src/models/CommentManager.js +++ b/backend/src/models/CommentManager.js @@ -21,5 +21,16 @@ WHERE r.ID = ?`, ); return rows; } + + async readByUser(id) { + const [rows] = await this.database.query( + `SELECT c.description,r.name FROM comment AS c +JOIN recipe AS r ON r.ID = c.recipe_ID +JOIN user AS u ON u.ID = c.user_ID +WHERE u.ID = ?`, + [id] + ); + return rows; + } } module.exports = CommentManager; diff --git a/backend/src/models/FavManager.js b/backend/src/models/FavManager.js index 3b3e51c..dbde4d0 100644 --- a/backend/src/models/FavManager.js +++ b/backend/src/models/FavManager.js @@ -16,6 +16,17 @@ class CommentManager extends AbstractManager { `SELECT f.id FROM fav AS f JOIN recipe AS r ON r.ID = f.recipe_ID JOIN user AS u ON u.ID = f.user_ID +WHERE r.ID = ?`, + [id] + ); + return rows; + } + + async readByUser(id) { + const [rows] = await this.database.query( + `SELECT f.id, r.name, r.titre FROM fav AS f +JOIN recipe AS r ON r.ID = f.recipe_ID +JOIN user AS u ON u.ID = f.user_ID WHERE r.ID = ?`, [id] ); diff --git a/backend/src/models/RecipeManager.js b/backend/src/models/RecipeManager.js index 92b8bdb..04c2846 100644 --- a/backend/src/models/RecipeManager.js +++ b/backend/src/models/RecipeManager.js @@ -6,7 +6,8 @@ class RecipeManager extends AbstractManager { } async readAll() { - const [rows] = await this.database.query(`select * from ${this.table}`); + const [rows] = await this.database.query(`select * + from ${this.table}`); return rows; } @@ -20,6 +21,16 @@ class RecipeManager extends AbstractManager { ); return rows[0]; } -} + async readByUser(id) { + const [rows] = await this.database.query( + `SELECT r.name, r.titre, r.image + FROM recipe AS r + JOIN user AS u ON u.ID = r.user_ID + WHERE u.ID = ?`, + [id] + ); + return rows[0]; + } +} module.exports = RecipeManager; diff --git a/backend/src/router.js b/backend/src/router.js index 0278f2b..8e4853d 100644 --- a/backend/src/router.js +++ b/backend/src/router.js @@ -60,6 +60,7 @@ router.get("/materialByRecipe/:id", MaterialControllers.readByRecipe); // Route const CommentControllers = require("./controllers/commentControllers"); router.get("/commentbyrecipe/:id", CommentControllers.readByRecipe); // Route to get comments for a specific Recipe +router.get("/commentbyuser/:id", CommentControllers.readByUser); // Route to get comments for a specific User /* ************************************************************************* */ // INSTRUCTION