From 746090e1abb7f5df1b542f1932de348acb80f22b Mon Sep 17 00:00:00 2001 From: igor Date: Thu, 12 Feb 2026 00:37:48 +0100 Subject: [PATCH] added DB tables - users - ingredients - meals - meal_items - diary_days - diary_entries --- backend/src/Maintenance.php | 91 ++++++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-) diff --git a/backend/src/Maintenance.php b/backend/src/Maintenance.php index f4d7024..7da6ba9 100644 --- a/backend/src/Maintenance.php +++ b/backend/src/Maintenance.php @@ -16,7 +16,96 @@ class Maintenance extends \TPsoft\DBmodel\Maintenance } $dbver = $this->dbver(); if ($dbver == 1) { - // create new + $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; } }