clqms-be/app/Controllers/Specimen/SpecimenController.php
mahdahar e5ac1957fe ● refactor: update API responses to use {field}Label format
- Transform coded fields to lowercase with Label suffix for display text                                                                                                                                              - Controllers: OrderTestController, DemoOrderController, SpecimenController,
    SpecimenStatusController, SpecimenCollectionController, ContainerDefController,
    ContactController, TestMapController
  - Example: Priority: "R" → priority: "R", priorityLabel: "Routine"
  - Update api-docs.yaml with new OpenAPI schema definitions
  - Add API docs reminder to CLAUDE.md
2026-01-28 17:31:00 +07:00

105 lines
4.2 KiB
PHP

<?php
namespace App\Controllers\Specimen;
use CodeIgniter\API\ResponseTrait;
use App\Controllers\BaseController;
use App\Libraries\ValueSet;
use App\Models\Specimen\SpecimenModel;
class SpecimenController extends BaseController {
use ResponseTrait;
protected $db;
protected $model;
protected $rules;
public function __construct() {
$this->db = \Config\Database::connect();
$this->model = new SpecimenModel();
$this->rules = [];
}
public function index() {
try {
$rows = $this->model->findAll();
// Transform SpecimenType, SpecimenStatus, BodySite
foreach ($rows as &$row) {
if (isset($row['SpecimenType'])) {
$row['specimenType'] = $row['SpecimenType'];
$row['specimenTypeLabel'] = ValueSet::getLabel('specimen_type', $row['SpecimenType']) ?? '';
unset($row['SpecimenType']);
}
if (isset($row['SpecimenStatus'])) {
$row['specimenStatus'] = $row['SpecimenStatus'];
$row['specimenStatusLabel'] = ValueSet::getLabel('specimen_status', $row['SpecimenStatus']) ?? '';
unset($row['SpecimenStatus']);
}
if (isset($row['BodySite'])) {
$row['bodySite'] = $row['BodySite'];
$row['bodySiteLabel'] = ValueSet::getLabel('body_site', $row['BodySite']) ?? '';
unset($row['BodySite']);
}
}
return $this->respond([ 'status' => 'success', 'message'=> "data fetched successfully", 'data' => $rows ], 200);
} catch (\Exception $e) {
return $this->failServerError('Exception : '.$e->getMessage());
}
}
public function show($id) {
try {
$row = $this->model->where('SID',$id)->first();
if (empty($row)) {
return $this->respond([ 'status' => 'success', 'message'=> "data not found", 'data' => null ], 200);
}
// Transform SpecimenType, SpecimenStatus, BodySite
if (isset($row['SpecimenType'])) {
$row['specimenType'] = $row['SpecimenType'];
$row['specimenTypeLabel'] = ValueSet::getLabel('specimen_type', $row['SpecimenType']) ?? '';
unset($row['SpecimenType']);
}
if (isset($row['SpecimenStatus'])) {
$row['specimenStatus'] = $row['SpecimenStatus'];
$row['specimenStatusLabel'] = ValueSet::getLabel('specimen_status', $row['SpecimenStatus']) ?? '';
unset($row['SpecimenStatus']);
}
if (isset($row['BodySite'])) {
$row['bodySite'] = $row['BodySite'];
$row['bodySiteLabel'] = ValueSet::getLabel('body_site', $row['BodySite']) ?? '';
unset($row['BodySite']);
}
return $this->respond([ 'status' => 'success', 'message'=> "data fetched successfully", 'data' => $row ], 200);
} catch (\Exception $e) {
return $this->failServerError('Exception : '.$e->getMessage());
}
}
public function create() {
$input = $this->request->getJSON(true);
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); }
try {
$id = $this->model->insert($input);
return $this->respondCreated([ 'status' => 'success', 'message' => "data $id created successfully" ]);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
public function update() {
$input = $this->request->getJSON(true);
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); }
try {
$id = $this->model->update($input['SID'], $input);
return $this->respondCreated([ 'status' => 'success', 'message' => "data $id updated successfully" ]);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
}