db = \Config\Database::connect(); $this->rulesValueSet = [ 'VSetID' => 'required', 'VValue' => 'required', ]; } public function index() { $rows = $this->db->table('valueset') ->select("*") ->get()->getResultArray(); if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [], ], 200); } return $this->respond([ 'status' => 'success', 'message'=> "Value Set fetched successfully", 'data' => $rows, ], 200); } public function show($VID = null) { $rows = $this->db->table('valueset') ->select("valueset.*, valuesetdef.VSName") ->join('valuesetdef', 'valuesetdef.VSetID = valueset.VSetID', 'LEFT') ->where('valueset.VID', (int) $VID) ->get()->getResultArray(); 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 create() { try { $input = $this->request->getJSON(true); $dataValueSet = $this->prepareValueSetData($input); if (!$this->validateData($dataValueSet, $this->rulesValueSet)) { return $this->failValidationErrors($this->validator->getErrors()); } // Start transaction $this->db->transStart(); // Insert $this->db->table('valueset')->insert($dataValueSet); // Complete transaction $this->db->transComplete(); if ($this->db->transStatus() === false) { $dbError = $this->db->error(); return $this->failServerError( 'Failed to create data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown database error') ); } return $this->respondCreated([ 'status' => 'success', 'message' => 'Data created successfully', 'data' => $dataValueSet, ], 201); } catch (\Throwable $e) { // Ensure rollback if something goes wrong if ($this->db->transStatus() !== false) { $this->db->transRollback(); } return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } public function update() { try { $input = $this->request->getJSON(true); $VID = $input["VID"]; if (!$VID) { return $this->failValidationErrors('VID is required.'); } $dataValueSet = $this->prepareValueSetData($input); if (!$this->validateData($dataValueSet, $this->rulesValueSet)) { return $this->failValidationErrors( $this->validator->getErrors() ); } // Start transaction $this->db->transStart(); // Insert location $this->db->table('valueset')->where('VID', $VID)->update($dataValueSet); // Complete transaction $this->db->transComplete(); if ($this->db->transStatus() === false) { $dbError = $this->db->error(); return $this->failServerError( 'Failed to update data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown database error') ); } return $this->respondCreated([ 'status' => 'success', 'message' => 'Data updated successfully', 'data' => $dataValueSet, ], 201); } catch (\Throwable $e) { // Ensure rollback if something goes wrong if ($this->db->transStatus() !== false) { $this->db->transRollback(); } return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } public function delete() { try { $input = $this->request->getJSON(true); $VID = $input["VID"]; if (!$VID) { return $this->failValidationErrors('VID is required.'); } $valueset = $this->db->table('valuset')->where('VID', $VID)->get()->getRow(); if (!$valueset) { return $this->failNotFound("VID with {$VID} not found."); } $this->db->table('valueset')->where('VID', $VID)->update(['EndDate' => $this->now ]); return $this->respondDeleted([ 'status' => 'success', 'message' => "Data with ID {$VID} deleted successfully." ]); } catch (\Throwable $e) { // Ensure rollback if something goes wrong if ($this->db->transStatus() !== false) { $this->db->transRollback(); } return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } private function prepareValueSetData(array $input): array { $data = [ "VSetID" => $input['VSetID'] ?? null, "VOrder" => $input['VOrder'] ?? null, "VValue" => $input['VValue'] ?? null, "VDesc" => $input['VDesc'] ?? null, "VCategory" => $input['VCategory'] ?? null ]; if(!empty($input["VID"])) { $data["VID"]=$input["VID"]; } return $data; } }