clqms-be/app/Controllers/Specimen/SpecimenCollectionController.php

105 lines
4.3 KiB
PHP
Raw Normal View History

2025-11-10 16:02:52 +07:00
<?php
namespace App\Controllers\Specimen;
use CodeIgniter\API\ResponseTrait;
use App\Controllers\BaseController;
use App\Libraries\ValueSet;
2025-11-10 16:02:52 +07:00
use App\Models\Specimen\SpecimenCollectionModel;
class SpecimenCollectionController extends BaseController {
2025-11-10 16:02:52 +07:00
use ResponseTrait;
protected $db;
protected $model;
protected $rules;
public function __construct() {
$this->db = \Config\Database::connect();
$this->model = new SpecimenCollectionModel();
$this->rules = [];
}
public function index() {
try {
$rows = $this->model->findAll();
// Transform CollectionMethod, Additive, SpecimenRole
foreach ($rows as &$row) {
if (isset($row['CollectionMethod'])) {
$row['collectionMethod'] = $row['CollectionMethod'];
$row['collectionMethodLabel'] = ValueSet::getLabel('collection_method', $row['CollectionMethod']) ?? '';
unset($row['CollectionMethod']);
}
if (isset($row['Additive'])) {
$row['additive'] = $row['Additive'];
$row['additiveLabel'] = ValueSet::getLabel('additive', $row['Additive']) ?? '';
unset($row['Additive']);
}
if (isset($row['SpecimenRole'])) {
$row['specimenRole'] = $row['SpecimenRole'];
$row['specimenRoleLabel'] = ValueSet::getLabel('specimen_role', $row['SpecimenRole']) ?? '';
unset($row['SpecimenRole']);
}
}
2025-11-10 16:02:52 +07:00
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('SpcColID', $id)->first();
if (empty($row)) {
return $this->respond([ 'status' => 'success', 'message'=> "data not found", 'data' => null ], 200);
}
// Transform CollectionMethod, Additive, SpecimenRole
if (isset($row['CollectionMethod'])) {
$row['collectionMethod'] = $row['CollectionMethod'];
$row['collectionMethodLabel'] = ValueSet::getLabel('collection_method', $row['CollectionMethod']) ?? '';
unset($row['CollectionMethod']);
}
if (isset($row['Additive'])) {
$row['additive'] = $row['Additive'];
$row['additiveLabel'] = ValueSet::getLabel('additive', $row['Additive']) ?? '';
unset($row['Additive']);
}
if (isset($row['SpecimenRole'])) {
$row['specimenRole'] = $row['SpecimenRole'];
$row['specimenRoleLabel'] = ValueSet::getLabel('specimen_role', $row['SpecimenRole']) ?? '';
unset($row['SpecimenRole']);
}
return $this->respond([ 'status' => 'success', 'message'=> "data fetched successfully", 'data' => $row ], 200);
2025-11-10 16:02:52 +07:00
} 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['SpcColID'], $input);
return $this->respondCreated([ 'status' => 'success', 'message' => "data $id updated successfully" ]);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
}