Skip to content

Commit

Permalink
Merge branch 'dev' into S03US05_Add+Security
Browse files Browse the repository at this point in the history
  • Loading branch information
fgaujard authored Jan 10, 2024
2 parents 489188c + ac0567d commit 0aa56a5
Show file tree
Hide file tree
Showing 29 changed files with 355 additions and 135 deletions.
34 changes: 11 additions & 23 deletions backend/database/sample_data_injection.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,22 @@ VALUES
('hugoD','hugo.durand@gmail.com','Hugo','Durand','1989-02-05','test5',0)
;

INSERT INTO recipe (name, user_ID, prep_time, nb_people, difficulty,image )
INSERT INTO recipe (name,titre, user_ID, prep_time, nb_people, difficulty,tag1, tag2 )
VALUES
('oeufs au plat',1, 15, 1, 'facile','jetbrains://web-storm/navigate/reference?project=eating-nam-nam&path=backend/public/assets/images/uploads/pexels-iina-luoto-1211887.jpg'),
('pâtes au beurre', 2, 25, 2, 'facile', 'C:\Users\trist\projects\eating-nam-nam\backend\public\assets\images\uploads\pexels-jer-chung-2116094.jpg'),
('religieuse au chocolat', 5, 90, 2, 'difficile','C:\Users\trist\projects\eating-nam-nam\backend\public\assets\images\uploads\pexels-julie-aagaard-2097090.jpg')
('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)
INSERT INTO ingredient (name, unit, kcal)
VALUES
('oeuf', 'pièce(s)', 20),
('oeuf', 'pièce(s)', 20),
('beurre', 'grammes', 5),
('chocolat noir', 'grammes', 3),
('farine de blé', 'grammes', 1),
('pâtes', 'grammes', 2)
;

INSERT INTO recipe_ingredient (recipe_ID, ingredient_ID, quantity)
VALUES
(1, 1, 2),
(1, 2, 20),
(2, 5, 200),
(2, 2, 50),
(3,3,300),
(3,4,200),
(3,1,6)
;

INSERT INTO instruction (description, recipe_ID)
VALUES
('Préchauffez la poêle',1),
Expand Down Expand Up @@ -74,12 +63,6 @@ VALUES
("sans gluten")
;

INSERT INTO recipe_tag (recipe_ID,tag_ID)
VALUES
(1,2),
(1,3),
(2,2)
;

INSERT INTO comment (description, user_ID, recipe_ID)
VALUES
Expand All @@ -88,3 +71,8 @@ VALUES
("J'ai pas eu besoin de spatule pour ça!",5,2)
;

INSERT INTO recipe_ingredient (recipe_ID, ingredient_ID, quantity)
VALUES
(1,1,2),
(1,2,10)
;
39 changes: 16 additions & 23 deletions backend/database/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,24 @@ 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,
image VARCHAR(200),
tag1 VARCHAR(30),
tag2 VARCHAR(30),
tag3 VARCHAR(30),
FOREIGN KEY (user_ID) REFERENCES user(id)
);
CREATE TABLE ingredient
(
ID INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
kcal INT NOT NULL,
unité VARCHAR(10) NOT NULL
);
CREATE TABLE recipe_ingredient
(
ID INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
recipe_ID INT NOT NULL,
ingredient_ID INT NOT NULL,
quantity INT NOT NULL,
FOREIGN KEY (recipe_ID) REFERENCES recipe(ID),
FOREIGN KEY (ingredient_ID) REFERENCES ingredient(ID)
);
CREATE TABLE tag
(
ID INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VARCHAR(40) NOT NULL
);
CREATE TABLE recipe_tag
(
ID INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
recipe_ID INT NOT NULL,
tag_ID INT NOT NULL,
FOREIGN KEY (recipe_ID) REFERENCES recipe(ID),
FOREIGN KEY (tag_ID) REFERENCES tag(ID)
unit VARCHAR(10) NOT NULL
);

CREATE TABLE fav
(
ID INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
Expand Down Expand Up @@ -92,4 +75,14 @@ CREATE TABLE recipe_material
FOREIGN KEY (material_ID) REFERENCES material(ID)
);

CREATE TABLE recipe_ingredient
(
ID INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
recipe_ID INT NOT NULL,
ingredient_ID INT NOT NULL,
quantity INT NOT NULL,
FOREIGN KEY (recipe_ID) REFERENCES recipe(ID),
FOREIGN KEY (ingredient_ID) REFERENCES ingredient(ID)
);


20 changes: 17 additions & 3 deletions backend/src/controllers/commentControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,22 @@ const browse = async (req, res, next) => {
};

