Compare commits

...

2 Commits

Author SHA1 Message Date
3e9fdd36bb Merge pull request 'TIST-28: Create ajax endpoint to create new ingredient and search ingredients' (#20) from TIST-28 into develop
Reviewed-on: #20
Reviewed-by: greendavid004 <davidkatrinka1995@gmail.com>
2025-06-29 17:35:27 +02:00
95e41b813f Added ajax endpoints to search and create ingredient 2025-06-29 15:02:57 +02:00

View File

@ -1,6 +1,7 @@
<?php <?php
use Lycoreco\Apps\Recipes\Models\{ use Lycoreco\Apps\Recipes\Models\{
IngredientModel,
RecipeModel, RecipeModel,
RecipeUserMenu RecipeUserMenu
}; };
@ -30,12 +31,8 @@ function ajax_search()
$result = array(); $result = array();
$recipes = RecipeModel::filter( $recipes = RecipeModel::filter(
array(), count: 5,
array(), search: $search_query
5,
'AND',
0,
$search_query
); );
$result['count'] = count($recipes); $result['count'] = count($recipes);
@ -97,3 +94,44 @@ function ajax_usermenu()
$result['success'] = 'You have successfully added the recipe to your menu.'; $result['success'] = 'You have successfully added the recipe to your menu.';
return $result; return $result;
} }
function ajax_create_ingredient()
{
$ingredient_name = $_POST['name'] ?? null;
$ingredient_unit = $_POST['unit'] ?? null;
if (!CURRENT_USER) {
return get_ajax_error('You are not authorized', 401);
}
if (!isset($ingredient_name)) {
return get_ajax_error("Missing 'name' parameter.", 400);
}
if (!isset($ingredient_unit)) {
return get_ajax_error("Missing 'unit' parameter.", 400);
}
$ingredient = new IngredientModel();
$ingredient->field_name = $ingredient_name;
$ingredient->field_unit_name = $ingredient_unit;
$ingredient->save();
$result = array();
$result['success'] = 'You have successfully added new ingredient';
return $result;
}
function ajax_search_ingredient()
{
$search_query = $_POST['query'] ?? null;
if (!isset($search_query)) {
return get_ajax_error("Missing 'query' parameter.", 400);
}
$result = array();
$ingredients = IngredientModel::filter(
count: 5,
search: $search_query
);
$result['count'] = count($ingredients);
$result['result'] = $ingredients;
return $result;
}