From 84337e8a5039201179c10082acfc1bb71f244a64 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 12 Jul 2026 19:16:49 -0400 Subject: [PATCH] fix test issues --- app/Controllers/Api/AssignmentController.php | 150 ++++++++++++++++++ app/Models/AssignmentModel.php | 53 +++++++ composer.json | 4 +- .../Api/AssignmentControllerTest.php | 4 +- 4 files changed, 208 insertions(+), 3 deletions(-) create mode 100644 app/Controllers/Api/AssignmentController.php create mode 100644 app/Models/AssignmentModel.php diff --git a/app/Controllers/Api/AssignmentController.php b/app/Controllers/Api/AssignmentController.php new file mode 100644 index 0000000..29958dd --- /dev/null +++ b/app/Controllers/Api/AssignmentController.php @@ -0,0 +1,150 @@ +assignmentModel = new AssignmentModel(); + } + + public function setRequest(RequestInterface $request): void + { + $this->request = $request; + } + + public function index() + { + $page = (int) ($this->request->getGet('page') ?? 1); + $perPage = (int) ($this->request->getGet('per_page') ?? 20); + + foreach (['class_id', 'teacher_id', 'student_id'] as $filter) { + $value = $this->request->getGet($filter); + + if ($value !== null && $value !== '') { + $this->assignmentModel->where($filter, $value); + } + } + + $assignments = $this->assignmentModel->paginate(max(1, $perPage), 'default', max(1, $page)); + $pager = $this->assignmentModel->pager ?? null; + + return $this->respond([ + 'data' => $assignments, + 'pagination' => [ + 'current_page' => $pager ? (int) $pager->getCurrentPage() : max(1, $page), + 'per_page' => $pager ? (int) $pager->getPerPage() : max(1, $perPage), + 'total' => $pager ? (int) $pager->getTotal() : count($assignments), + 'total_pages' => $pager ? (int) $pager->getPageCount() : 1, + ], + ], ResponseInterface::HTTP_OK, 'Assignments retrieved successfully'); + } + + public function show(int $id) + { + $assignment = $this->assignmentModel->find($id); + + if (! $assignment) { + return $this->respond(null, ResponseInterface::HTTP_NOT_FOUND, 'Assignment not found'); + } + + return $this->respond($assignment, ResponseInterface::HTTP_OK, 'Assignment retrieved successfully'); + } + + public function store() + { + $payload = $this->request->getJSON(true) ?? []; + $rules = [ + 'title' => 'required|string|max_length[255]', + 'due_date' => 'required|valid_date', + 'class_id' => 'required|integer', + 'teacher_id' => 'required|integer', + ]; + + if (! $this->validate($rules)) { + return [ + 'status' => false, + 'code' => ResponseInterface::HTTP_UNPROCESSABLE_ENTITY, + 'message' => 'Validation failed', + 'errors' => $this->validator->getErrors(), + ]; + } + + $id = $this->assignmentModel->insert($payload); + + return $this->respond( + $this->assignmentModel->find($id), + ResponseInterface::HTTP_CREATED, + 'Assignment created successfully' + ); + } + + public function update(int $id) + { + if (! $this->assignmentModel->find($id)) { + return $this->respond(null, ResponseInterface::HTTP_NOT_FOUND, 'Assignment not found'); + } + + $payload = $this->request->getJSON(true) ?? []; + $rules = [ + 'title' => 'permit_empty|string|max_length[255]', + 'description' => 'permit_empty|string', + 'due_date' => 'permit_empty|valid_date', + 'class_id' => 'permit_empty|integer', + 'teacher_id' => 'permit_empty|integer', + 'student_id' => 'permit_empty|integer', + ]; + + if (! $this->validate($rules)) { + return [ + 'status' => false, + 'code' => ResponseInterface::HTTP_UNPROCESSABLE_ENTITY, + 'message' => 'Validation failed', + 'errors' => $this->validator->getErrors(), + ]; + } + + $this->assignmentModel->update($id, $payload); + + return $this->respond( + $this->assignmentModel->find($id), + ResponseInterface::HTTP_OK, + 'Assignment updated successfully' + ); + } + + public function delete(int $id) + { + if (! $this->assignmentModel->find($id)) { + return $this->respond(null, ResponseInterface::HTTP_NOT_FOUND, 'Assignment not found'); + } + + $this->assignmentModel->delete($id); + + return $this->respond(null, ResponseInterface::HTTP_OK, 'Assignment deleted successfully'); + } + + public function student(int $studentId) + { + $assignments = $this->assignmentModel + ->where('student_id', $studentId) + ->findAll(); + + return $this->respond( + $assignments, + ResponseInterface::HTTP_OK, + 'Assignments retrieved successfully for student' + ); + } +} diff --git a/app/Models/AssignmentModel.php b/app/Models/AssignmentModel.php new file mode 100644 index 0000000..27b789d --- /dev/null +++ b/app/Models/AssignmentModel.php @@ -0,0 +1,53 @@ + 'required|string|max_length[255]', + 'due_date' => 'required|valid_date', + 'class_id' => 'required|integer', + 'teacher_id' => 'required|integer', + ]; + protected $validationMessages = []; + protected $skipValidation = false; + protected $cleanValidationRules = true; + protected $allowCallbacks = true; + protected $beforeInsert = []; + protected $afterInsert = []; + protected $beforeUpdate = []; + protected $afterUpdate = []; + protected $beforeFind = []; + protected $afterFind = []; + protected $beforeDelete = []; + protected $afterDelete = []; +} diff --git a/composer.json b/composer.json index 203fea4..d108f61 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,9 @@ "Config\\": "app/Config/" }, "exclude-from-classmap": [ - "**/Database/Migrations/**" + "**/Database/Migrations/**", + "app/ThirdParty/fpdf/", + "app/Views/invoice_payment/pdf_template.php" ] }, "autoload-dev": { diff --git a/tests/app/Controllers/Api/AssignmentControllerTest.php b/tests/app/Controllers/Api/AssignmentControllerTest.php index 748cc8d..b79ecea 100644 --- a/tests/app/Controllers/Api/AssignmentControllerTest.php +++ b/tests/app/Controllers/Api/AssignmentControllerTest.php @@ -40,7 +40,7 @@ class FakeGetRequest extends MockIncomingRequest } } -class StubPager +class AssignmentStubPager { public function __construct(private readonly int $current, private readonly int $perPage, private readonly int $total) { @@ -84,7 +84,7 @@ class StubAssignmentModel public function paginate(int $perPage, string $group, int $page) { - $this->pager = new StubPager($this->pagerData['current'], $this->pagerData['perPage'], $this->pagerData['total']); + $this->pager = new AssignmentStubPager($this->pagerData['current'], $this->pagerData['perPage'], $this->pagerData['total']); return $this->findAllResult; }