clqms-be/app/Controllers/Patient.php

157 lines
5.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Controllers;
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\Controller;
use App\Models\PatientModel;
class Patient extends Controller {
use ResponseTrait;
protected $db;
protected $modelPatient;
protected $rulesPatient;
protected $rulesPatIdt;
public function __construct() {
$this->db = \Config\Database::connect();
$this->modelPatient = new PatientModel();
$this->rulesPatient = [
2025-10-01 15:36:55 +07:00
'PatientID' => 'required|max_length[50]',
'AlternatePID' => 'permit_empty|max_length[50]',
'NameFirst' => 'required|min_length[1]|max_length[255]',
2025-10-01 15:36:55 +07:00
'EmailAddress1' => 'required',
'Gender' => 'required'
];
}
public function index() {
$filters = [
'InternalPID' => $this->request->getVar('InternalPID'),
'PatientID' => $this->request->getVar('PatientID'),
'Name' => $this->request->getVar('Name'),
'Birthdate' => $this->request->getVar('Birthdate'),
];
2025-07-23 11:03:46 +07:00
try {
$rows = $this->modelPatient->getPatients($filters);
return $this->respond([ 'status' => 'success', 'message'=> "data fetched successfully", 'data' => $rows ], 200);
2025-07-23 11:03:46 +07:00
} catch (\Exception $e) {
return $this->failServerError('Exception : '.$e->getMessage());
2025-07-23 11:03:46 +07:00
}
}
2025-08-01 22:18:45 +07:00
public function show($InternalPID = null) {
2025-07-23 11:03:46 +07:00
try {
$rows = $this->modelPatient->getPatient($InternalPID);
if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "data not found." ], 200); }
return $this->respond([ 'status' => 'success', 'message' => "data fetched successfully", 'data' => $rows ], 200);
2025-07-23 11:03:46 +07:00
} catch (\Exception $e) {
2025-09-08 15:56:38 +07:00
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
2025-07-23 11:03:46 +07:00
public function create() {
$input = $this->request->getJSON(true);
if (!$this->validateData($input, $this->rulesPatient)) { return $this->validationError('patient', $this->validator->getErrors()); }
2025-07-23 11:03:46 +07:00
try {
2025-10-01 15:36:55 +07:00
$InternalPID = $this->modelPatient->createPatient($input);
return $this->respondCreated([ 'status' => 'success', 'message' => "data $InternalPID created successfully" ]);
2025-07-23 11:03:46 +07:00
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
2025-07-23 11:03:46 +07:00
}
}
private function validationError(string $context, array $errors) {
2025-09-08 15:56:38 +07:00
return $this->respond([
'status' => 'error',
'message' => "Validation failed ({$context})",
'errors' => $errors
], 400);
}
public function update() {
2025-10-01 15:36:55 +07:00
$input = $this->request->getJSON(true);
if (!$this->validateData($input, $this->rulesPatient)) { return $this->validationError('patient', $this->validator->getErrors()); }
2025-07-23 11:03:46 +07:00
try {
2025-10-01 15:36:55 +07:00
$InternalPID = $this->modelPatient->updatePatient($input);
return $this->respondCreated([ 'status' => 'success', 'message' => "data $InternalPID update successfully" ]);
2025-07-23 11:03:46 +07:00
} catch (\Exception $e) {
2025-08-12 09:19:10 +07:00
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
2025-07-23 11:03:46 +07:00
2025-09-25 14:01:33 +07:00
// Unit Testing Pass : \clqms-be\tests\feature\Patients\PatientDeleteTest.php
public function delete() {
2025-07-23 11:03:46 +07:00
try {
$input = $this->request->getJSON(true);
$InternalPID = $input["InternalPID"];
2025-09-25 14:01:33 +07:00
// Mencegah Inputan 0, [], null, sql injection
if (empty($InternalPID) || !ctype_digit((string) $InternalPID)) {
return $this->respond([
'status' => 'error',
'message' => "Patient ID must be a valid integer."
], 400);
2025-07-23 11:03:46 +07:00
}
$patient = $this->db->table('patient')->where('InternalPID', $InternalPID)->get()->getRow();
2025-07-23 11:03:46 +07:00
if (!$patient) {
2025-08-01 22:18:45 +07:00
return $this->failNotFound("Patient ID with {$InternalPID} not found.");
2025-07-23 11:03:46 +07:00
}
2025-08-05 10:03:33 +07:00
$this->db->table('patient')->where('InternalPID', $InternalPID)->update(['DelDate' => date('Y-m-d H:i:s')]);
2025-07-23 11:03:46 +07:00
return $this->respondDeleted([
'status' => 'success',
2025-08-01 22:18:45 +07:00
'message' => "Patient ID with {$InternalPID} deleted successfully."
2025-07-23 11:03:46 +07:00
]);
} catch (\Exception $e) {
return $this->failServerError("Internal server error: " . $e->getMessage());
}
}
public function patientCheck() {
2025-08-14 09:17:15 +07:00
try {
$PatientID = $this->request->getVar('PatientID');
$EmailAddress1 = $this->request->getVar('EmailAddress1');
if ($PatientID!=null){
$tableName = 'PatientID';
$searchName = $PatientID;
}
if ($EmailAddress1!=null){
$tableName = 'EmailAddress1';
$searchName = $EmailAddress1;
2025-08-14 09:17:15 +07:00
}
$patient = $this->db->table('patient')
->where($tableName, $searchName)
2025-08-14 09:17:15 +07:00
->get()
2025-09-25 14:01:33 +07:00
->getRowArray();
2025-08-14 09:17:15 +07:00
if (!$patient) {
return $this->respond([
'status' => 'success',
'message' => "$tableName not found.",
2025-08-14 09:17:15 +07:00
'data' => true,
], 200);
}
return $this->respond([
'status' => 'success',
'message' => "$tableName already exists.",
2025-08-14 09:17:15 +07:00
'data' => false,
], 200);
} catch (\Exception $e) {
// Error Server Mengembalikan 500
return $this->failServerError('Something went wrong.'.$e->getMessage());
2025-08-14 09:17:15 +07:00
}
}
}