Merge pull request 'Create models for the recipe app and connect them to the database' (#3) from TIST-5 into develop

Reviewed-on: #3
Reviewed-by: greendavid004 <davidkatrinka1995@gmail.com>
This commit is contained in:
steve_dekart 2025-06-15 18:25:22 +02:00
commit 82b4c98f29
10 changed files with 118 additions and 2 deletions

View File

@ -0,0 +1,10 @@
<?php
namespace Lycoreco\Apps\Recipes\Controllers;
use Lycoreco\Includes\BaseController;
class CatalogController extends BaseController
{
protected $template_name = APPS_PATH . '/Recipes/Templates/catalog.php';
}

View File

@ -0,0 +1,10 @@
<?php
namespace Lycoreco\Apps\Recipes\Controllers;
use Lycoreco\Includes\BaseController;
class SingleRecipeController extends BaseController
{
protected $template_name = APPS_PATH . '/Recipes/Templates/single.php';
}

View File

@ -5,5 +5,20 @@ use Lycoreco\Includes\Model\BaseModel;
class CategoryModel extends BaseModel class CategoryModel extends BaseModel
{ {
public $field_name;
static protected $table_name = 'categories';
static protected $table_fields = [
'id' => 'int',
'name' => 'string',
];
public static function init_table()
{
$result = db_query('CREATE TABLE ' . static::$table_name . ' (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);');
return $result;
}
} }

View File

@ -5,5 +5,29 @@ use Lycoreco\Includes\Model\BaseModel;
class IngredientInRecipeModel extends BaseModel class IngredientInRecipeModel extends BaseModel
{ {
public $field_ingredient_id;
public $field_receipt_id;
public $field_amount;
static protected $table_name = 'recipe_ingredients';
static protected $table_fields = [
'id' => 'int',
'ingredient_id' => 'int',
'receipt_id' => 'int',
'amount' => 'int'
];
public static function init_table()
{
$result = db_query('CREATE TABLE ' . static::$table_name . ' (
id INT AUTO_INCREMENT PRIMARY KEY,
ingredient_id INT NOT NULL,
receipt_id INT NOT NULL,
amount INT NOT NULL,
FOREIGN KEY (ingredient_id) REFERENCES ingredients(id) ON DELETE CASCADE,
FOREIGN KEY (receipt_id) REFERENCES recipes(id) ON DELETE CASCADE
);');
return $result;
}
} }

View File

@ -5,5 +5,23 @@ use Lycoreco\Includes\Model\BaseModel;
class IngredientModel extends BaseModel class IngredientModel extends BaseModel
{ {
public $field_name;
public $field_unit_name;
static protected $table_name = 'ingredients';
static protected $table_fields = [
'id' => 'int',
'name' => 'string',
'unit_name' => 'string'
];
public static function init_table()
{
$result = db_query('CREATE TABLE ' . static::$table_name . ' (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
unit_name VARCHAR(20) NULL
);');
return $result;
}
} }

View File

@ -13,4 +13,33 @@ class RecipeModel extends BaseModel
public $field_author_id; public $field_author_id;
public $field_status; public $field_status;
public $field_created_at; public $field_created_at;
static protected $table_name = 'recipes';
static protected $table_fields = [
'id' => 'int',
'title' => 'string',
'instruction' => 'string',
'estimated_time' => 'int',
'estimated_price' => 'int',
'author_id' => 'int',
'status' => 'string',
'created_at' => 'DateTime'
];
public static function init_table()
{
$result = db_query('CREATE TABLE ' . static::$table_name . ' (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
instruction TEXT NOT NULL,
estimated_time INT NOT NULL,
estimated_price INT NOT NULL,
author_id INT NOT NULL,
status VARCHAR(20) NOT NULL CHECK (status IN (\'publish\', \'pending\')),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (author_id) REFERENCES users(id) ON DELETE CASCADE
);');
return $result;
}
} }

View File

@ -0,0 +1 @@
Catalog page

View File

@ -0,0 +1 @@
Single Recipe item

View File

@ -1,3 +1,9 @@
<?php <?php
use Lycoreco\Apps\Recipes\Controllers;
use Lycoreco\Includes\Routing\Path; use Lycoreco\Includes\Routing\Path;
$recipes_urls = [
new Path('/recipe/[:string]', new Controllers\SingleRecipeController(), 'single'),
new Path('/catalog', new Controllers\CatalogController(), 'catalog'),
];

View File

@ -5,11 +5,13 @@ require APPS_PATH . '/Index/urls.php';
require APPS_PATH . '/Users/urls.php'; require APPS_PATH . '/Users/urls.php';
require APPS_PATH . '/Admin/urls.php'; require APPS_PATH . '/Admin/urls.php';
require APPS_PATH . '/Ajax/urls.php'; require APPS_PATH . '/Ajax/urls.php';
require APPS_PATH . '/Recipes/urls.php';
Router::includes($index_urls, "index"); Router::includes($index_urls, "index");
Router::includes($users_urls, 'users'); Router::includes($users_urls, 'users');
Router::includes($admin_urls, 'admin'); Router::includes($admin_urls, 'admin');
Router::includes($ajax_urls, 'ajax'); Router::includes($ajax_urls, 'ajax');
Router::includes($recipes_urls, 'recipes');
// Router::set_error_controller('default', new ErrorController()) // Router::set_error_controller('default', new ErrorController())
?> ?>