From e7f568f8fa94eb8fe93a889bc7c37803c646a70c Mon Sep 17 00:00:00 2001 From: mikael-zakaria Date: Thu, 14 Aug 2025 09:17:15 +0700 Subject: [PATCH] Menambahkan Endpoint patientIdCheck --- app/Config/Routes.php | 1 + app/Controllers/Patient.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/app/Config/Routes.php b/app/Config/Routes.php index a8fdf30..c95ac24 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -19,6 +19,7 @@ $routes->post('/api/patient', 'Patient::create'); $routes->get('/api/patient/(:num)', 'Patient::show/$1'); $routes->delete('/api/patient/(:num)', 'Patient::delete/$1'); $routes->patch('/api/patient/(:num)', 'Patient::update/$1'); +$routes->patch('/api/patient/patid-check/(:any)', 'Patient::patientIdCheck/$1'); $routes->get('/api/race', 'Race::index'); $routes->get('/api/race/(:num)', 'Race::show/$1'); diff --git a/app/Controllers/Patient.php b/app/Controllers/Patient.php index 9e4ab09..e11189e 100644 --- a/app/Controllers/Patient.php +++ b/app/Controllers/Patient.php @@ -488,6 +488,38 @@ class Patient extends Controller { } } + public function patientIdCheck($PatientID = null) { + try { + if (empty($PatientID)) { + return $this->failValidationError('PatientID is required.'); + } + + $patient = $this->db->table('patient') + ->where('PatientID', $PatientID) + ->get() + ->getRowArray(); + + if (!$patient) { + return $this->respond([ + 'status' => 'success', + 'message' => "PatientID {$PatientID} not found.", + 'data' => true, + ], 200); + } + + return $this->respond([ + 'status' => 'success', + 'message' => "PatientID {$PatientID} already exists.", + 'data' => false, + ], 200); + + } catch (\Throwable $e) { + log_message('error', $e->getMessage()); + return $this->failServerError('Something went wrong. Please try again later.'); + } + } + + // Ubah ke format Years Months Days private function calculateAgeFromBirthdate($birthdate) { $dob = new \DateTime($birthdate);