188 lines
5.8 KiB
PHP
188 lines
5.8 KiB
PHP
<?php
|
|
namespace App\Controllers;
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
use CodeIgniter\Controller;
|
|
use CodeIgniter\Database\RawSql;
|
|
|
|
class ValueSet extends Controller {
|
|
use ResponseTrait;
|
|
|
|
public function __construct() {
|
|
$this->db = \Config\Database::connect();
|
|
$this->now = date('Y-m-d H:i:s');
|
|
$this->rules = [
|
|
'VSet' => 'required',
|
|
'VValue' => 'required',
|
|
];
|
|
}
|
|
|
|
public function index() {
|
|
$rows = $this->db->table('codedtxt')
|
|
->select("*")
|
|
->get()->getResultArray();
|
|
|
|
if (empty($rows)) {
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => "no Data.",
|
|
'data' => [],
|
|
], 200);
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message'=> "Value Set fetched successfully",
|
|
'data' => $rows,
|
|
], 200);
|
|
}
|
|
|
|
public function show($VID = null) {
|
|
$rows = $this->db->table('valueset')
|
|
->select("*")
|
|
->where('VID', (int) $VID)
|
|
->get()->getResultArray();
|
|
|
|
if (empty($rows)) {
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => "ValueSet with ID $VID not found.",
|
|
'data' => [],
|
|
], 200);
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message'=> "Data fetched successfully",
|
|
'data' => $rows,
|
|
], 200);
|
|
}
|
|
|
|
public function create() {
|
|
try {
|
|
$input = $this->request->getJSON(true);
|
|
$now = date('Y-m-d H:i:s');
|
|
|
|
// Prepare data
|
|
$data = $this->prepareData($input, $now, 'create');
|
|
|
|
if (!$this->validateData($data, $this->rules)) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
|
|
// Start transaction
|
|
$this->db->transStart();
|
|
|
|
// Insert
|
|
$this->db->table('valueset')->insert($data);
|
|
|
|
// 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',
|
|
'data' => $data,
|
|
], 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());
|
|
}
|
|
}
|
|
|
|
public function update($VID = null) {
|
|
try {
|
|
$input = $this->request->getJSON(true);
|
|
$now = $this->now;
|
|
|
|
$data = $this->prepareData($input, $now, 'update');
|
|
|
|
if (!$this->validateData($data, $this->rules)) {
|
|
return $this->failValidationErrors( $this->validator->getErrors() );
|
|
}
|
|
|
|
// Start transaction
|
|
$this->db->transStart();
|
|
|
|
// Insert location
|
|
$this->db->table('valueset')->where('VID', $VID)->update($data);
|
|
|
|
// 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',
|
|
'data' => $data,
|
|
], 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());
|
|
}
|
|
}
|
|
|
|
public function delete($VID = null) {
|
|
try {
|
|
if (!$VID) {
|
|
return $this->failValidationErrors('VID is required.');
|
|
}
|
|
|
|
$valueset = $this->db->table('valuset')->where('VID', $VID)->get()->getRow();
|
|
if (!$valueset) {
|
|
return $this->failNotFound("VID with {$VID} not found.");
|
|
}
|
|
|
|
$this->db->table('codedtxt')->where('VID', $VID)->update(['EndDate' => $this->now ]);
|
|
|
|
return $this->respondDeleted([
|
|
'status' => 'success',
|
|
'message' => "Data with ID {$VID} deleted successfully."
|
|
]);
|
|
|
|
} 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());
|
|
}
|
|
}
|
|
|
|
private function prepareData(array $input, string $now, string $mode = 'create'): array {
|
|
$data = [
|
|
"VSet" => $input['VSet'] ?? null,
|
|
"VOrder" => $input['VOrder'] ?? null,
|
|
"VValue" => $input['VValue'] ?? null,
|
|
"VDesc" => $input['VDesc'] ?? null,
|
|
"VCategory" => $input['VCategory'] ?? null
|
|
];
|
|
|
|
if ($mode === 'create') {
|
|
$data["CreateDate"] = $now;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
} |