Skip to content

Commit e7f8356

Browse files
authored
Fix get_recipe_by_name (#16)
* Fix get_recipes_paginated with filter parameter * Refactor get_recipes_paginated calls to include additional NULL parameter for consistency
1 parent 85a7e8f commit e7f8356

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

procedures/get/recipe.sql

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ BEGIN
1818
r.difficulty_level,
1919
r.number_of_reviews,
2020
r.recipe_status,
21-
r.recipe_status,
2221
rt.title,
2322
rt.details,
2423
rt.preparation,
@@ -40,7 +39,8 @@ CREATE OR REPLACE PROCEDURE get_recipes_paginated(
4039
IN p_language_iso_code CHAR(2),
4140
IN p_group_by TEXT,
4241
IN p_having_condition TEXT,
43-
IN p_order_by TEXT
42+
IN p_order_by TEXT,
43+
IN p_filter_param TEXT
4444
)
4545
BEGIN
4646
DECLARE v_limit INT;
@@ -58,7 +58,7 @@ BEGIN
5858
'WHERE l.iso_code = ? ';
5959

6060
IF p_filter_condition IS NOT NULL THEN
61-
SET @query = CONCAT(@query, p_filter_condition);
61+
SET @query = CONCAT(@query, ' ', p_filter_condition);
6262
END IF;
6363

6464
IF p_group_by IS NOT NULL THEN
@@ -76,7 +76,13 @@ BEGIN
7676
SET @query = CONCAT(@query, ' LIMIT ? OFFSET ?');
7777

7878
PREPARE stmt FROM @query;
79-
EXECUTE stmt USING p_language_iso_code, v_limit, v_offset;
79+
80+
IF p_filter_param IS NOT NULL THEN
81+
EXECUTE stmt USING p_language_iso_code, p_filter_param, v_limit, v_offset;
82+
ELSE
83+
EXECUTE stmt USING p_language_iso_code, v_limit, v_offset;
84+
END IF;
85+
8086
DEALLOCATE PREPARE stmt;
8187
END //
8288

@@ -87,7 +93,9 @@ CREATE OR REPLACE PROCEDURE get_all_recipes_paginated(
8793
IN p_language_iso_code CHAR(2)
8894
)
8995
BEGIN
90-
CALL get_recipes_paginated(NULL, p_limit, p_offset, p_language_iso_code, NULL, NULL, NULL);
96+
CALL get_recipes_paginated(
97+
NULL, p_limit, p_offset, p_language_iso_code, NULL, NULL, NULL, NULL
98+
);
9199
END //
92100

93101
-- Procedure for retrieving recipes by author with pagination
@@ -98,7 +106,9 @@ CREATE OR REPLACE PROCEDURE get_recipes_by_author_paginated(
98106
IN p_language_iso_code CHAR(2)
99107
)
100108
BEGIN
101-
CALL get_recipes_paginated('AND r.author_id = ?', p_limit, p_offset, p_language_iso_code, NULL, NULL, NULL);
109+
CALL get_recipes_paginated(
110+
'AND r.author_id = ?', p_limit, p_offset, p_language_iso_code, NULL, NULL, NULL, NULL
111+
);
102112
END //
103113

104114
-- Procedure for retrieving top-rated recipes with pagination
@@ -113,7 +123,7 @@ BEGIN
113123
'INNER JOIN recipe_rating rr ON r.recipe_id = rr.recipe_id',
114124
p_limit, p_offset, p_language_iso_code, 'GROUP BY r.recipe_id',
115125
'HAVING AVG(rr.rating) >= ?',
116-
'ORDER BY AVG(rr.rating) DESC'
126+
'ORDER BY AVG(rr.rating) DESC', NULL
117127
);
118128
END //
119129

@@ -156,7 +166,7 @@ CREATE OR REPLACE PROCEDURE get_recipes_by_category_paginated(
156166
BEGIN
157167
CALL get_recipes_paginated(
158168
'INNER JOIN recipe_category rc ON r.recipe_id = rc.recipe_id AND rc.category_id = ?',
159-
p_limit, p_offset, p_language_iso_code, NULL, NULL, NULL
169+
p_limit, p_offset, p_language_iso_code, NULL, NULL, NULL, NULL
160170
);
161171
END //
162172

@@ -171,7 +181,7 @@ BEGIN
171181
CALL get_recipes_paginated(
172182
'INNER JOIN recipe_tag rtg ON r.recipe_id = rtg.recipe_id '
173183
'AND JSON_CONTAINS(?, JSON_QUOTE(rtg.tag))',
174-
p_limit, p_offset, p_language_iso_code, NULL, NULL, NULL
184+
p_limit, p_offset, p_language_iso_code, NULL, NULL, NULL, NULL
175185
);
176186
END //
177187

@@ -188,7 +198,8 @@ BEGIN
188198

189199
CALL get_recipes_paginated(
190200
'AND rt.title LIKE ?',
191-
p_limit, p_offset, p_language_iso_code, NULL, NULL, NULL
201+
p_limit, p_offset, p_language_iso_code, NULL, NULL, NULL,
202+
v_safe_recipe_name
192203
);
193204
END //
194205

0 commit comments

Comments
 (0)