2025-10-29 11:08:38 +07:00
|
|
|
<?php
|
|
|
|
|
namespace App\Controllers\Organization;
|
|
|
|
|
|
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
|
use App\Controllers\BaseController;
|
|
|
|
|
|
2025-11-04 12:22:29 +07:00
|
|
|
use App\Models\Organization\SiteModel;
|
2025-10-29 11:08:38 +07:00
|
|
|
|
2025-11-04 12:22:29 +07:00
|
|
|
class Site extends BaseController {
|
2025-10-29 11:08:38 +07:00
|
|
|
use ResponseTrait;
|
|
|
|
|
|
|
|
|
|
protected $db;
|
|
|
|
|
protected $model;
|
|
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
$this->db = \Config\Database::connect();
|
2025-11-04 12:22:29 +07:00
|
|
|
$this->model = new SiteModel();
|
2025-10-29 11:08:38 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function index() {
|
2025-11-06 12:28:42 +07:00
|
|
|
//$rows = $this->model->findAll();
|
|
|
|
|
$rows = $this->model->getSites();
|
2025-10-29 11:08:38 +07:00
|
|
|
|
|
|
|
|
if (empty($rows)) {
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $rows ], 200);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-04 12:22:29 +07:00
|
|
|
public function show($SiteID = null) {
|
2025-11-06 12:28:42 +07:00
|
|
|
//$rows = $this->model->where('SiteID', $SiteID)->findAll();
|
|
|
|
|
$rows = $this->model->getSite($SiteID);
|
2025-10-29 11:08:38 +07:00
|
|
|
|
|
|
|
|
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 delete() {
|
|
|
|
|
try {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
2025-11-04 12:22:29 +07:00
|
|
|
$id = $input["SiteID"];
|
2025-10-29 11:08:38 +07:00
|
|
|
if (!$id) { return $this->failValidationErrors('ID 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() {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
|
|
|
|
try {
|
2025-11-04 12:22:29 +07:00
|
|
|
$id = $input['SiteID'];
|
|
|
|
|
if (!$id) { return $this->failValidationErrors('ID is required.'); }
|
2025-10-31 11:11:45 +07:00
|
|
|
$this->model->update($id, $input);
|
2025-10-29 11:08:38 +07:00
|
|
|
return $this->respondCreated([ 'status' => 'success', 'message' => 'data updated successfully', 'data' => $id ], 201);
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|