103 lines
3.4 KiB
PHP
103 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Organization;
|
|
|
|
use App\Traits\ResponseTrait;
|
|
use App\Controllers\BaseController;
|
|
use App\Models\Organization\CodingSysModel;
|
|
|
|
class CodingSysController extends BaseController {
|
|
use ResponseTrait;
|
|
|
|
protected $db;
|
|
protected $model;
|
|
|
|
public function __construct() {
|
|
$this->db = \Config\Database::connect();
|
|
$this->model = new CodingSysModel();
|
|
}
|
|
|
|
public function index() {
|
|
$filter = [
|
|
'CodingSysAbb' => $this->request->getVar('CodingSysAbb'),
|
|
'FullText' => $this->request->getVar('FullText'),
|
|
];
|
|
|
|
$builder = $this->model;
|
|
|
|
if (!empty($filter['CodingSysAbb'])) {
|
|
$builder->like('CodingSysAbb', $filter['CodingSysAbb'], 'both');
|
|
}
|
|
if (!empty($filter['FullText'])) {
|
|
$builder->like('FullText', $filter['FullText'], 'both');
|
|
}
|
|
|
|
$rows = $builder->findAll();
|
|
|
|
if (empty($rows)) {
|
|
return $this->respond(['status' => 'success', 'message' => 'no Data.', 'data' => []], 200);
|
|
}
|
|
|
|
return $this->respond(['status' => 'success', 'message' => 'fetch success', 'data' => $rows], 200);
|
|
}
|
|
|
|
public function show($CodingSysID = null) {
|
|
$row = $this->model->where('CodingSysID', $CodingSysID)->first();
|
|
|
|
if (empty($row)) {
|
|
return $this->respond(['status' => 'success', 'message' => 'no Data.', 'data' => null], 200);
|
|
}
|
|
|
|
return $this->respond(['status' => 'success', 'message' => 'fetch success', 'data' => $row], 200);
|
|
}
|
|
|
|
public function delete() {
|
|
try {
|
|
$input = $this->request->getJSON(true);
|
|
$id = $input['CodingSysID'] ?? null;
|
|
|
|
if (!$id) {
|
|
return $this->failValidationErrors('CodingSysID is required.');
|
|
}
|
|
|
|
$this->model->delete($id);
|
|
return $this->respondDeleted(['status' => 'success', 'message' => "{$id} deleted successfully."]);
|
|
} catch (\Throwable $e) {
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function create() {
|
|
$input = $this->request->getJSON(true);
|
|
|
|
try {
|
|
$id = $this->model->insert($input, true);
|
|
return $this->respondCreated(['status' => 'success', 'message' => 'data created successfully', 'data' => $id], 201);
|
|
} catch (\Throwable $e) {
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function update($CodingSysID = null) {
|
|
$input = $this->request->getJSON(true);
|
|
|
|
try {
|
|
if (!$CodingSysID || !ctype_digit((string) $CodingSysID)) {
|
|
return $this->failValidationErrors('CodingSysID is required.');
|
|
}
|
|
|
|
if (isset($input['CodingSysID']) && (string) $input['CodingSysID'] !== (string) $CodingSysID) {
|
|
return $this->failValidationErrors('CodingSysID in URL does not match body.');
|
|
}
|
|
|
|
$id = (int) $CodingSysID;
|
|
$input['CodingSysID'] = $id;
|
|
|
|
$this->model->update($id, $input);
|
|
return $this->respondCreated(['status' => 'success', 'message' => 'data updated successfully', 'data' => $id], 201);
|
|
} catch (\Throwable $e) {
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|