Update Routes & Controller Patient

This commit is contained in:
mikael-zakaria 2025-08-01 22:18:45 +07:00
parent 0b1b85b206
commit 951d5598ad
2 changed files with 95 additions and 86 deletions

View File

@ -16,6 +16,7 @@ $routes->post('/auth/register/', 'Auth::register');
$routes->get('/api/patient', 'Patient::index');
$routes->post('/api/patient', 'Patient::create');
$routes->get('/api/patient/(:any)', 'Patient::show/$1');
$routes->delete('/api/patient/(:any)', 'Patient::delete/$1');
$routes->patch('/api/patient/(:num)', 'Patient::update/$1');

View File

@ -12,7 +12,7 @@ class Patient extends Controller {
$this->db = \Config\Database::connect();
}
// OK
// OK - Done
public function index() {
try {
$PatientID = $this->request->getVar('PatientID');
@ -67,13 +67,13 @@ class Patient extends Controller {
}
}
// OK
public function show($id = null) {
// OK - Done
public function show($InternalPID = null) {
try {
$builder = $this->db->table('Patient');
$patient = $builder->where('PatientID', $id)->get()->getRowArray();
$patient = $builder->where('InternalPID', $InternalPID)->get()->getRowArray();
// Data pasien tidak ada mengembalikan - success 200
if (empty($patient)) {
@ -99,56 +99,60 @@ class Patient extends Controller {
}
// OK
// OK - Done
public function create() {
try {
$input = $this->request->getJSON(true);
$data = [
"PatientID" => $input['PatientID'] ?? null,
"AlternatePID" => $input['AlternatePID'] ?? null,
"Prefix" => $input['Prefix'] ?? null,
"NameFirst" => $input['NameFirst'] ?? null,
"NameMiddle" => $input['NameMiddle'] ?? null,
"NameLast" => $input['NameLast'] ?? null,
"NameMaiden" => $input['NameMaiden'] ?? null,
"Suffix" => $input['Suffix'] ?? null,
"NameAlias" => $input['NameAlias'] ?? null,
"Gender" => $input['Gender'] ?? null,
"PlaceOfBirth" => $input['PlaceOfBirth'] ?? null,
"BirthDate" => $input['BirthDate'] ?? null,
"Street_1" => $input['Street_1'] ?? null,
"Street_2" => $input['Street_2'] ?? null,
"Street_3" => $input['Street_3'] ?? null,
"City" => $input['City'] ?? null,
"Province" => $input['Province'] ?? null,
"CountryID" => $input['CountryID'] ?? null,
"ZIP" => $input['ZIP'] ?? null,
"EmailAddress1" => $input['EmailAddress1'] ?? null,
"EmailAddress2" => $input['EmailAddress2'] ?? null,
"Phone" => $input['Phone'] ?? null,
"Mobilephone" => $input['Mobilephone'] ?? null,
"Mother" => $input['Mother'] ?? null,
"Accountnumber" => $input['Accountnumber'] ?? null,
"RaceID" => $input['RaceID'] ?? null,
"MaritalStatus" => $input['MaritalStatus'] ?? null,
"ReligionID" => $input['ReligionID'] ?? null,
"EthnicID" => $input['EthnicID'] ?? null,
"Citizenship" => $input['Citizenship'] ?? null,
"DeathIndicator" => $input['DeathIndicator'] ?? null,
"DeathDateTime" => $input['DeathDateTime'] ?? null,
"LinkTo" => $input['LinkTo'] ?? null,
"create_date" => date('Y-m-d H:i:s')
"PatientID" => $input['patientID'] ?? null,
"AlternatePID" => $input['alternateID'] ?? null,
"Prefix" => $input['title'] ?? null,
"NameFirst" => $input['firstName'] ?? null,
"NameMiddle" => $input['middleName'] ?? null,
"NameMaiden" => $input['maidenName'] ?? null,
"NameLast" => $input['lastName'] ?? null,
"Suffix" => $input['suffixName'] ?? null,
"NameAlias" => null,
"Gender" => ((int) $input['gender']) ?? null, //int
"PlaceOfBirth" => $input['placeOfBirthdate'] ?? null,
"BirthDate" => $input['birthdate'] ?? null,
"Street_1" => $input['street1'] ?? null,
"Street_2" => $input['street2'] ?? null,
"Street_3" => null,
"City" => $input['city'] ?? null,
"Province" => $input['province'] ?? null,
"ZIP" => null,
"CountryID" => null, // int
"EmailAddress1" => $input['email1'] ?? null,
"EmailAddress2" => $input['email2'] ?? null,
"Phone" => $input['phone'] ?? null,
"Mobilephone" => $input['mobile'] ?? null,
// "Mother" => $input['motherName'] ?? null, //int
"Accountnumber" => null, //int
"RaceID" => ((int) $input['race']) ?? null, //int
"MaritalStatus" => $input['maritalStatus'] ?? null,
"ReligionID" => ((int)$input['religion']) ?? null, //int
"EthnicID" => ((int)$input['ethnic']) ?? null, //int
"Citizenship" => null,
"DeathIndicator" => ((int)$input['death']) ?? null, //int
"DeathDateTime" => $input['deathTime'] ?? null,
"CreateDate" => date('Y-m-d H:i:s')
// "LinkTo" => $input['linkTo'] ?? null,
// "PatientComment" => $input['patientComment'] ?? null,
// "IdentityIDType" => $input['identityIDType'] ?? null,
// "IdentityID" => $input['identityID'] ?? null,
];
$rules = [
'PatientID' => 'required|is_unique[patients.pat_num]|max_length[50]',
'PatientID' => 'required|is_unique[patient.PatientID]|max_length[50]',
'NameFirst' => 'required|min_length[3]|max_length[255]',
'NameMiddle' => 'permit_empty',
'NameMaiden' => 'permit_empty',
'NameLast' => 'permit_empty',
// 'birth_date' => 'permit_empty|valid_date[Y-m-d]|not_in_list[0000-00-00]',
'AlternatePID' => 'permit_empty|max_length[50]',
'Street_1' => 'permit_empty',
'Street_2' => 'permit_empty',
@ -165,7 +169,7 @@ class Patient extends Controller {
], 400);
}
$this->db->table('Patient')->insert($data);
$this->db->table('patient')->insert($data);
$newPatientId = $this->db->insertID();
// Sukses & Insert = 201 - Kirim data patient ID
@ -183,45 +187,49 @@ class Patient extends Controller {
}
// OK
public function update($PatientID = null) {
public function update($InternalPID = null) {
try {
$input = $this->request->getJSON(true);
$data = [
"AlternatePID" => $input['AlternatePID'] ?? null,
"Prefix" => $input['Prefix'] ?? null,
"NameFirst" => $input['NameFirst'] ?? null,
"NameMiddle" => $input['NameMiddle'] ?? null,
"NameLast" => $input['NameLast'] ?? null,
"NameMaiden" => $input['NameMaiden'] ?? null,
"Suffix" => $input['Suffix'] ?? null,
"NameAlias" => $input['NameAlias'] ?? null,
"Gender" => $input['Gender'] ?? null,
"PlaceOfBirth" => $input['PlaceOfBirth'] ?? null,
"BirthDate" => $input['BirthDate'] ?? null,
"Street_1" => $input['Street_1'] ?? null,
"Street_2" => $input['Street_2'] ?? null,
"Street_3" => $input['Street_3'] ?? null,
"City" => $input['City'] ?? null,
"Province" => $input['Province'] ?? null,
"CountryID" => $input['CountryID'] ?? null,
"ZIP" => $input['ZIP'] ?? null,
"EmailAddress1" => $input['EmailAddress1'] ?? null,
"EmailAddress2" => $input['EmailAddress2'] ?? null,
"Phone" => $input['Phone'] ?? null,
"Mobilephone" => $input['Mobilephone'] ?? null,
"Mother" => $input['Mother'] ?? null,
"Accountnumber" => $input['Accountnumber'] ?? null,
"RaceID" => $input['RaceID'] ?? null,
"MaritalStatus" => $input['MaritalStatus'] ?? null,
"ReligionID" => $input['ReligionID'] ?? null,
"EthnicID" => $input['EthnicID'] ?? null,
"Citizenship" => $input['Citizenship'] ?? null,
"DeathIndicator" => $input['DeathIndicator'] ?? null,
"DeathDateTime" => $input['DeathDateTime'] ?? null,
"LinkTo" => $input['LinkTo'] ?? null,
"create_date" => date('Y-m-d H:i:s')
"PatientID" => $input['patientID'] ?? null,
"AlternatePID" => $input['alternateID'] ?? null,
"Prefix" => $input['title'] ?? null,
"NameFirst" => $input['firstName'] ?? null,
"NameMiddle" => $input['middleName'] ?? null,
"NameMaiden" => $input['maidenName'] ?? null,
"NameLast" => $input['lastName'] ?? null,
"Suffix" => $input['suffixName'] ?? null,
"NameAlias" => null,
"Gender" => ((int) $input['gender']) ?? null, //int
"PlaceOfBirth" => $input['placeOfBirthdate'] ?? null,
"BirthDate" => $input['birthdate'] ?? null,
"Street_1" => $input['street1'] ?? null,
"Street_2" => $input['street2'] ?? null,
"Street_3" => null,
"City" => $input['city'] ?? null,
"Province" => $input['province'] ?? null,
"ZIP" => null,
"CountryID" => null, // int
"EmailAddress1" => $input['email1'] ?? null,
"EmailAddress2" => $input['email2'] ?? null,
"Phone" => $input['phone'] ?? null,
"Mobilephone" => $input['mobile'] ?? null,
// "Mother" => $input['motherName'] ?? null, //int
"Accountnumber" => null, //int
"RaceID" => ((int) $input['race']) ?? null, //int
"MaritalStatus" => $input['maritalStatus'] ?? null,
"ReligionID" => ((int)$input['religion']) ?? null, //int
"EthnicID" => ((int)$input['ethnic']) ?? null, //int
"Citizenship" => null,
"DeathIndicator" => ((int)$input['death']) ?? null, //int
"DeathDateTime" => $input['deathTime'] ?? null,
"CreateDate" => date('Y-m-d H:i:s')
// "LinkTo" => $input['linkTo'] ?? null,
// "PatientComment" => $input['patientComment'] ?? null,
// "IdentityIDType" => $input['identityIDType'] ?? null,
// "IdentityID" => $input['identityID'] ?? null,
];
$rules = [
@ -237,11 +245,11 @@ class Patient extends Controller {
'City' => 'permit_empty',
];
$existingPatient = $this->db->table('Patient')->where('PatientID', $PatientID)->get()->getRowArray();
$existingPatient = $this->db->table('Patient')->where('InternalPID', $InternalPID)->get()->getRowArray();
// Mengembalikan 404
if (empty($existingPatient)) {
return $this->failNotFound('Patient with ID ' . $PatientID . ' not found.');
return $this->failNotFound('Patient with ID ' . $InternalPID . ' not found.');
}
// Request dari client tidak valid atau tidak bisa diproses oleh server - 400
@ -267,7 +275,7 @@ class Patient extends Controller {
return $this->failValidationError('No data provided for update.');
}
$this->db->table('Patient')->where('PatientID', $PatientID)->update($data);
$this->db->table('Patient')->where('PatientID', $InternalPID)->update($data);
// Sukses & Insert = 201 - Kirim data patient ID
return $this->respondCreated([
@ -283,28 +291,28 @@ class Patient extends Controller {
}
// OK
public function delete($PatientID = null) {
public function delete($InternalPID = null) {
try {
if (!$PatientID) {
if (!$InternalPID) {
return $this->failValidationError('Patient ID is required.');
}
// Cari data pasien
$patient = $this->db->table('Patient')->where('PatientID', $PatientID)->get()->getRow();
$patient = $this->db->table('Patient')->where('InternalPID', $InternalPID)->get()->getRow();
if (!$patient) {
return $this->failNotFound("Patient ID with {$PatientID} not found.");
return $this->failNotFound("Patient ID with {$InternalPID} not found.");
}
// Hapus data pasien berdasarkan pat_num
$this->db->table('Patient')->where('PatientID', $PatientID)->delete();
$this->db->table('Patient')->where('InternalPID', $InternalPID)->delete();
// Mengembalikan 200
return $this->respondDeleted([
'status' => 'success',
'message' => "Patient ID with {$PatientID} deleted successfully."
'message' => "Patient ID with {$InternalPID} deleted successfully."
]);
} catch (\Exception $e) {