Implement Enterprise Logic (Redis, Phone Auth, Full Schema)
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Request;
|
||||
use App\Core\Response;
|
||||
use App\Core\Database;
|
||||
|
||||
class TeacherController
|
||||
{
|
||||
public function addCourse(Request $request, Response $response): void
|
||||
{
|
||||
if ($request->role !== 'teacher' && $request->role !== 'admin') {
|
||||
$response->status(403)->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Forbidden: Only teachers can add courses'
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
$body = $request->getBody();
|
||||
$title = $body['title'] ?? '';
|
||||
$description = $body['description'] ?? '';
|
||||
|
||||
if (empty($title)) {
|
||||
$response->status(400)->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Course title is required'
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
$courseId = Database::insert(
|
||||
"INSERT INTO courses (teacher_id, title, description) VALUES (?, ?, ?)",
|
||||
[$request->user_id, $title, $description]
|
||||
);
|
||||
|
||||
$response->status(201)->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Course added successfully',
|
||||
'data' => [
|
||||
'course_id' => $courseId
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
public function addLesson(Request $request, Response $response): void
|
||||
{
|
||||
if ($request->role !== 'teacher' && $request->role !== 'admin') {
|
||||
$response->status(403)->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Forbidden: Only teachers can add lessons'
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
$body = $request->getBody();
|
||||
$courseId = $body['course_id'] ?? null;
|
||||
$title = $body['title'] ?? '';
|
||||
$content = $body['content'] ?? '';
|
||||
$videoUrl = $body['video_url'] ?? null;
|
||||
$orderNum = $body['order_num'] ?? 1;
|
||||
|
||||
if (!$courseId || empty($title) || empty($content)) {
|
||||
$response->status(400)->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Course ID, title, and content are required'
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
// Verify the course belongs to the teacher
|
||||
$course = Database::selectOne("SELECT id, teacher_id FROM courses WHERE id = ? LIMIT 1", [$courseId]);
|
||||
if (!$course || ($course['teacher_id'] != $request->user_id && $request->role !== 'admin')) {
|
||||
$response->status(403)->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Forbidden: You do not own this course'
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
$lessonId = Database::insert(
|
||||
"INSERT INTO lessons (course_id, title, content, video_url, order_num) VALUES (?, ?, ?, ?, ?)",
|
||||
[$courseId, $title, $content, $videoUrl, $orderNum]
|
||||
);
|
||||
|
||||
$response->status(201)->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Lesson added successfully',
|
||||
'data' => [
|
||||
'lesson_id' => $lessonId
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user