2025-09-09 09:16:35 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
2026-02-18 10:15:47 +07:00
|
|
|
use App\Traits\ResponseTrait;
|
2025-09-09 09:16:35 +07:00
|
|
|
use CodeIgniter\Controller;
|
2026-03-04 16:48:12 +07:00
|
|
|
use App\Models\PatResultModel;
|
2025-09-09 09:16:35 +07:00
|
|
|
|
2026-01-05 16:55:34 +07:00
|
|
|
class ResultController extends Controller {
|
2025-09-09 09:16:35 +07:00
|
|
|
use ResponseTrait;
|
|
|
|
|
|
2026-03-04 16:48:12 +07:00
|
|
|
protected $model;
|
|
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
$this->model = new PatResultModel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* List results with optional filters
|
|
|
|
|
* GET /api/results
|
|
|
|
|
*/
|
2025-09-09 09:16:35 +07:00
|
|
|
public function index() {
|
2026-03-04 16:48:12 +07:00
|
|
|
try {
|
|
|
|
|
$orderID = $this->request->getGet('order_id');
|
|
|
|
|
$patientID = $this->request->getGet('patient_id');
|
|
|
|
|
|
|
|
|
|
if ($orderID) {
|
|
|
|
|
$results = $this->model->getByOrder((int)$orderID);
|
|
|
|
|
} elseif ($patientID) {
|
|
|
|
|
$results = $this->model->getByPatient((int)$patientID);
|
|
|
|
|
} else {
|
|
|
|
|
// Get all results with pagination
|
|
|
|
|
$page = (int)($this->request->getGet('page') ?? 1);
|
|
|
|
|
$perPage = (int)($this->request->getGet('per_page') ?? 20);
|
|
|
|
|
|
|
|
|
|
$results = $this->model
|
|
|
|
|
->where('DelDate', null)
|
|
|
|
|
->orderBy('ResultID', 'DESC')
|
|
|
|
|
->paginate($perPage, 'default', $page);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => 'Results retrieved successfully',
|
|
|
|
|
'data' => $results
|
|
|
|
|
], 200);
|
|
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
log_message('error', 'ResultController::index error: ' . $e->getMessage());
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'failed',
|
|
|
|
|
'message' => 'Failed to retrieve results',
|
|
|
|
|
'data' => []
|
|
|
|
|
], 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get single result
|
|
|
|
|
* GET /api/results/{id}
|
|
|
|
|
*/
|
|
|
|
|
public function show($id) {
|
|
|
|
|
try {
|
|
|
|
|
$result = $this->model->getWithRelations((int)$id);
|
|
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'failed',
|
|
|
|
|
'message' => 'Result not found',
|
|
|
|
|
'data' => []
|
|
|
|
|
], 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => 'Result retrieved successfully',
|
|
|
|
|
'data' => $result
|
|
|
|
|
], 200);
|
|
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
log_message('error', 'ResultController::show error: ' . $e->getMessage());
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'failed',
|
|
|
|
|
'message' => 'Failed to retrieve result',
|
|
|
|
|
'data' => []
|
|
|
|
|
], 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update result with validation
|
|
|
|
|
* PATCH /api/results/{id}
|
|
|
|
|
*/
|
|
|
|
|
public function update($id) {
|
|
|
|
|
try {
|
|
|
|
|
$data = $this->request->getJSON(true);
|
|
|
|
|
|
|
|
|
|
if (empty($data)) {
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'failed',
|
|
|
|
|
'message' => 'No data provided',
|
|
|
|
|
'data' => []
|
|
|
|
|
], 400);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result = $this->model->updateWithValidation((int)$id, $data);
|
|
|
|
|
|
|
|
|
|
if (!$result['success']) {
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'failed',
|
|
|
|
|
'message' => $result['message'],
|
|
|
|
|
'data' => []
|
|
|
|
|
], 400);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get updated result with relations
|
|
|
|
|
$updatedResult = $this->model->getWithRelations((int)$id);
|
|
|
|
|
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => $result['message'],
|
|
|
|
|
'data' => [
|
|
|
|
|
'result' => $updatedResult,
|
|
|
|
|
'flag' => $result['flag']
|
|
|
|
|
]
|
|
|
|
|
], 200);
|
|
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
log_message('error', 'ResultController::update error: ' . $e->getMessage());
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'failed',
|
|
|
|
|
'message' => 'Failed to update result',
|
|
|
|
|
'data' => []
|
|
|
|
|
], 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Soft delete result
|
|
|
|
|
* DELETE /api/results/{id}
|
|
|
|
|
*/
|
|
|
|
|
public function delete($id) {
|
|
|
|
|
try {
|
|
|
|
|
$result = $this->model->find((int)$id);
|
|
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'failed',
|
|
|
|
|
'message' => 'Result not found',
|
|
|
|
|
'data' => []
|
|
|
|
|
], 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$deleted = $this->model->softDelete((int)$id);
|
2025-09-09 09:16:35 +07:00
|
|
|
|
2026-03-04 16:48:12 +07:00
|
|
|
if (!$deleted) {
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'failed',
|
|
|
|
|
'message' => 'Failed to delete result',
|
|
|
|
|
'data' => []
|
|
|
|
|
], 500);
|
|
|
|
|
}
|
2025-09-09 09:16:35 +07:00
|
|
|
|
2026-03-04 16:48:12 +07:00
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => 'Result deleted successfully',
|
|
|
|
|
'data' => []
|
|
|
|
|
], 200);
|
2025-09-09 09:16:35 +07:00
|
|
|
|
2026-03-04 16:48:12 +07:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
log_message('error', 'ResultController::delete error: ' . $e->getMessage());
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'failed',
|
|
|
|
|
'message' => 'Failed to delete result',
|
|
|
|
|
'data' => []
|
|
|
|
|
], 500);
|
|
|
|
|
}
|
2025-09-09 09:16:35 +07:00
|
|
|
}
|
|
|
|
|
}
|