Files
Nutrio/backend/src/Maintenance.php
igor 2d96baa389 added TOKEN for users,
added user*() method for API,
added check TOKEN for all methods in API
2026-02-12 02:11:07 +01:00

154 lines
5.2 KiB
PHP

<?php
namespace TPsoft\Nutrio;
class Maintenance extends \TPsoft\DBmodel\Maintenance
{
public function database()
{
if (!$this->existsTable('options')) {
$this->checkDBTable('options', '
`key` VARCHAR(255) NOT NULL PRIMARY KEY,
`value` VARCHAR(255) NOT NULL
');
$this->dbver(1);
}
$dbver = $this->dbver();
if ($dbver == 1) {
$this->checkDBTable('users', '
`user_id` BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
`email` VARCHAR(255) NOT NULL UNIQUE,
`password_hash` VARCHAR(255) NOT NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
');
$this->dbver(2);
$dbver = 2;
}
if ($dbver == 2) {
$this->checkDBTable('ingredients', '
`ingredient_id` BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
`user_id` BIGINT UNSIGNED NULL, -- NULL = nikoho surovina, 0 = globalna, alebo viazaná na usera
`name` VARCHAR(255) NOT NULL,
-- hodnoty na 100g
`protein_g_100` DECIMAL(7,2) NOT NULL DEFAULT 0, -- bielkoviny
`carbs_g_100` DECIMAL(7,2) NOT NULL DEFAULT 0, -- sacharidy
`sugar_g_100` DECIMAL(7,2) NOT NULL DEFAULT 0, -- cukry (subset carbs)
`fat_g_100` DECIMAL(7,2) NOT NULL DEFAULT 0, -- tuky
`fiber_g_100` DECIMAL(7,2) NOT NULL DEFAULT 0, -- voliteľné
-- voliteľne: kcal na 100g (môžeš aj dopočítať z makier)
`kcal_100` DECIMAL(8,2) NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT fk_ingredients_user
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE SET NULL,
INDEX idx_ingredients_user_name (user_id, name)
');
$this->dbver(3);
$dbver = 3;
}
if ($dbver == 3) {
$this->checkDBTable('meals', '
`meal_id` BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
`user_id` BIGINT UNSIGNED NOT NULL,
`name` VARCHAR(255) NOT NULL,
`meal_type` ENUM("breakfast","lunch","dinner") NOT NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT fk_meals_user
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
INDEX idx_meals_user_type (user_id, meal_type)
');
$this->dbver(4);
$dbver = 4;
}
if ($dbver == 4) {
$this->checkDBTable('meal_items', '
`meal_item_id` BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
`meal_id` BIGINT UNSIGNED NOT NULL,
`ingredient_id` BIGINT UNSIGNED NOT NULL,
`grams` DECIMAL(10,2) NOT NULL, -- zadaná hmotnosť
`position` INT UNSIGNED NOT NULL DEFAULT 1, -- poradie v UI
CONSTRAINT fk_meal_items_meal
FOREIGN KEY (meal_id) REFERENCES meals(meal_id) ON DELETE CASCADE,
CONSTRAINT fk_meal_items_ingredient
FOREIGN KEY (ingredient_id) REFERENCES ingredients(ingredient_id) ON DELETE RESTRICT,
INDEX idx_meal_items_meal (meal_id),
INDEX idx_meal_items_ingredient (ingredient_id)
');
$this->dbver(5);
$dbver = 5;
}
if ($dbver == 5) {
$this->checkDBTable('diary_days', '
`diary_day_id` BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
`user_id` BIGINT UNSIGNED NOT NULL,
`day_date` DATE NOT NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT fk_diary_days_user
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
UNIQUE KEY uniq_user_day (user_id, day_date)
');
$this->dbver(6);
$dbver = 6;
}
if ($dbver == 6) {
$this->checkDBTable('diary_entries', '
`diary_entry_id` BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
`diary_day_id` BIGINT UNSIGNED NOT NULL,
`meal_type` ENUM("breakfast","lunch","dinner") NOT NULL,
`meal_id` BIGINT UNSIGNED NOT NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT fk_diary_entries_day
FOREIGN KEY (diary_day_id) REFERENCES diary_days(diary_day_id) ON DELETE CASCADE,
CONSTRAINT fk_diary_entries_meal
FOREIGN KEY (meal_id) REFERENCES meals(meal_id) ON DELETE RESTRICT,
UNIQUE KEY uniq_day_mealtype (diary_day_id, meal_type),
INDEX idx_diary_entries_meal (meal_id)
');
$this->dbver(7);
$dbver = 7;
}
if ($dbver == 7) {
$this->checkDBAdd('users', 'token', 'VARCHAR(255) DEFAULT NULL AFTER `password_hash`');
$this->checkDBAdd('users', 'token_expires', 'DATETIME DEFAULT NULL AFTER `token`');
$this->dbver(8);
$dbver = 8;
}
}
protected function settings(string $key, ?string $value = null): string|false
{
if (is_null($value)) {
return $this->dbh->getOne(sprintf('SELECT `value` FROM `options` WHERE `key` = %s', $this->dbh->quote($key)));
} else {
$db_type = $this->dbh->getDBtype();
switch ($db_type) {
case 'mysql':
return $this->dbh->query(sprintf(
'INSERT INTO `options` (`key`, `value`) VALUES (%s, %s) ON DUPLICATE KEY UPDATE `value` = %s',
$this->dbh->quote($key),
$this->dbh->quote($value),
$this->dbh->quote($value)
)) !== false;
break;
case 'sqlite':
return $this->dbh->query(sprintf(
'INSERT INTO `options` (`key`, `value`) VALUES (%s, %s) ON CONFLICT(`key`) DO UPDATE SET `value` = %s',
$this->dbh->quote($key),
$this->dbh->quote($value),
$this->dbh->quote($value)
)) !== false;
break;
default:
new \Exception('Unknown DB type: ' . $db_type);
return false;
break;
}
}
}
protected function dbver(?string $ver = null)
{
return $this->settings('version', $ver);
}
}