// READ
const read = async (req, res, next) => {
const readByRecipe = async (req, res, next) => {
try {
const comment = await tables.comment.read(req.params.id);
const comment = await tables.comment.readByRecipe(req.params.id);
if (comment == null) {
res.sendStatus(404);
} else {
res.json(comment);
}
} catch (err) {
next(err);
}
};

const readByUser = async (req, res, next) => {
try {
const comment = await tables.comment.readByUser(req.params.id);
if (comment == null) {
res.sendStatus(404);
} else {
Expand Down Expand Up @@ -47,7 +60,8 @@ const add = async (req, res, next) => {
// Ready to export the controller functions
module.exports = {
browse,
read,
readByRecipe,
readByUser,
// edit,
add,
// destroy,
Expand Down
6 changes: 3 additions & 3 deletions backend/src/controllers/favControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const browse = async (req, res, next) => {
};

// READ
const read = async (req, res, next) => {
const readByRecipe = async (req, res, next) => {
try {
const fav = await tables.fav.read(req.params.id);
const fav = await tables.fav.readByRecipe(req.params.id);
if (fav == null) {
res.sendStatus(404);
} else {
Expand Down Expand Up @@ -47,7 +47,7 @@ const add = async (req, res, next) => {
// Ready to export the controller functions
module.exports = {
browse,
read,
readByRecipe,
// edit,
add,
// destroy,
Expand Down
14 changes: 13 additions & 1 deletion backend/src/controllers/ingredientControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,18 @@ const read = async (req, res, next) => {
next(err);
}
};

const readByRecipe = async (req, res, next) => {
try {
const ingredient = await tables.ingredient.readByRecipe(req.params.id);
if (ingredient == null) {
res.sendStatus(404);
} else {
res.json(ingredient);
}
} catch (err) {
next(err);
}
};
// The E of BREAD - Edit (Update) operation
// This operation is not yet implemented

Expand All @@ -48,6 +59,7 @@ const add = async (req, res, next) => {
module.exports = {
browse,
read,
readByRecipe,
// edit,
add,
// destroy,
Expand Down
13 changes: 13 additions & 0 deletions backend/src/controllers/instructionControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ const read = async (req, res, next) => {
}
};

const readByRecipe = async (req, res, next) => {
try {
const instruction = await tables.instruction.readByRecipe(req.params.id);
if (instruction == null) {
res.sendStatus(404);
} else {
res.json(instruction);
}
} catch (err) {
next(err);
}
};
// The E of BREAD - Edit (Update) operation
// This operation is not yet implemented

Expand All @@ -48,6 +60,7 @@ const add = async (req, res, next) => {
module.exports = {
browse,
read,
readByRecipe,
// edit,
add,
// destroy,
Expand Down
13 changes: 13 additions & 0 deletions backend/src/controllers/materialControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ const read = async (req, res, next) => {
next(err);
}
};
const readByRecipe = async (req, res, next) => {
try {
const material = await tables.material.readByRecipe(req.params.id);
if (material == null) {
res.sendStatus(404);
} else {
res.json(material);
}
} catch (err) {
next(err);
}
};

// The E of BREAD - Edit (Update) operation
// This operation is not yet implemented
Expand All @@ -48,6 +60,7 @@ const add = async (req, res, next) => {
module.exports = {
browse,
read,
readByRecipe,
// edit,
add,
// destroy,
Expand Down
14 changes: 14 additions & 0 deletions backend/src/controllers/tagControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const tables = require("../tables");

const browse = async (req, res, next) => {
try {
const tags = await tables.tag.readAll();

res.json(tags);
} catch (err) {
next(err);
}
};
module.exports = {
browse,
};
17 changes: 14 additions & 3 deletions backend/src/models/CommentManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@ class CommentManager extends AbstractManager {
return rows;
}

async read(id) {
async readByRecipe(id) {
const [rows] = await this.database.query(
`SELECT c.description, u.username FROM comment AS c
`SELECT c.description, u.username, u.avatar 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 r.ID = ${id};`,
WHERE r.ID = ?`,
[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;
Expand Down
15 changes: 13 additions & 2 deletions backend/src/models/FavManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@ class CommentManager extends AbstractManager {
return rows;
}

async read(id) {
async readByRecipe(id) {
const [rows] = await this.database.query(
`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};`,
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]
);
return rows;
Expand Down
11 changes: 11 additions & 0 deletions backend/src/models/IngredientManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,16 @@ WHERE r.ID = ${id};`,
);
return rows;
}

async readByRecipe(id) {
const [rows] = await this.database.query(
`SELECT i.name, i.unit, i.kcal, p.quantity from ingredient AS i
JOIN recipe_ingredient AS p ON p.ingredient_ID = i.ID
JOIN recipe ON recipe.ID= p.recipe_ID
WHERE recipe.ID= ?`,
[id]
);
return rows;
}
}
module.exports = IngredientManager;
10 changes: 10 additions & 0 deletions backend/src/models/InstructionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,15 @@ WHERE r.ID = ${id};`,
);
return rows;
}

async readByRecipe(id) {
const [rows] = await this.database.query(
`SELECT i.description from instruction AS i
JOIN recipe ON recipe.ID= i.recipe_ID
WHERE recipe.ID= ?`,
[id]
);
return rows;
}
}
module.exports = InstructionManager;
11 changes: 11 additions & 0 deletions backend/src/models/MaterialManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,16 @@ WHERE r.ID = ${id};`,
);
return rows;
}

async readByRecipe(id) {
const [rows] = await this.database.query(
`SELECT m.name from material AS m
JOIN recipe_material AS p ON p.material_ID = m.ID
JOIN recipe ON recipe.ID= p.recipe_ID
WHERE recipe.ID= ?`,
[id]
);
return rows;
}
}
module.exports = MaterialManager;
Loading

0 comments on commit 0aa56a5

Please sign in to comment.