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-15 15:45:44 +07:00
|
|
|
class ValueSet extends Controller {
|
2025-09-11 16:40:36 +07:00
|
|
|
use ResponseTrait;
|
|
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
$this->db = \Config\Database::connect();
|
2025-09-16 15:33:22 +07:00
|
|
|
$this->rulesValueSet = [
|
2025-09-15 15:45:44 +07:00
|
|
|
'VSet' => 'required',
|
|
|
|
|
'VValue' => 'required',
|
2025-09-11 16:40:36 +07:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function index() {
|
2025-09-16 10:10:19 +07:00
|
|
|
$rows = $this->db->table('valueset')
|
2025-09-11 16:40:36 +07:00
|
|
|
->select("*")
|
|
|
|
|
->get()->getResultArray();
|
|
|
|
|
|
|
|
|
|
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'=> "Value Set fetched successfully",
|
2025-09-11 16:40:36 +07:00
|
|
|
'data' => $rows,
|
|
|
|
|
], 200);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-15 15:45:44 +07:00
|
|
|
public function show($VID = null) {
|
|
|
|
|
$rows = $this->db->table('valueset')
|
2025-09-11 16:40:36 +07:00
|
|
|
->select("*")
|
2025-09-15 15:45:44 +07:00
|
|
|
->where('VID', (int) $VID)
|
2025-09-11 16:40:36 +07:00
|
|
|
->get()->getResultArray();
|
|
|
|
|
|
|
|
|
|
if (empty($rows)) {
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
2025-09-15 15:45:44 +07:00
|
|
|
'message' => "ValueSet with ID $VID 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-16 15:33:22 +07:00
|
|
|
$dataValueSet = $this->prepareDataValueSet($input);
|
2025-09-11 16:40:36 +07:00
|
|
|
|
2025-09-16 15:33:22 +07:00
|
|
|
if (!$this->validateData($data, $this->rulesValueSet)) {
|
2025-09-11 16:40:36 +07:00
|
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start transaction
|
|
|
|
|
$this->db->transStart();
|
|
|
|
|
|
|
|
|
|
// Insert
|
2025-09-15 15:45:44 +07:00
|
|
|
$this->db->table('valueset')->insert($data);
|
2025-09-11 16:40:36 +07:00
|
|
|
|
|
|
|
|
// Complete transaction
|
|
|
|
|
$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-16 15:33:22 +07:00
|
|
|
'data' => $dataValueSet,
|
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-16 15:33:22 +07:00
|
|
|
$VID = $input["VID"];
|
|
|
|
|
if (!$VID) {
|
|
|
|
|
return $this->failValidationErrors('VID is required.');
|
|
|
|
|
}
|
2025-09-11 16:40:36 +07:00
|
|
|
|
2025-09-16 15:33:22 +07:00
|
|
|
$dataValueSet = $this->prepareValueSetData($input);
|
2025-09-11 16:40:36 +07:00
|
|
|
|
2025-09-16 15:33:22 +07:00
|
|
|
if (!$this->validateData($dataValueSet, $this->rulesValueSet)) {
|
2025-09-12 09:33:00 +07:00
|
|
|
return $this->failValidationErrors( $this->validator->getErrors() );
|
2025-09-11 16:40:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start transaction
|
|
|
|
|
$this->db->transStart();
|
|
|
|
|
|
|
|
|
|
// Insert location
|
2025-09-16 15:33:22 +07:00
|
|
|
$this->db->table('valueset')->where('VID', $VID)->update($dataValueSet);
|
2025-09-11 16:40:36 +07:00
|
|
|
|
|
|
|
|
// Complete transaction
|
|
|
|
|
$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-16 15:33:22 +07:00
|
|
|
'data' => $dataValueSet,
|
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);
|
|
|
|
|
$VID = $input["VID"];
|
2025-09-15 15:45:44 +07:00
|
|
|
if (!$VID) {
|
|
|
|
|
return $this->failValidationErrors('VID is required.');
|
2025-09-11 16:40:36 +07:00
|
|
|
}
|
|
|
|
|
|
2025-09-15 15:45:44 +07:00
|
|
|
$valueset = $this->db->table('valuset')->where('VID', $VID)->get()->getRow();
|
|
|
|
|
if (!$valueset) {
|
|
|
|
|
return $this->failNotFound("VID with {$VID} not found.");
|
2025-09-11 16:40:36 +07:00
|
|
|
}
|
|
|
|
|
|
2025-09-16 10:10:19 +07:00
|
|
|
$this->db->table('valueset')->where('VID', $VID)->update(['EndDate' => $this->now ]);
|
2025-09-11 16:40:36 +07:00
|
|
|
|
|
|
|
|
return $this->respondDeleted([
|
|
|
|
|
'status' => 'success',
|
2025-09-15 15:45:44 +07:00
|
|
|
'message' => "Data with ID {$VID} 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-16 15:33:22 +07:00
|
|
|
private function prepareValueSetData(array $input): array {
|
2025-09-11 16:40:36 +07:00
|
|
|
$data = [
|
2025-09-15 15:45:44 +07:00
|
|
|
"VSet" => $input['VSet'] ?? null,
|
|
|
|
|
"VOrder" => $input['VOrder'] ?? null,
|
|
|
|
|
"VValue" => $input['VValue'] ?? null,
|
|
|
|
|
"VDesc" => $input['VDesc'] ?? null,
|
|
|
|
|
"VCategory" => $input['VCategory'] ?? null
|
2025-09-11 16:40:36 +07:00
|
|
|
];
|
|
|
|
|
|
2025-09-16 15:33:22 +07:00
|
|
|
if(!empty($input["VID"])) { $data["VID"]=$input["VID"]; }
|
2025-09-11 16:40:36 +07:00
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
}
|