2025-09-11 16:40:36 +07:00
|
|
|
<?php
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
|
use CodeIgniter\Controller;
|
|
|
|
|
use CodeIgniter\Database\RawSql;
|
|
|
|
|
|
2025-09-17 15:50:55 +07:00
|
|
|
class ValueSetDef extends Controller {
|
2025-09-11 16:40:36 +07:00
|
|
|
use ResponseTrait;
|
|
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
$this->db = \Config\Database::connect();
|
2025-09-17 15:50:55 +07:00
|
|
|
$this->rulesvaluesetdef = [
|
2025-09-15 15:45:44 +07:00
|
|
|
'VSName' => 'required',
|
|
|
|
|
'VSDesc' => 'required'
|
2025-09-11 16:40:36 +07:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function index() {
|
2025-09-18 13:04:36 +07:00
|
|
|
$builder = $this->db->table('valuesetdef')->select("*");
|
|
|
|
|
$param = $this->request->getVar('param');
|
|
|
|
|
if ($param !== null) {
|
|
|
|
|
$builder->like('VSName', $param, 'both');
|
|
|
|
|
$builder->orlike('VSDesc', $param, 'both');
|
|
|
|
|
}
|
|
|
|
|
$rows = $builder->get()->getResultArray();
|
|
|
|
|
|
2025-09-11 16:40:36 +07:00
|
|
|
if (empty($rows)) {
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => "no Data.",
|
|
|
|
|
'data' => [],
|
|
|
|
|
], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
2025-09-15 15:45:44 +07:00
|
|
|
'message'=> "Data fetched successfully",
|
2025-09-11 16:40:36 +07:00
|
|
|
'data' => $rows,
|
|
|
|
|
], 200);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 15:50:55 +07:00
|
|
|
public function show($VSetID = null) {
|
|
|
|
|
$rows = $this->db->table('valuesetdef')
|
2025-09-11 16:40:36 +07:00
|
|
|
->select("*")
|
2025-09-17 15:50:55 +07:00
|
|
|
->where('VSetID', (int) $VSetID)
|
2025-09-11 16:40:36 +07:00
|
|
|
->get()->getResultArray();
|
|
|
|
|
|
|
|
|
|
if (empty($rows)) {
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
2025-09-17 15:50:55 +07:00
|
|
|
'message' => "data with ID $VSetID not found.",
|
2025-09-11 16:40:36 +07:00
|
|
|
'data' => [],
|
|
|
|
|
], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
2025-09-15 15:45:44 +07:00
|
|
|
'message'=> "Data fetched successfully",
|
2025-09-11 16:40:36 +07:00
|
|
|
'data' => $rows,
|
|
|
|
|
], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function create() {
|
|
|
|
|
try {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
2025-09-17 15:50:55 +07:00
|
|
|
$datavaluesetdef = $this->preparevaluesetdefData($input);
|
2025-09-11 16:40:36 +07:00
|
|
|
|
2025-09-17 15:50:55 +07:00
|
|
|
if (!$this->validateData($datavaluesetdef, $this->rulesvaluesetdef)) {
|
2025-09-11 16:40:36 +07:00
|
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->db->transStart();
|
2025-09-17 15:50:55 +07:00
|
|
|
$this->db->table('valuesetdef')->insert($datavaluesetdef);
|
2025-09-11 16:40:36 +07:00
|
|
|
$this->db->transComplete();
|
|
|
|
|
|
|
|
|
|
if ($this->db->transStatus() === false) {
|
|
|
|
|
$dbError = $this->db->error();
|
|
|
|
|
return $this->failServerError(
|
|
|
|
|
'Failed to create data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown database error')
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->respondCreated([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => 'Data created successfully',
|
2025-09-17 15:50:55 +07:00
|
|
|
'data' => $datavaluesetdef,
|
2025-09-11 16:40:36 +07:00
|
|
|
], 201);
|
|
|
|
|
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
// Ensure rollback if something goes wrong
|
|
|
|
|
if ($this->db->transStatus() !== false) {
|
|
|
|
|
$this->db->transRollback();
|
|
|
|
|
}
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-16 15:33:22 +07:00
|
|
|
public function update() {
|
2025-09-11 16:40:36 +07:00
|
|
|
try {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
2025-09-17 15:50:55 +07:00
|
|
|
$VSetID = $input["VSetID"];
|
|
|
|
|
if (!$VSetID) {
|
|
|
|
|
return $this->failValidationErrors('VSetID is required.');
|
2025-09-16 15:33:22 +07:00
|
|
|
}
|
2025-09-17 15:50:55 +07:00
|
|
|
$datavaluesetdef = $this->preparevaluesetdefData($input);
|
2025-09-11 16:40:36 +07:00
|
|
|
|
2025-09-17 15:50:55 +07:00
|
|
|
if (!$this->validateData($datavaluesetdef, $this->rulesvaluesetdef)) {
|
2025-09-11 16:40:36 +07:00
|
|
|
return $this->failValidationErrors( $this->validator->getErrors());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->db->transStart();
|
2025-09-17 15:50:55 +07:00
|
|
|
$this->db->table('valuesetdef')->where('VSetID', $VSetID)->update($datavaluesetdef);
|
2025-09-11 16:40:36 +07:00
|
|
|
$this->db->transComplete();
|
|
|
|
|
|
|
|
|
|
if ($this->db->transStatus() === false) {
|
|
|
|
|
$dbError = $this->db->error();
|
|
|
|
|
return $this->failServerError(
|
|
|
|
|
'Failed to update data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown database error')
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->respondCreated([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => 'Data updated successfully',
|
2025-09-17 15:50:55 +07:00
|
|
|
'data' => $datavaluesetdef,
|
2025-09-11 16:40:36 +07:00
|
|
|
], 201);
|
|
|
|
|
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
// Ensure rollback if something goes wrong
|
|
|
|
|
if ($this->db->transStatus() !== false) {
|
|
|
|
|
$this->db->transRollback();
|
|
|
|
|
}
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-16 15:33:22 +07:00
|
|
|
public function delete() {
|
|
|
|
|
try {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
2025-09-17 15:50:55 +07:00
|
|
|
$VSetID = $input["VSetID"];
|
|
|
|
|
if (!$VSetID) {
|
|
|
|
|
return $this->failValidationErrors('VSetID is required.');
|
2025-09-11 16:40:36 +07:00
|
|
|
}
|
|
|
|
|
|
2025-09-17 15:50:55 +07:00
|
|
|
$valuesetdef = $this->db->table('valuesetdef')->where('VSetID', $VSetID)->get()->getRow();
|
|
|
|
|
if (!$valuesetdef) {
|
|
|
|
|
return $this->failNotFound("Data with {$VSetID} not found.");
|
2025-09-11 16:40:36 +07:00
|
|
|
}
|
|
|
|
|
|
2025-09-17 15:50:55 +07:00
|
|
|
$this->db->table('valuesetdef')->where('VSetID', $VSetID)->update(['EndDate' => $this->now]);
|
2025-09-11 16:40:36 +07:00
|
|
|
|
|
|
|
|
return $this->respondDeleted([
|
|
|
|
|
'status' => 'success',
|
2025-09-17 15:50:55 +07:00
|
|
|
'message' => "data with {$VSetID} deleted successfully."
|
2025-09-11 16:40:36 +07:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
// Ensure rollback if something goes wrong
|
|
|
|
|
if ($this->db->transStatus() !== false) {
|
|
|
|
|
$this->db->transRollback();
|
|
|
|
|
}
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 15:50:55 +07:00
|
|
|
private function preparevaluesetdefData(array $input): array {
|
2025-09-11 16:40:36 +07:00
|
|
|
$data = [
|
2025-09-15 15:45:44 +07:00
|
|
|
"VSName" => $input['VSName'] ?? null,
|
|
|
|
|
"VSDesc" => $input['VSDesc'] ?? null
|
2025-09-11 16:40:36 +07:00
|
|
|
];
|
|
|
|
|
|
2025-09-17 15:50:55 +07:00
|
|
|
if(!empty($input["VSetID"])) { $data["VSetID"]=$input["VSetID"]; }
|
2025-09-16 15:36:51 +07:00
|
|
|
|
2025-09-11 16:40:36 +07:00
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|