- handle contact PATCH failures by checking model save result and returning HTTP 400 with the model error message - update ContactDetailModel nested updates to enforce active-detail checks and use model update() with explicit failure propagation - extend contact patch assertions and align test-create variants expectations to status=success for POST responses - refresh composer lock metadata/dependency constraints and include generated docs/data/test files updated during normalization - impact: API contract unchanged except clearer 400 error responses on invalid contact detail updates
113 lines
3.7 KiB
PHP
Executable File
113 lines
3.7 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Controllers\Organization;
|
|
|
|
use App\Traits\PatchValidationTrait;
|
|
use App\Traits\ResponseTrait;
|
|
use App\Controllers\BaseController;
|
|
use App\Models\Organization\CodingSysModel;
|
|
|
|
class CodingSysController extends BaseController {
|
|
use ResponseTrait;
|
|
use PatchValidationTrait;
|
|
|
|
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->requirePatchPayload($this->request->getJSON(true));
|
|
if ($input === null) {
|
|
return;
|
|
}
|
|
|
|
$id = $this->requirePatchId($CodingSysID, 'CodingSysID');
|
|
if ($id === null) {
|
|
return;
|
|
}
|
|
|
|
$existing = $this->model->find($id);
|
|
if (!$existing) {
|
|
return $this->respond(['status' => 'failed', 'message' => 'CodingSys not found', 'data' => []], 404);
|
|
}
|
|
|
|
if (isset($input['CodingSysID']) && (string) $input['CodingSysID'] !== (string) $id) {
|
|
return $this->failValidationErrors('CodingSysID in URL does not match body.');
|
|
}
|
|
|
|
$input['CodingSysID'] = $id;
|
|
|
|
try {
|
|
$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());
|
|
}
|
|
}
|
|
}
|