add projet
This commit is contained in:
Executable
+63
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Models\ClassModel;
|
||||
use App\Models\StudentClass;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class ClassController extends BaseApiController
|
||||
{
|
||||
protected Class $class;
|
||||
protected StudentClass $studentClass;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->class = model(ClassModel::class);
|
||||
$this->studentClass = model(StudentClass::class);
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$page = max(1, (int) ($this->request->getGet('page') ?? 1));
|
||||
$perPage = min(100, max(1, (int) ($this->request->getGet('per_page') ?? 20)));
|
||||
$grade = $this->request->getGet('grade');
|
||||
$teacher = $this->request->getGet('teacher_id');
|
||||
|
||||
$query = $this->class->newQuery();
|
||||
if ($grade) {
|
||||
$query->where('grade', $grade);
|
||||
}
|
||||
if ($teacher) {
|
||||
$query->where('teacher_id', $teacher);
|
||||
}
|
||||
|
||||
$query->orderBy('grade', 'ASC');
|
||||
|
||||
$result = $this->paginate($query, $page, $perPage);
|
||||
return $this->success($result, 'Classes retrieved successfully');
|
||||
}
|
||||
|
||||
public function show($id = null)
|
||||
{
|
||||
$class = $this->class->find($id);
|
||||
if (!$class) {
|
||||
return $this->respondError('Class not found', Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
return $this->success($class, 'Class details retrieved successfully');
|
||||
}
|
||||
|
||||
public function students($id = null)
|
||||
{
|
||||
$students = $this->studentClass->newQuery()
|
||||
->select('students.id', 'students.firstname', 'students.lastname', 'students.school_id')
|
||||
->join('students', 'students.id', '=', 'student_class.student_id')
|
||||
->where('student_class.class_id', $id)
|
||||
->orderBy('students.lastname', 'ASC')
|
||||
->get()
|
||||
->toArray();
|
||||
|
||||
return $this->success($students, 'Students retrieved successfully for class');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user