clqms-be/app/Controllers/Test/TestMapController.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

86 lines
3.4 KiB
PHP

<?php
namespace App\Controllers\Test;
use CodeIgniter\API\ResponseTrait;
use App\Controllers\BaseController;
use App\Libraries\ValueSet;
use App\Models\Test\TestMapModel;
class TestMapController extends BaseController {
use ResponseTrait;
protected $db;
protected $rules;
protected $model;
public function __construct() {
$this->db = \Config\Database::connect();
$this->model = new TestMapModel;
}
public function index() {
$rows = $this->model->findAll();
if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200); }
// Transform HostType and ClientType
foreach ($rows as &$row) {
if (isset($row['HostType'])) {
$row['hostType'] = $row['HostType'];
$row['hostTypeLabel'] = ValueSet::getLabel('entity_type', $row['HostType']) ?? '';
unset($row['HostType']);
}
if (isset($row['ClientType'])) {
$row['clientType'] = $row['ClientType'];
$row['clientTypeLabel'] = ValueSet::getLabel('entity_type', $row['ClientType']) ?? '';
unset($row['ClientType']);
}
}
return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200);
}
public function show($id = null) {
$row = $this->model->where('TestMapID',$id)->first();
if (empty($row)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => null ], 200); }
// Transform HostType and ClientType
if (isset($row['HostType'])) {
$row['hostType'] = $row['HostType'];
$row['hostTypeLabel'] = ValueSet::getLabel('entity_type', $row['HostType']) ?? '';
unset($row['HostType']);
}
if (isset($row['ClientType'])) {
$row['clientType'] = $row['ClientType'];
$row['clientTypeLabel'] = ValueSet::getLabel('entity_type', $row['ClientType']) ?? '';
unset($row['ClientType']);
}
return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $row ], 200);
}
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 created successfully", 'data' => $id ]);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
public function update() {
$input = $this->request->getJSON(true);
$id = $input["TestMapID"];
if (!$id) { return $this->failValidationErrors('TestMapID is required.'); }
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors( $this->validator->getErrors() ); }
try {
$this->model->update($id,$input);
return $this->respondCreated([ 'status' => 'success', 'message' => "data updated successfully", 'data' => $id ]);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
}