- 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
136 lines
4.7 KiB
PHP
Executable File
136 lines
4.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\HostComParaModel;
|
|
|
|
class HostComParaController extends BaseController {
|
|
use ResponseTrait;
|
|
use PatchValidationTrait;
|
|
|
|
protected $db;
|
|
protected $model;
|
|
|
|
public function __construct() {
|
|
$this->db = \Config\Database::connect();
|
|
$this->model = new HostComParaModel();
|
|
}
|
|
|
|
public function index() {
|
|
$filter = [
|
|
'HostAppID' => $this->request->getVar('HostAppID'),
|
|
'HostIP' => $this->request->getVar('HostIP'),
|
|
];
|
|
|
|
$builder = $this->model->select('hostcompara.*, hostapp.HostAppName')
|
|
->join('hostapp', 'hostapp.HostAppID = hostcompara.HostAppID', 'left');
|
|
|
|
if (!empty($filter['HostAppID'])) {
|
|
if (!ctype_digit((string) $filter['HostAppID'])) {
|
|
return $this->failValidationErrors('HostAppID filter must be a valid integer.');
|
|
}
|
|
$builder->where('hostcompara.HostAppID', (int) $filter['HostAppID']);
|
|
}
|
|
if (!empty($filter['HostIP'])) {
|
|
$builder->like('hostcompara.HostIP', $filter['HostIP'], '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($HostAppID = null) {
|
|
$id = $this->requirePatchId($HostAppID, 'HostAppID');
|
|
if ($id === null) {
|
|
return;
|
|
}
|
|
|
|
$row = $this->model->select('hostcompara.*, hostapp.HostAppName')
|
|
->join('hostapp', 'hostapp.HostAppID = hostcompara.HostAppID', 'left')
|
|
->where('hostcompara.HostAppID', $id)
|
|
->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 = $this->requirePatchId($input['HostAppID'] ?? null, 'HostAppID');
|
|
if ($id === null) {
|
|
return;
|
|
}
|
|
|
|
$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 {
|
|
$hostAppId = $input['HostAppID'] ?? null;
|
|
if ($hostAppId === null) {
|
|
return $this->failValidationErrors('HostAppID is required.');
|
|
}
|
|
|
|
if (!ctype_digit((string) $hostAppId)) {
|
|
return $this->failValidationErrors('HostAppID must be a valid integer.');
|
|
}
|
|
|
|
$input['HostAppID'] = (int) $hostAppId;
|
|
|
|
$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($HostAppID = null) {
|
|
$input = $this->requirePatchPayload($this->request->getJSON(true));
|
|
if ($input === null) {
|
|
return;
|
|
}
|
|
|
|
$id = $this->requirePatchId($HostAppID, 'HostAppID');
|
|
if ($id === null) {
|
|
return;
|
|
}
|
|
|
|
$existing = $this->model->find($id);
|
|
if (!$existing) {
|
|
return $this->respond(['status' => 'failed', 'message' => 'HostComPara not found', 'data' => []], 404);
|
|
}
|
|
|
|
if (isset($input['HostAppID'])) {
|
|
if ((string) $input['HostAppID'] !== (string) $id) {
|
|
return $this->failValidationErrors('HostAppID in URL does not match body.');
|
|
}
|
|
unset($input['HostAppID']);
|
|
}
|
|
|
|
try {
|
|
$this->model->update($id, $input);
|
|
return $this->respond(['status' => 'success', 'message' => 'data updated successfully', 'data' => $id], 200);
|
|
} catch (\Throwable $e) {
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|