158 lines
5.1 KiB
PHP
158 lines
5.1 KiB
PHP
<?php
|
|
namespace App\Controllers;
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
use CodeIgniter\Controller;
|
|
use CodeIgniter\Database\RawSql;
|
|
|
|
class Counter extends Controller {
|
|
use ResponseTrait;
|
|
|
|
protected $db;
|
|
protected $model;
|
|
public function __construct() {
|
|
$this->db = \Config\Database::connect();
|
|
}
|
|
|
|
public function index() {
|
|
$rows = $this->db->table('counter')->select("*")->get()->getResultArray();
|
|
|
|
if (empty($rows)) {
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => "No Data.",
|
|
'data' => [],
|
|
], 200);
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message'=> "Data fetched successfully",
|
|
'data' => $rows,
|
|
], 200);
|
|
}
|
|
|
|
public function show($CounterID = null) {
|
|
$rows = $this->db->table('counter')->select("*")->where('CounterID', (int) $CounterID)->get()->getResultArray();
|
|
|
|
if (empty($rows)) {
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => "Data not found.",
|
|
'data' => [],
|
|
], 200);
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message'=> "Data fetched successfully",
|
|
'data' => $rows,
|
|
], 200);
|
|
}
|
|
|
|
public function create() {
|
|
$input = $this->request->getJSON(true);
|
|
$dataCounter = $this->prepareCounterData($input);
|
|
|
|
try {
|
|
$this->db->transStart();
|
|
$this->db->table('counter')->insert($dataCounter);
|
|
$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' => $dataCounter,
|
|
], 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() {
|
|
try {
|
|
$input = $this->request->getJSON(true);
|
|
$dataCounter = $this->prepareCounterData($input);
|
|
|
|
$this->db->transStart();
|
|
$this->db->table('counter')->where('CounterID', $dataCounter["CounterID"])->update($dataCounter);
|
|
$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' => $dataCounter,
|
|
], 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() {
|
|
try {
|
|
$input = $this->request->getJSON(true);
|
|
$CounterID = $input["CounterID"];
|
|
if (!$CounterID) {
|
|
return $this->failValidationErrors('CounterID is required.');
|
|
}
|
|
|
|
|
|
$location = $this->db->table('counter')->where('CounterID', $CounterID)->get()->getRow();
|
|
if (!$location) {
|
|
return $this->failNotFound("Data not found.");
|
|
}
|
|
|
|
$this->db->table('counter')->where('CounterID', $CounterID)->update(['EndDate' => NOW()]);
|
|
|
|
return $this->respondDeleted([
|
|
'status' => 'success',
|
|
'message' => "Counter 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 prepareCounterData(array $input): array {
|
|
$data = [
|
|
"CounterValue" => $input['CounterValue'] ?? null,
|
|
"CounterStart" => $input['CounterStart'] ?? null,
|
|
"CounterEnd" => $input['CounterEnd'] ?? null,
|
|
"CounterDesc" => $input['CounterDesc'] ?? null,
|
|
"CounterReset" => $input['CounterReset'] ?? null,
|
|
];
|
|
|
|
if(!empty($input["CounterID"])) { $data["CounterID"] = $input["CounterID"]; }
|
|
|
|
return $data;
|
|
}
|
|
} |