2026-02-27 16:31:55 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers\Organization;
|
|
|
|
|
|
|
|
|
|
use App\Traits\ResponseTrait;
|
|
|
|
|
use App\Controllers\BaseController;
|
|
|
|
|
use App\Models\Organization\HostAppModel;
|
|
|
|
|
|
|
|
|
|
class HostAppController extends BaseController {
|
|
|
|
|
use ResponseTrait;
|
|
|
|
|
|
|
|
|
|
protected $db;
|
|
|
|
|
protected $model;
|
|
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
$this->db = \Config\Database::connect();
|
|
|
|
|
$this->model = new HostAppModel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function index() {
|
|
|
|
|
$filter = [
|
|
|
|
|
'HostAppID' => $this->request->getVar('HostAppID'),
|
|
|
|
|
'HostAppName' => $this->request->getVar('HostAppName'),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$builder = $this->model->select('hostapp.*, site.SiteName')
|
|
|
|
|
->join('site', 'site.SiteID = hostapp.SiteID', 'left');
|
|
|
|
|
|
|
|
|
|
if (!empty($filter['HostAppID'])) {
|
|
|
|
|
$builder->like('hostapp.HostAppID', $filter['HostAppID'], 'both');
|
|
|
|
|
}
|
|
|
|
|
if (!empty($filter['HostAppName'])) {
|
|
|
|
|
$builder->like('hostapp.HostAppName', $filter['HostAppName'], '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('hostapp.*, site.SiteName')
|
|
|
|
|
->join('site', 'site.SiteID = hostapp.SiteID', 'left')
|
|
|
|
|
->where('hostapp.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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 15:58:56 +07:00
|
|
|
public function update($HostAppID = null) {
|
2026-02-27 16:31:55 +07:00
|
|
|
$input = $this->request->getJSON(true);
|
|
|
|
|
|
|
|
|
|
try {
|
2026-03-16 15:58:56 +07:00
|
|
|
if ($HostAppID === null || $HostAppID === '') {
|
2026-02-27 16:31:55 +07:00
|
|
|
return $this->failValidationErrors('HostAppID is required.');
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 15:58:56 +07:00
|
|
|
if (isset($input['HostAppID']) && (string) $input['HostAppID'] !== (string) $HostAppID) {
|
|
|
|
|
return $this->failValidationErrors('HostAppID in URL does not match body.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$id = $HostAppID;
|
|
|
|
|
$input['HostAppID'] = $id;
|
2026-02-27 16:31:55 +07:00
|
|
|
$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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|