2026-03-16 07:24:50 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers\Specimen;
|
|
|
|
|
|
2026-04-08 08:37:41 +07:00
|
|
|
use App\Traits\PatchValidationTrait;
|
|
|
|
|
use App\Traits\ResponseTrait;
|
|
|
|
|
use App\Controllers\BaseController;
|
|
|
|
|
use App\Libraries\ValueSet;
|
|
|
|
|
use App\Models\Specimen\SpecimenStatusModel;
|
2026-03-16 07:24:50 +07:00
|
|
|
|
2026-04-08 06:54:50 +07:00
|
|
|
class SpecimenStatusController extends BaseController {
|
2026-04-08 08:37:41 +07:00
|
|
|
use ResponseTrait;
|
|
|
|
|
use PatchValidationTrait;
|
2026-03-16 07:24:50 +07:00
|
|
|
|
|
|
|
|
protected $db;
|
|
|
|
|
protected $model;
|
|
|
|
|
protected $rules;
|
|
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
$this->db = \Config\Database::connect();
|
|
|
|
|
$this->model = new SpecimenStatusModel();
|
|
|
|
|
$this->rules = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function index() {
|
|
|
|
|
try {
|
|
|
|
|
$rows = $this->model->findAll();
|
|
|
|
|
|
|
|
|
|
$rows = ValueSet::transformLabels($rows, [
|
|
|
|
|
'Status' => 'specimen_status',
|
|
|
|
|
'Activity' => 'specimen_activity',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
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('SpcStaID', $id)->first();
|
|
|
|
|
if (empty($row)) {
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "data not found", 'data' => null ], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$row = ValueSet::transformLabels([$row], [
|
|
|
|
|
'Status' => 'specimen_status',
|
|
|
|
|
'Activity' => 'specimen_activity',
|
|
|
|
|
])[0];
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 15:58:56 +07:00
|
|
|
public function update($SpcStaID = null) {
|
2026-04-08 08:37:41 +07:00
|
|
|
$input = $this->requirePatchPayload($this->request->getJSON(true));
|
|
|
|
|
if ($input === null) {
|
|
|
|
|
return;
|
2026-03-16 15:58:56 +07:00
|
|
|
}
|
2026-04-08 08:37:41 +07:00
|
|
|
|
|
|
|
|
$id = $this->requirePatchId($SpcStaID, 'SpcStaID');
|
|
|
|
|
if ($id === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$existing = $this->model->where('SpcStaID', $id)->first();
|
|
|
|
|
if (!$existing) {
|
|
|
|
|
return $this->respond([ 'status' => 'failed', 'message' => 'Specimen status not found', 'data' => [] ], 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$input['SpcStaID'] = $id;
|
2026-04-08 16:07:19 +07:00
|
|
|
if ($this->rules !== [] && !$this->validateData($input, $this->rules)) {
|
|
|
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
|
|
|
}
|
2026-03-16 15:58:56 +07:00
|
|
|
try {
|
2026-04-08 08:37:41 +07:00
|
|
|
$this->model->update($id, $input);
|
2026-04-08 16:07:19 +07:00
|
|
|
return $this->respondCreated([ 'status' => 'success', 'message' => 'data updated successfully', 'data' => $id ], 201);
|
2026-04-08 08:37:41 +07:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-16 07:24:50 +07:00
|
|
|
|
|
|
|
|
}
|