2025-10-14 16:54:43 +07:00

86 lines
3.4 KiB
PHP

<?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');
$rows = $this->model->getValueSets($param);
if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200); }
return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200);
}
public function show($VID = null) {
$rows = $this->model->getValueSet($VID);
if (empty($rows)) {
return $this->respond([ 'status' => 'success', 'message' => "ValueSet with ID $VID not found.", 'data' => [] ], 200);
}
return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows], 200);
}
public function showByValueSetDef($VSetID = null) {
$rows = $this->db->table('valueset')
->select("*")
->where('VSetID', (int) $VSetID)
->get()->getResultArray();
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());
}
}
}