Skip to content

Commit

Permalink
Merge pull request #53 from WildCodeSchool-2023-09/managers_user
Browse files Browse the repository at this point in the history
add routers for users page and added column 'titre' in recipe table
  • Loading branch information
Palutenya authored Jan 10, 2024
2 parents bc3808e + bd17715 commit ac0567d
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 6 deletions.
8 changes: 4 additions & 4 deletions backend/database/sample_data_injection.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions backend/database/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions backend/src/controllers/commentControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -48,6 +61,7 @@ const add = async (req, res, next) => {
module.exports = {
browse,
readByRecipe,
readByUser,
// edit,
add,
// destroy,
Expand Down
11 changes: 11 additions & 0 deletions backend/src/models/CommentManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
11 changes: 11 additions & 0 deletions backend/src/models/FavManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
);
Expand Down
15 changes: 13 additions & 2 deletions backend/src/models/RecipeManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
1 change: 1 addition & 0 deletions backend/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ac0567d

Please sign in to comment.