From 8f8769bceeb4f34f40c095df0e9cf81b3efb8764 Mon Sep 17 00:00:00 2001 From: mahdahar <89adham@gmail.com> Date: Wed, 2 Jul 2025 16:10:37 +0700 Subject: [PATCH] add patient index, show, create, update --- app/Config/Routes.php | 4 + app/Controllers/Patient.php | 174 ++++++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 app/Controllers/Patient.php diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 089b173..4fbf11c 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -10,3 +10,7 @@ use CodeIgniter\Router\RouteCollection; $routes->post('/auth/login/', 'Auth::login'); $routes->post('/auth/change_pass/', 'Auth::change_pass'); $routes->post('/auth/register/', 'Auth::register'); + +$routes->get('/patient', 'Patient::index'); +$routes->post('/patient', 'Patient::create'); +$routes->patch('/patient/(:num)', 'Patient::update/$1'); \ No newline at end of file diff --git a/app/Controllers/Patient.php b/app/Controllers/Patient.php new file mode 100644 index 0000000..52431c7 --- /dev/null +++ b/app/Controllers/Patient.php @@ -0,0 +1,174 @@ +db = \Config\Database::connect(); + } + + public function index() { + $pat_num = $this->request->getVar('pat_num'); + $pat_altnum = $this->request->getVar('pat_altnum'); + $pat_name = $this->request->getVar('pat_name'); + $pat_dob = $this->request->getVar('pat_dob'); + $start_date = $this->request->getVar('start_date'); + $end_date = $this->request->getVar('end_date'); + + $builder = $this->db->table('patients'); + + if ($pat_name !== null) { + $sql = "LOWER(CONCAT_WS(' ', IFNULL(prefix,''), IFNULL(name_first,''), IFNULL(name_middle,''), IFNULL(name_last,''), IFNULL(name_maiden,''), IFNULL(suffix,'')))"; + $rawSql = new RawSql($sql); + $builder->like($rawSql, $pat_name, 'both'); + } + if ($pat_num !== null) { $builder->where('pat_num', $pat_num); } + if ($pat_altnum !== null) { $builder->where('pat_altnum', $pat_altnum); } + if ($pat_dob !== null) { $builder->where('pat_dob', $pat_dob); } + if ($start_date !== null || $end_date !== null) { + $builder->join('requests', 'pat_id=patients.pat_id','left'); + if ($start_date !== null) { $builder->where('requests.req_date >=', $start_date . ' 00:00:00'); } + if ($end_date !== null) { $builder->where('requests.req_date <=', $end_date . ' 23:59:00'); } + } + + $filteredPatients = $builder->get()->getResultArray(); + + if (empty($filteredPatients)) { + return $this->failNotFound('No patient records found matching the criteria.'); + } + + return $this->respond($filteredPatients); + } + + public function show($id = null) { + $builder = $this->db->table('patients'); + $patient = $builder->where('pat_num', $id)->get()->getRowArray(); + + if (empty($patient)) { + return $this->failNotFound('Patient with ID ' . $id . ' not found.'); + } + + return $this->respond($patient); + } + + public function create() { + $rules = [ + 'pat_num' => 'required|is_unique[patients.pat_num]|max_length[50]', + 'name_first' => 'required|min_length[3]|max_length[255]', + 'name_middle' => 'permit_empty', + 'name_maiden' => 'permit_empty', + 'name_last' => 'permit_empty', + 'birth_date' => 'permit_empty|valid_date[Y-m-d]', + 'pat_altnum' => 'permit_empty|max_length[50]', + 'address_1' => 'permit_empty', + 'address_2' => 'permit_empty', + 'address_3' => 'permit_empty', + 'city' => 'permit_empty', + ]; + + $data = $this->request->getJSON(true); + + if (!$this->validate($rules)) { + return $this->failValidationErrors($this->validator->getErrors()); + } + + $datas = [ + 'name_first' => $data['name_first'], + 'name_last' => $data['name_last'], + 'name_middle' => $data['name_middle'] ?? null, + 'name_maiden' => $data['name_maiden'] ?? null, + 'pat_num' => $data['pat_num'], + 'prefix' => $data['prefix'] ?? null, + 'suffix' => $data['suffix'] ?? null, + 'birth_date' => $data['pat_dob'] ?? null, + 'pat_altnum' => $data['pat_altnum'] ?? null, + 'address_1' => $data['address_1'] ?? null, + 'address_2' => $data['address_2'] ?? null, + 'address_3' => $data['address_3'] ?? null, + 'city' => $data['city'] ?? null, + 'province' => $data['province'] ?? null, + 'zip' => $data['zip'] ?? null, + 'email_1' => $data['email_1'] ?? null, + 'email_2' => $data['email_2'] ?? null, + 'phone' => $data['phone'] ?? null, + 'mobile_phone' => $data['mobile_phone'] ?? null, + 'mother' => $data['mother'] ?? null, + 'account_number' => $data['account_number'] ?? null, + 'marital_status' => $data['marital_status'] ?? null, + 'country_id' => $data['country_id'] ?? null, + 'race_id' => $data['race_id'] ?? null, + 'religion_id' => $data['religion_id'] ?? null, + 'ethnic_id' => $data['ethnic_id'] ?? null, + 'citizenship' => $data['citizenship'] ?? null, + 'death' => $data['death'] ?? null, + 'death_date' => $data['death_date'] ?? null, + 'create_date' => date('Y-m-d H:i:s'), + ]; + + $this->db->table('patients')->insert($datas); + $newPatientId = $this->db->insertID(); + + return $this->respondCreated([ + 'message' => 'Patient created successfully', + 'pat_id' => $newPatientId + ]); + } + + public function update($pat_id = null) { + $data = $this->request->getJSON(true); + + $existingPatient = $this->db->table('patients')->where('pat_id', $pat_id)->get()->getRowArray(); + if (empty($existingPatient)) { + return $this->failNotFound('Patient with ID ' . $pat_id . ' not found.'); + } + + $rules = [ + 'pat_num' => 'required|max_length[50]', + 'name_first' => 'required|min_length[3]|max_length[255]', + 'name_middle' => 'permit_empty', + 'name_maiden' => 'permit_empty', + 'name_last' => 'permit_empty', + 'birth_date' => 'permit_empty|valid_date[Y-m-d]', + 'pat_altnum' => 'permit_empty|max_length[50]', + 'address_1' => 'permit_empty', + 'address_2' => 'permit_empty', + 'address_3' => 'permit_empty', + 'city' => 'permit_empty', + ]; + + // Validate the input data + if (!$this->validate($rules)) { + return $this->failValidationErrors($this->validator->getErrors()); + } + + $allowedUpdateFields = [ + 'name_first', 'name_last', 'name_middle', + 'pat_num', 'pat_altnum', 'birth_date', 'birth_place', + 'address_1', 'address_2', 'address_3', 'city', 'province', 'zip', + 'email_1', 'email_2', 'phone', 'mobile_phone', 'mother', 'account_number' + ]; + + $datas = []; + foreach ($allowedUpdateFields as $field) { + if (isset($data[$field])) { + $datas[$field] = $data[$field]; + } + } + + if (empty($datas)) { + return $this->failValidationError('No data provided for update.'); + } + + $this->db->table('patients')->where('pat_id', $pat_id)->update($datas); + + return $this->respond([ + 'message' => 'Patient updated successfully', + 'pat_id' => $pat_id + ]); + } +} \ No newline at end of file