clqms-be/app/Controllers/Contact/ContactController.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

106 lines
4.1 KiB
PHP

<?php
namespace App\Controllers\Contact;
use CodeIgniter\API\ResponseTrait;
use App\Controllers\BaseController;
use App\Libraries\ValueSet;
use App\Models\Contact\ContactModel;
class ContactController extends BaseController {
use ResponseTrait;
protected $db;
protected $model;
protected $rules;
public function __construct() {
$this->db = \Config\Database::connect();
$this->model = new ContactModel();
$this->rules = [ 'NameFirst' => 'required' ];
}
public function index() {
$ContactName = $this->request->getVar('ContactName');
$Specialty = $this->request->getVar('Specialty');
$rows = $this->model->getContacts($ContactName, $Specialty);
if (empty($rows)) {
return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200);
}
// Transform Specialty and Occupation
foreach ($rows as &$row) {
if (isset($row['Specialty'])) {
$row['specialty'] = $row['Specialty'];
$row['specialtyLabel'] = ValueSet::getLabel('specialty', $row['Specialty']) ?? '';
unset($row['Specialty']);
}
if (isset($row['Occupation'])) {
$row['occupation'] = $row['Occupation'];
$row['occupationLabel'] = ValueSet::getLabel('occupation', $row['Occupation']) ?? '';
unset($row['Occupation']);
}
}
return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $rows ], 200);
}
public function show($ContactID = null) {
$model = new ContactModel();
$row = $model->getContactWithDetail($ContactID);
if (empty($row)) {
return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => null ], 200);
}
// Transform Specialty and Occupation
if (isset($row['Specialty'])) {
$row['specialty'] = $row['Specialty'];
$row['specialtyLabel'] = ValueSet::getLabel('specialty', $row['Specialty']) ?? '';
unset($row['Specialty']);
}
if (isset($row['Occupation'])) {
$row['occupation'] = $row['Occupation'];
$row['occupationLabel'] = ValueSet::getLabel('occupation', $row['Occupation']) ?? '';
unset($row['Occupation']);
}
return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $row ], 200);
}
public function delete() {
try {
$input = $this->request->getJSON(true);
$ContactID = $input["ContactID"];
if (!$ContactID) { return $this->failValidationErrors('ContactID is required.'); }
$this->model->delete($ContactID);
return $this->respondDeleted([ 'status' => 'success', 'message' => "Contact with {$ContactID} deleted successfully."]);
} catch (\Throwable $e) {
return $this->failServerError('Something went wrong: ' . $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->saveContact($input,true);
return $this->respondCreated([ 'status' => 'success', 'message' => 'data created successfully', 'data' => $id ], 201);
} catch (\Throwable $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 {
$this->model->saveContact($input);
$id = $input['ContactID'];
return $this->respondCreated([ 'status' => 'success', 'message' => 'data updated successfully', 'data' => $id ], 201);
} catch (\Throwable $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
}