add counter endpoint and move counter to model
This commit is contained in:
parent
cfd791ddaf
commit
5f317a7e38
@ -75,4 +75,10 @@ $routes->get('/api/valuesetdef/', 'ValueSetDef::index');
|
||||
$routes->get('/api/valuesetdef/(:num)', 'ValueSetDef::show/$1');
|
||||
$routes->post('/api/valuesetdef', 'ValueSetDef::create');
|
||||
$routes->patch('/api/valuesetdef', 'ValueSetDef::update');
|
||||
$routes->delete('/api/valuesetdef', 'ValueSetDef::delete');
|
||||
$routes->delete('/api/valuesetdef', 'ValueSetDef::delete');
|
||||
|
||||
$routes->get('/api/counter/', 'Counter::index');
|
||||
$routes->get('/api/counter/(:num)', 'Counter::show/$1');
|
||||
$routes->post('/api/counter', 'Counter::create');
|
||||
$routes->patch('/api/counter', 'Counter::update');
|
||||
$routes->delete('/api/counter', 'Counter::delete');
|
||||
156
app/Controllers/Counter.php
Normal file
156
app/Controllers/Counter.php
Normal file
@ -0,0 +1,156 @@
|
||||
<?php
|
||||
namespace App\Controllers;
|
||||
|
||||
use CodeIgniter\API\ResponseTrait;
|
||||
use CodeIgniter\Controller;
|
||||
use CodeIgniter\Database\RawSql;
|
||||
|
||||
class Counter extends Controller {
|
||||
use ResponseTrait;
|
||||
|
||||
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($dataCounterID);
|
||||
$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->failValidationError('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;
|
||||
}
|
||||
}
|
||||
@ -5,6 +5,8 @@ use CodeIgniter\API\ResponseTrait;
|
||||
use CodeIgniter\Controller;
|
||||
use CodeIgniter\Database\RawSql;
|
||||
|
||||
use App\Models\CounterModel;
|
||||
|
||||
class PatVisit extends Controller {
|
||||
use ResponseTrait;
|
||||
|
||||
@ -13,21 +15,6 @@ class PatVisit extends Controller {
|
||||
$this->visnum_prefix = "DV";
|
||||
}
|
||||
|
||||
private function preparePVID() {
|
||||
$row = $this->db->table('counter')->select('*')->where('CounterID','2')->get()->getResultArray();
|
||||
$cValue = $row[0]['CounterValue'];
|
||||
$cStart = $row[0]['CounterStart'];
|
||||
$cEnd = $row[0]['CounterEnd'];
|
||||
$cReset = $row[0]['CounterReset'];
|
||||
$cPad = strlen((string)$cEnd);
|
||||
if($cValue > $cEnd) { $cValue = $cStart; }
|
||||
$cnum = $this->visnum_prefix.str_pad($cValue, $cPad, "0", STR_PAD_LEFT);
|
||||
$cValue_next = $cValue+1;
|
||||
// next value > end, back to start
|
||||
$this->db->table('counter')->set('CounterValue', $cValue_next)->where('CounterID','2')->update();
|
||||
return $cnum;
|
||||
}
|
||||
|
||||
public function show($PVID = null) {
|
||||
try {
|
||||
$row = $this->db->table('patvisit pv')
|
||||
@ -105,7 +92,9 @@ class PatVisit extends Controller {
|
||||
if (!$input) { return $this->respond(['status' => 'error', 'message' => 'Invalid JSON input'], 400); }
|
||||
|
||||
if($input['PVID'] =='' || !isset($input['PVID'])) {
|
||||
$input['PVID'] = $this->preparePVID();
|
||||
$model = new CounterModel();
|
||||
$input['PVID'] = $this->visnum_prefix .$model->use(2);
|
||||
//$input['PVID'] = $this->preparePVID();
|
||||
}
|
||||
|
||||
$dataPatVisit = $this->preparePatVisitData($input);
|
||||
|
||||
27
app/Models/CounterModel.php
Normal file
27
app/Models/CounterModel.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
|
||||
class CounterModel extends Model {
|
||||
protected $table = 'counter';
|
||||
protected $primaryKey = 'CounterID';
|
||||
protected $allowedFields = ['CounterValue', 'CounterStart', 'CounterEnd', 'CounterReset'];
|
||||
|
||||
public function use($CounterID) {
|
||||
$row = $this->where('CounterID',$CounterID)->get()->getResultArray();
|
||||
$cValue = $row[0]['CounterValue'];
|
||||
$cStart = $row[0]['CounterStart'];
|
||||
$cEnd = $row[0]['CounterEnd'];
|
||||
$cReset = $row[0]['CounterReset'];
|
||||
$cPad = strlen((string)$cEnd);
|
||||
// next value > end, back to start
|
||||
if($cValue > $cEnd) { $cValue = $cStart; }
|
||||
$cnum = str_pad($cValue, $cPad, "0", STR_PAD_LEFT);
|
||||
$cValue_next = $cValue+1;
|
||||
$this->set('CounterValue', $cValue_next)->where('CounterID',$CounterID)->update();
|
||||
return $cnum;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user