187 lines
5.8 KiB
PHP
187 lines
5.8 KiB
PHP
<?php
|
|
namespace App\Controllers;
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
use CodeIgniter\Controller;
|
|
use CodeIgniter\Database\RawSql;
|
|
|
|
class CodedTextField extends Controller {
|
|
use ResponseTrait;
|
|
|
|
public function __construct() {
|
|
$this->db = \Config\Database::connect();
|
|
$this->now = date('Y-m-d H:i:s');
|
|
$this->rules = [
|
|
'TblName' => 'required',
|
|
'FldName' => 'required',
|
|
'CTGroup' => 'required'
|
|
];
|
|
}
|
|
|
|
public function index() {
|
|
$rows = $this->db->table('codedtxtfld')
|
|
->select("*")
|
|
->get()->getResultArray();
|
|
|
|
if (empty($rows)) {
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => "no Data.",
|
|
'data' => [],
|
|
], 200);
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message'=> "CTF fetched successfully",
|
|
'data' => $rows,
|
|
], 200);
|
|
}
|
|
|
|
public function show($CTFldID = null) {
|
|
$rows = $this->db->table('codedtxtfld')
|
|
->select("*")
|
|
->where('CTFldID', (int) $CTFldID)
|
|
->get()->getResultArray();
|
|
|
|
if (empty($rows)) {
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => "CTFld with ID $CTFldID 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');
|
|
|
|
$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('codedtxtfld')->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($CTFldID = 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('codedtxtfld')->where('CTFldID', $CTFldID)->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($CTFldID = null) {
|
|
try {
|
|
if (!$CTFldID) {
|
|
return $this->failValidationErrors('CTFldID is required.');
|
|
}
|
|
|
|
$codedtxtfld = $this->db->table('codedtxtfld')->where('CTFldID', $CTFldID)->get()->getRow();
|
|
if (!$codedtxtfld) {
|
|
return $this->failNotFound("CTFld with {$CTFldID} not found.");
|
|
}
|
|
|
|
$this->db->table('codedtxtfld')->where('CTFldID', $CTFldID)->update(['EndDate' => $this->now]);
|
|
|
|
return $this->respondDeleted([
|
|
'status' => 'success',
|
|
'message' => "CodedTxtFld with {$CTFldID} 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,
|
|
"FldName" => $input['FldName'] ?? null,
|
|
"TblName" => $input['TblName'] ?? null
|
|
];
|
|
|
|
if ($mode === 'create') {
|
|
$data["CreateDate"] = $now;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
} |