2026-03-16 07:24:50 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers\Specimen;
|
|
|
|
|
|
|
|
|
|
use App\Traits\ResponseTrait;
|
|
|
|
|
use App\Controllers\BaseController;
|
|
|
|
|
use App\Libraries\ValueSet;
|
|
|
|
|
use App\Models\Specimen\ContainerDefModel;
|
|
|
|
|
|
2026-04-06 15:38:30 +07:00
|
|
|
class ContainerDefController extends BaseController {
|
2026-03-16 07:24:50 +07:00
|
|
|
use ResponseTrait;
|
|
|
|
|
|
2026-04-06 15:38:30 +07:00
|
|
|
protected $db;
|
|
|
|
|
protected $model;
|
|
|
|
|
protected $rules;
|
|
|
|
|
protected $patchRules;
|
2026-03-16 07:24:50 +07:00
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
$this->db = \Config\Database::connect();
|
2026-04-06 15:38:30 +07:00
|
|
|
$this->model = new ContainerDefModel();
|
|
|
|
|
$this->rules = [
|
|
|
|
|
'ConCode' => 'required|max_length[50]',
|
|
|
|
|
'ConName' => 'required|max_length[50]'
|
|
|
|
|
];
|
|
|
|
|
$this->patchRules = [
|
|
|
|
|
'ConCode' => 'permit_empty|max_length[50]',
|
|
|
|
|
'ConName' => 'permit_empty|max_length[50]'
|
|
|
|
|
];
|
|
|
|
|
}
|
2026-03-16 07:24:50 +07:00
|
|
|
|
|
|
|
|
public function index() {
|
|
|
|
|
try {
|
|
|
|
|
$filter = [
|
|
|
|
|
'ConCode' => $this->request->getVar('ConCode'),
|
|
|
|
|
'ConName' => $this->request->getVar('ConName')
|
|
|
|
|
];
|
|
|
|
|
$rows = $this->model->getContainers($filter);
|
|
|
|
|
|
|
|
|
|
$rows = ValueSet::transformLabels($rows, [
|
|
|
|
|
'ConCategory' => 'container_class',
|
|
|
|
|
'CapColor' => 'container_cap_color',
|
|
|
|
|
'ConSize' => 'container_size',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "data fetched successfully", 'data' => $rows ], 200);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->failServerError('Exception : '.$e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function show($ConDefID) {
|
|
|
|
|
try {
|
|
|
|
|
$row = $this->model->getContainer($ConDefID);
|
|
|
|
|
if (empty($row)) {
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "data not found", 'data' => null ], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$row = ValueSet::transformLabels([$row], [
|
|
|
|
|
'ConCategory' => 'container_class',
|
|
|
|
|
'CapColor' => 'container_cap_color',
|
|
|
|
|
'ConSize' => 'container_size',
|
|
|
|
|
])[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 {
|
|
|
|
|
$ConDefID = $this->model->insert($input);
|
|
|
|
|
return $this->respondCreated([ 'status' => 'success', 'message' => "data $ConDefID created successfully", 'data' => $ConDefID ]);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 15:58:56 +07:00
|
|
|
public function update($ConDefID = null) {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
|
|
|
|
if (!$ConDefID || !ctype_digit((string) $ConDefID)) {
|
|
|
|
|
return $this->failValidationErrors('ConDefID is required.');
|
|
|
|
|
}
|
2026-04-06 15:38:30 +07:00
|
|
|
|
|
|
|
|
if (empty($input) || !is_array($input)) {
|
|
|
|
|
return $this->failValidationErrors('No data provided for update.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$validationInput = array_intersect_key($input, $this->patchRules);
|
|
|
|
|
if (!empty($validationInput) && !$this->validateData($validationInput, $this->patchRules)) {
|
|
|
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 15:58:56 +07:00
|
|
|
$input['ConDefID'] = (int) $ConDefID;
|
|
|
|
|
try {
|
|
|
|
|
$ConDefID = $this->model->update($input['ConDefID'], $input);
|
2026-04-06 15:38:30 +07:00
|
|
|
return $this->respondCreated([ 'status' => 'success', 'message' => "data $ConDefID updated successfully" ]);
|
2026-03-16 07:24:50 +07:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|