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>
This commit is contained in:
commit
3e9fdd36bb
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Lycoreco\Apps\Recipes\Models\{
|
||||
IngredientModel,
|
||||
RecipeModel,
|
||||
RecipeUserMenu
|
||||
};
|
||||
@ -30,12 +31,8 @@ function ajax_search()
|
||||
$result = array();
|
||||
|
||||
$recipes = RecipeModel::filter(
|
||||
array(),
|
||||
array(),
|
||||
5,
|
||||
'AND',
|
||||
0,
|
||||
$search_query
|
||||
count: 5,
|
||||
search: $search_query
|
||||
);
|
||||
|
||||
$result['count'] = count($recipes);
|
||||
@ -97,3 +94,44 @@ function ajax_usermenu()
|
||||
$result['success'] = 'You have successfully added the recipe to your menu.';
|
||||
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;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user