Completed web programming project #44
@ -3,6 +3,7 @@
|
||||
namespace Lycoreco\Apps\Ajax\Controllers;
|
||||
|
||||
use Lycoreco\Includes\BaseController;
|
||||
use Lycoreco\Includes\Model\ValidationError;
|
||||
|
||||
class AjaxController extends BaseController
|
||||
{
|
||||
@ -32,8 +33,15 @@ class AjaxController extends BaseController
|
||||
|
||||
try {
|
||||
$context['result'] = $action();
|
||||
} catch (\Exception $ex) {
|
||||
}
|
||||
catch (ValidationError $ex) {
|
||||
$context['result'] = get_ajax_error($ex->getMessage(), 400);
|
||||
return $context;
|
||||
}
|
||||
catch (\Exception $ex) {
|
||||
http_response_code(500);
|
||||
$context['result'] = get_ajax_error($ex->getMessage());
|
||||
return $context;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
<?php
|
||||
echo $context['result'];
|
||||
echo json_encode($context['result'], JSON_PRETTY_PRINT);
|
||||
@ -1,6 +1,9 @@
|
||||
<?php
|
||||
|
||||
use Lycoreco\Apps\Recipes\Models\RecipeModel;
|
||||
use Lycoreco\Apps\Recipes\Models\{
|
||||
RecipeModel,
|
||||
RecipeUserMenu
|
||||
};
|
||||
|
||||
function get_ajax_error($message, $error_code = 500)
|
||||
{
|
||||
@ -9,13 +12,14 @@ function get_ajax_error($message, $error_code = 500)
|
||||
$error = array();
|
||||
$error['error'] = $message;
|
||||
|
||||
return json_encode($error, JSON_PRETTY_PRINT);
|
||||
return $error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ajax actions
|
||||
*/
|
||||
function ajax_search() {
|
||||
function ajax_search()
|
||||
{
|
||||
$search_query = $_POST['query'] ?? null;
|
||||
if (!isset($search_query)) {
|
||||
return get_ajax_error("Missing 'query' parameter.", 400);
|
||||
@ -37,5 +41,59 @@ function ajax_search() {
|
||||
$result['count'] = count($recipes);
|
||||
$result['result'] = $recipes;
|
||||
|
||||
return json_encode($result, JSON_PRETTY_PRINT);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
function ajax_usermenu()
|
||||
{
|
||||
$recipe_id = $_POST['recipe_id'] ?? null;
|
||||
$dayofweek = $_POST['dayofweek'] ?? null;
|
||||
|
||||
if (!CURRENT_USER) {
|
||||
return get_ajax_error('You are not authorized', 401);
|
||||
}
|
||||
if (!isset($recipe_id)) {
|
||||
return get_ajax_error("Missing 'recipe_id' parameter.", 400);
|
||||
}
|
||||
if (!isset($dayofweek)) {
|
||||
return get_ajax_error("Missing 'dayofweek' parameter.", 400);
|
||||
}
|
||||
$result = array();
|
||||
|
||||
$user_menu = RecipeUserMenu::get(array(
|
||||
[
|
||||
'name' => 'obj.recipe_id',
|
||||
'type' => '=',
|
||||
'value' => $recipe_id
|
||||
],
|
||||
[
|
||||
'name' => 'obj.user_id',
|
||||
'type' => '=',
|
||||
'value' => CURRENT_USER->get_id()
|
||||
]
|
||||
));
|
||||
// If user choose optiopn 'remove'
|
||||
if($dayofweek == 'remove') {
|
||||
if($user_menu) {
|
||||
$user_menu->delete();
|
||||
$result['success'] = 'This recipe was removed from your list';
|
||||
return $result;
|
||||
}
|
||||
else {
|
||||
return get_ajax_error("This recipe in your menu is not exists", 400);
|
||||
}
|
||||
}
|
||||
|
||||
// If not exists, add new recipe in user menu
|
||||
if(!$user_menu) {
|
||||
$user_menu = new RecipeUserMenu();
|
||||
$user_menu->field_recipe_id = $recipe_id;
|
||||
$user_menu->field_user_id = CURRENT_USER->get_id();
|
||||
}
|
||||
|
||||
$user_menu->field_dayofweek = $dayofweek;
|
||||
$user_menu->save();
|
||||
|
||||
$result['success'] = 'You have successfully added the recipe to your menu.';
|
||||
return $result;
|
||||
}
|
||||
|
||||
57
apps/Recipes/Models/RecipeUserMenu.php
Normal file
57
apps/Recipes/Models/RecipeUserMenu.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace Lycoreco\Apps\Recipes\Models;
|
||||
|
||||
use Lycoreco\Includes\Model\BaseModel;
|
||||
|
||||
class RecipeUserMenu extends BaseModel
|
||||
{
|
||||
public $field_dayofweek;
|
||||
public $field_recipe_id;
|
||||
public $field_user_id;
|
||||
public $field_created_at;
|
||||
|
||||
static protected $table_name = 'recipe_usermenu';
|
||||
static protected $table_fields = [
|
||||
'id' => 'int',
|
||||
'dayofweek' => 'string',
|
||||
'recipe_id' => 'int',
|
||||
'user_id' => 'int',
|
||||
'created_at' => 'DateTime'
|
||||
];
|
||||
|
||||
public static function init_table()
|
||||
{
|
||||
$result = db_query('CREATE TABLE ' . static::$table_name . ' (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
|
||||
dayofweek VARCHAR(150) NOT NULL,
|
||||
recipe_id INT NOT NULL,
|
||||
user_id INT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
FOREIGN KEY (recipe_id) REFERENCES recipes(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
);');
|
||||
return $result;
|
||||
}
|
||||
public function valid()
|
||||
{
|
||||
require_once INCLUDES_PATH . '/Const/recipes.php';
|
||||
|
||||
if(!in_array($this->field_dayofweek, DAY_OF_WEEKS))
|
||||
return ['Day of Week is not valid'];
|
||||
|
||||
$recipe = RecipeModel::get(array(
|
||||
[
|
||||
'name' => 'obj.id',
|
||||
'type' => '=',
|
||||
'value' => $this->field_recipe_id
|
||||
]
|
||||
));
|
||||
if(!$recipe)
|
||||
return ['Recipe is not exists'];
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
5
includes/Const/recipes.php
Normal file
5
includes/Const/recipes.php
Normal file
@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
define('DAY_OF_WEEKS', array(
|
||||
'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'
|
||||
));
|
||||
Loading…
x
Reference in New Issue
Block a user