clqms-be/app/Controllers/CodedText.php
2025-09-12 09:33:00 +07:00

188 lines
5.8 KiB
PHP

<?php
namespace App\Controllers;
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\Controller;
use CodeIgniter\Database\RawSql;
class CodedText extends Controller {
use ResponseTrait;
public function __construct() {
$this->db = \Config\Database::connect();
$this->now = date('Y-m-d H:i:s');
$this->rules = [
'CTGroup' => 'required',
'CT' => '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'=> "Code Text fetched successfully",
'data' => $rows,
], 200);
}
public function show($CTID = null) {
$rows = $this->db->table('codedtxt')
->select("*")
->where('CTID', (int) $CTID)
->get()->getResultArray();
if (empty($rows)) {
return $this->respond([
'status' => 'success',
'message' => "CodeText with ID $CTID not found.",
'data' => [],
], 200);
}
return $this->respond([
'status' => 'success',
'message'=> "CodeText 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('codedtxt')->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($CTID = 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('codedtxt')->where('CTID', $CTID)->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($CTID = null) {
try {
if (!$CTID) {
return $this->failValidationErrors('CTID is required.');
}
$codedtxt = $this->db->table('codedtxt')->where('CTID', $CTID)->get()->getRow();
if (!$codedtxt) {
return $this->failNotFound("CTID with {$CTID} not found.");
}
$this->db->table('codedtxt')->where('CTID', $CTID)->update(['EndDate' => $this->now ]);
return $this->respondDeleted([
'status' => 'success',
'message' => "CodedTxt with ID {$CTID} 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 = [
"CTGroup" => $input['CTGroup'] ?? null,
"CT" => $input['CT'] ?? null,
"Description" => $input['Description'] ?? null,
"FontStyle" => $input['FontStyle'] ?? null,
"Category" => $input['Category'] ?? null
];
if ($mode === 'create') {
$data["CreateDate"] = $now;
}
return $data;
}
}