From ab52d44c94c9d00a70b06947d243f821f0945b97 Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Fri, 28 Aug 2026 04:03:01 +0300 Subject: [PATCH] fix: Auto-seed official subjects table to satisfy foreign key constraints on courses creation --- backend/app/Controllers/TeacherController.php | 2 ++ backend/app/Services/CurriculumService.php | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/backend/app/Controllers/TeacherController.php b/backend/app/Controllers/TeacherController.php index 6c40214..b1dab8a 100644 --- a/backend/app/Controllers/TeacherController.php +++ b/backend/app/Controllers/TeacherController.php @@ -7,6 +7,7 @@ use App\Core\Response; use App\Core\Database; use App\Core\Security; use App\Core\Validator; +use App\Services\CurriculumService; class TeacherController { @@ -167,6 +168,7 @@ class TeacherController */ public function getCourses(Request $request, Response $response): void { + CurriculumService::ensureSchema(); $userId = (int)$request->user_id; $courses = Database::select( diff --git a/backend/app/Services/CurriculumService.php b/backend/app/Services/CurriculumService.php index 1cd78ea..de9d831 100644 --- a/backend/app/Services/CurriculumService.php +++ b/backend/app/Services/CurriculumService.php @@ -17,7 +17,23 @@ class CurriculumService if (self::$schemaChecked) return; try { - // Check if lessons table has timeline_chapters_json column + // 1. Check if subjects table has data, seed if empty + $subjectsCount = (int)(Database::selectOne("SELECT COUNT(*) as total FROM subjects")['total'] ?? 0); + if ($subjectsCount === 0) { + Database::query( + "INSERT INTO subjects (id, name, code, stream, is_active) VALUES + (1, 'الرياضيات العلمي', 'math_scientific', 'scientific', 1), + (2, 'الثقافة العسكرية والتربية الوطنية', 'military_culture', 'common', 1), + (3, 'الفيزياء', 'physics_scientific', 'scientific', 1), + (4, 'الكيمياء', 'chemistry_scientific', 'scientific', 1), + (5, 'العلوم الحياتية (الأحياء)', 'biology_scientific', 'scientific', 1), + (6, 'اللغة الإنجليزية', 'english_common', 'common', 1), + (7, 'اللغة العربية المشتركة', 'arabic_common', 'common', 1) + ON DUPLICATE KEY UPDATE name = VALUES(name)" + ); + } + + // 2. Check if lessons table has timeline_chapters_json column $cols = Database::select("SHOW COLUMNS FROM lessons LIKE 'timeline_chapters_json'"); if (empty($cols)) { Database::query("ALTER TABLE lessons ADD COLUMN timeline_chapters_json JSON NULL AFTER hls_url");