Skip to content

Commit 5a9246f

Browse files
authored
Add required recipe rows (#23)
* Add preparation_time, servings, and update recipe table structure * Add estimated_cost column to recipe table * Update README to correct environment variable for MariaDB root password * Enhance recipe procedures: add preparation time, servings, estimated cost, and update recipe status handling
1 parent f6c94d1 commit 5a9246f

File tree

4 files changed

+60
-22
lines changed

4 files changed

+60
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ To set up the database locally, you'll need Docker installed. Follow the steps b
1818
2. **Run the Docker Container**:
1919

2020
```bash
21-
docker run -d -p 3306:3306 --name Smart-Cooking_Database -e MYSQL_ROOT_PASSWORD=myrootpassword smartcooking-mariadb
21+
docker run -d -p 3306:3306 --name Smart-Cooking_Database -e MARIADB_ROOT_PASSWORD=myrootpassword smartcooking-mariadb
2222
```
2323

2424
3. **Access the Database**:

database.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,11 @@ CREATE OR REPLACE TABLE recipe (
9797
publication_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
9898
modification_date TIMESTAMP NULL,
9999
picture_id INT NULL,
100+
preparation_time INT UNSIGNED NULL,
100101
cook_time INT UNSIGNED NULL,
102+
servings INT UNSIGNED NULL,
101103
difficulty_level TINYINT CHECK (difficulty_level BETWEEN 1 AND 3),
104+
estimated_cost TINYINT CHECK (estimated_cost BETWEEN 1 AND 3),
102105
number_of_reviews INT NULL,
103106
recipe_source VARCHAR(255) NULL,
104107
recipe_status TINYINT NOT NULL DEFAULT 1,

procedures/get/recipe.sql

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,20 @@ CREATE OR REPLACE PROCEDURE get_recipe_by_id(
99
)
1010
BEGIN
1111
SELECT
12+
r.recipe_id,
1213
r.author_id,
1314
p.person_name AS author_name,
1415
r.publication_date,
1516
r.modification_date,
1617
r.picture_id,
18+
r.preparation_time,
1719
r.cook_time,
20+
r.servings,
1821
r.difficulty_level,
22+
r.estimated_cost,
1923
r.number_of_reviews,
20-
r.recipe_status,
24+
r.recipe_source,
25+
rs.status_name AS recipe_status,
2126
rt.title,
2227
rt.details,
2328
rt.preparation,
@@ -27,6 +32,7 @@ BEGIN
2732
INNER JOIN recipe_translation rt ON r.recipe_id = rt.recipe_id
2833
INNER JOIN lang l ON rt.language_id = l.language_id
2934
INNER JOIN person p ON r.author_id = p.person_id
35+
INNER JOIN recipe_status rs ON r.recipe_status = rs.status_id
3036
WHERE r.recipe_id = p_recipe_id
3137
AND l.iso_code = p_language_iso_code;
3238
END //
@@ -107,7 +113,7 @@ CREATE OR REPLACE PROCEDURE get_recipes_by_author_paginated(
107113
)
108114
BEGIN
109115
CALL get_recipes_paginated(
110-
'AND r.author_id = ?', p_limit, p_offset, p_language_iso_code, NULL, NULL, NULL, NULL
116+
'AND r.author_id = ?', p_limit, p_offset, p_language_iso_code, NULL, NULL, NULL, p_author_id
111117
);
112118
END //
113119

@@ -150,7 +156,7 @@ CREATE OR REPLACE PROCEDURE get_recipes_by_category_paginated(
150156
BEGIN
151157
CALL get_recipes_paginated(
152158
'INNER JOIN recipe_category rc ON r.recipe_id = rc.recipe_id AND rc.category_id = ?',
153-
p_limit, p_offset, p_language_iso_code, NULL, NULL, NULL, NULL
159+
p_limit, p_offset, p_language_iso_code, NULL, NULL, NULL, p_category_id
154160
);
155161
END //
156162

@@ -163,9 +169,8 @@ CREATE OR REPLACE PROCEDURE get_recipes_by_tags_paginated(
163169
)
164170
BEGIN
165171
CALL get_recipes_paginated(
166-
'INNER JOIN recipe_tag rtg ON r.recipe_id = rtg.recipe_id '
167-
'AND JSON_CONTAINS(?, JSON_QUOTE(rtg.tag))',
168-
p_limit, p_offset, p_language_iso_code, NULL, NULL, NULL, NULL
172+
'INNER JOIN recipe_tag rtg ON r.recipe_id = rtg.recipe_id AND JSON_CONTAINS(?, JSON_QUOTE(rtg.tag))',
173+
p_limit, p_offset, p_language_iso_code, NULL, NULL, NULL, p_tags
169174
);
170175
END //
171176

procedures/insert/recipe.sql

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,58 @@ USE smartcooking;
33

44
DELIMITER //
55

6-
CREATE OR REPLACE PROCEDURE insert_recipe(
7-
IN author_id INT,
8-
IN picture_id INT,
9-
IN cook_time INT,
10-
IN difficulty_level TINYINT,
11-
IN recipe_source VARCHAR(255),
12-
IN recipe_status_name VARCHAR(25)
6+
CREATE OR REPLACE PROCEDURE insert_recipe (
7+
IN p_author_id INT,
8+
IN p_picture_id INT,
9+
IN p_preparation_time INT,
10+
IN p_cook_time INT,
11+
IN p_servings INT,
12+
IN p_difficulty_level TINYINT,
13+
IN p_estimated_cost TINYINT,
14+
IN p_number_of_reviews INT,
15+
IN p_recipe_source VARCHAR(255),
16+
IN p_recipe_status_name VARCHAR(25)
1317
)
1418
BEGIN
15-
DECLARE status_id TINYINT;
19+
DECLARE v_status_id TINYINT DEFAULT NULL;
1620

17-
SELECT status_id INTO status_id
21+
-- Retrieve the status_id corresponding to the status name
22+
SELECT status_id
23+
INTO v_status_id
1824
FROM recipe_status
19-
WHERE status_name = recipe_status_name;
25+
WHERE status_name = p_recipe_status_name;
2026

21-
-- If the status name is not found, default to 'draft' (status_id = 1)
22-
IF status_id IS NULL THEN
23-
SET status_id = 1;
27+
-- Default to 'draft' (status_id = 1) if the status name is not found
28+
IF v_status_id IS NULL THEN
29+
SET v_status_id = 1;
2430
END IF;
2531

26-
INSERT INTO recipe (author_id, picture_id, cook_time, difficulty_level, recipe_source, recipe_status)
27-
VALUES (author_id, picture_id, cook_time, difficulty_level, recipe_source, status_id);
32+
-- Insert a new recipe
33+
INSERT INTO recipe (
34+
author_id,
35+
picture_id,
36+
preparation_time,
37+
cook_time,
38+
servings,
39+
difficulty_level,
40+
estimated_cost,
41+
number_of_reviews,
42+
recipe_source,
43+
recipe_status,
44+
modification_date
45+
) VALUES (
46+
p_author_id,
47+
p_picture_id,
48+
p_preparation_time,
49+
p_cook_time,
50+
p_servings,
51+
p_difficulty_level,
52+
p_estimated_cost,
53+
p_number_of_reviews,
54+
p_recipe_source,
55+
v_status_id,
56+
CURRENT_TIMESTAMP
57+
);
2858
END //
2959

3060
DELIMITER ;

0 commit comments

Comments
 (0)