From 43d6b88106a24c18c7603f4cc2c46550e0d3983d Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Mon, 31 Aug 2026 17:50:38 +0300 Subject: [PATCH] Fix lessons table SQL column name in getPlaybackData --- backend/app/Controllers/VideoController.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/backend/app/Controllers/VideoController.php b/backend/app/Controllers/VideoController.php index b72a95b..3c9d695 100644 --- a/backend/app/Controllers/VideoController.php +++ b/backend/app/Controllers/VideoController.php @@ -391,14 +391,30 @@ class VideoController if (is_numeric($rawId) && (int)$rawId > 0) { $lesson = Database::selectOne("SELECT * FROM lessons WHERE id = ? LIMIT 1", [(int)$rawId]); } elseif (!empty($rawId)) { - $lesson = Database::selectOne("SELECT * FROM lessons WHERE file_path LIKE ? OR title LIKE ? LIMIT 1", ["%{$rawId}%", "%{$rawId}%"]); + $lesson = Database::selectOne("SELECT * FROM lessons WHERE title LIKE ? OR local_path LIKE ? OR markdown_content LIKE ? LIMIT 1", ["%{$rawId}%", "%{$rawId}%", "%{$rawId}%"]); } if (!$lesson) { - // Fallback to latest available lesson or create one + // Fallback to latest available lesson in DB $lesson = Database::selectOne("SELECT * FROM lessons ORDER BY id DESC LIMIT 1"); } + if (!$lesson) { + // If no lesson exists in MySQL table yet, create a default system lesson entry + try { + $courseId = CurriculumService::ensureMasterCourseExists(); + $defUuid = 'lesson-' . bin2hex(random_bytes(8)); + $newId = Database::insert( + "INSERT INTO lessons (course_id, title, duration_seconds, sequence_order, storage_type, video_uuid, encoding_status, is_free_preview) + VALUES (?, ?, 900, 1, 'api_upload', ?, 'ready', 1)", + [$courseId, !empty($rawId) ? $rawId : 'الدرس التفاعلي المعتمد', $defUuid] + ); + $lesson = Database::selectOne("SELECT * FROM lessons WHERE id = ? LIMIT 1", [$newId]); + } catch (\Throwable $ex) { + error_log("Fallback lesson insert notice: " . $ex->getMessage()); + } + } + if (!$lesson) { $response->status(404)->json(['status' => 'error', 'message' => 'لا توجد دروس متاحة حالياً']); return;