2025-10-14 16:54:43 +07:00
|
|
|
<?php
|
|
|
|
|
namespace App\Controllers\ValueSet;
|
|
|
|
|
|
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
|
use App\Controllers\BaseController;
|
|
|
|
|
use App\Models\ValueSet\ValueSetModel;
|
|
|
|
|
|
|
|
|
|
class ValueSet extends BaseController {
|
|
|
|
|
use ResponseTrait;
|
|
|
|
|
|
|
|
|
|
protected $db;
|
|
|
|
|
protected $model;
|
|
|
|
|
protected $rules;
|
|
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
$this->db = \Config\Database::connect();
|
|
|
|
|
$this->model = new ValueSetModel;
|
|
|
|
|
$this->rules = [
|
|
|
|
|
'VSetID' => 'required',
|
|
|
|
|
'VValue' => 'required',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function index() {
|
|
|
|
|
$param = $this->request->getVar('param');
|
2025-12-30 14:30:35 +07:00
|
|
|
$VSetID = $this->request->getVar('VSetID');
|
|
|
|
|
$page = $this->request->getVar('page') ?? 1;
|
|
|
|
|
$limit = $this->request->getVar('limit') ?? 20;
|
|
|
|
|
|
|
|
|
|
$result = $this->model->getValueSets($param, $page, $limit, $VSetID);
|
|
|
|
|
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message'=> "Data fetched successfully",
|
|
|
|
|
'data' => $result['data'],
|
|
|
|
|
'pagination' => [
|
|
|
|
|
'currentPage' => (int)$page,
|
|
|
|
|
'totalPages' => $result['pager']->getPageCount(),
|
|
|
|
|
'totalItems' => $result['pager']->getTotal(),
|
|
|
|
|
'limit' => (int)$limit
|
|
|
|
|
]
|
|
|
|
|
], 200);
|
2025-10-14 16:54:43 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function show($VID = null) {
|
2025-12-29 12:55:31 +07:00
|
|
|
$row = $this->model->getValueSet($VID);
|
|
|
|
|
if (empty($row)) {
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message' => "ValueSet with ID $VID not found.", 'data' => null ], 200);
|
2025-10-14 16:54:43 +07:00
|
|
|
}
|
2025-12-29 12:55:31 +07:00
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $row], 200);
|
2025-10-14 16:54:43 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function showByValueSetDef($VSetID = null) {
|
2025-10-14 18:53:06 +07:00
|
|
|
$rows = $this->model->getValueSetByValueSetDef($VSetID);
|
2025-10-14 16:54:43 +07:00
|
|
|
if (empty($rows)) {
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message' => "ValueSet not found.", 'data' => [] ], 200);
|
|
|
|
|
}
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function create() {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
|
|
|
|
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); }
|
|
|
|
|
try {
|
|
|
|
|
$VID = $this->model->insert($input);
|
|
|
|
|
return $this->respondCreated([ 'status' => 'success', 'message' => "data $VID created successfully" ]);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update() {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
|
|
|
|
$VID = $input["VID"];
|
|
|
|
|
if (!$VID) { return $this->failValidationErrors('VID is required.'); }
|
|
|
|
|
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors( $this->validator->getErrors() ); }
|
|
|
|
|
try {
|
|
|
|
|
$this->model->update($VID,$input);
|
|
|
|
|
return $this->respondCreated([ 'status' => 'success', 'message' => "data $VID updated successfully" ]);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function delete() {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
|
|
|
|
$VID = $input["VID"];
|
|
|
|
|
if (!$VID) { return $this->failValidationErrors('VID is required.'); }
|
|
|
|
|
try {
|
|
|
|
|
$this->model->delete($VID);
|
|
|
|
|
return $this->respondDeleted(['status' => 'success', 'message' => "Data $VID deleted successfully."]);
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|