clqms-be/app/Controllers/Organization/HostComParaController.php

117 lines
3.9 KiB
PHP
Raw Normal View History

<?php
namespace App\Controllers\Organization;
2026-04-08 08:37:41 +07:00
use App\Traits\PatchValidationTrait;
use App\Traits\ResponseTrait;
use App\Controllers\BaseController;
use App\Models\Organization\HostComParaModel;
class HostComParaController extends BaseController {
use ResponseTrait;
2026-04-08 08:37:41 +07:00
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'])) {
$builder->where('hostcompara.HostAppID', $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) {
$row = $this->model->select('hostcompara.*, hostapp.HostAppName')
->join('hostapp', 'hostapp.HostAppID = hostcompara.HostAppID', 'left')
->where('hostcompara.HostAppID', $HostAppID)
->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['HostAppID'] ?? null;
if (!$id) {
return $this->failValidationErrors('HostAppID 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($HostAppID = null) {
2026-04-08 08:37:41 +07:00
$input = $this->requirePatchPayload($this->request->getJSON(true));
if ($input === null) {
return;
}
2026-04-08 08:37:41 +07:00
$id = $this->requirePatchId($HostAppID, 'HostAppID');
if ($id === null) {
return;
}
2026-04-08 08:37:41 +07:00
$existing = $this->model->find($id);
if (!$existing) {
return $this->respond(['status' => 'failed', 'message' => 'HostComPara not found', 'data' => []], 404);
}
2026-04-08 08:37:41 +07:00
if (isset($input['HostAppID']) && (string) $input['HostAppID'] !== (string) $id) {
return $this->failValidationErrors('HostAppID in URL does not match body.');
}
2026-04-08 08:37:41 +07:00
$input['HostAppID'] = $id;
try {
$this->model->update($id, $input);
2026-04-08 08:37:41 +07:00
return $this->respond(['status' => 'success', 'message' => 'data updated successfully', 'data' => $id], 200);
} catch (\Throwable $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
}