db = \Config\Database::connect(); $this->rulesValueSetFld = [ 'VSet' => 'required', 'VSName' => 'required', 'VSDesc' => 'required' ]; } public function index() { $rows = $this->db->table('valuesetfld') ->select("*") ->get()->getResultArray(); 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($VSFldID = null) { $rows = $this->db->table('valuesetfld') ->select("*") ->where('VSFldID', (int) $VSFldID) ->get()->getResultArray(); if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "data with ID $VSFldID 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); $dataValueSetFld = $this->prepareData($input); if (!$this->validateData($dataValueSetFld, $this->rulesValueSetFld)) { return $this->failValidationErrors($this->validator->getErrors()); } $this->db->transStart(); $this->db->table('valuesetfld')->insert($dataValueSetFld); $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' => $dataValueSetFld, ], 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); $VSFldID = $input["VSFldID"]; if (!$VSFldID) { return $this->failValidationErrors('VSFldID is required.'); } $dataValueSetFld = $this->prepareData($input); if (!$this->validateData($data, $this->rulesValueSetFld)) { return $this->failValidationErrors( $this->validator->getErrors()); } $this->db->transStart(); $this->db->table('valuesetfld')->where('VSFldID', $VSFldID)->update($data); $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' => $dataValueSetFld, ], 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); $VSFldID = $input["VSFldID"]; if (!$VSFldID) { return $this->failValidationErrors('VSFldID is required.'); } $valuesetfld = $this->db->table('valuesetfld')->where('VSFldID', $VSFldID)->get()->getRow(); if (!$valuesetfld) { return $this->failNotFound("Data with {$VSFldID} not found."); } $this->db->table('valuesetfld')->where('VSFldID', $VSFldID)->update(['EndDate' => $this->now]); return $this->respondDeleted([ 'status' => 'success', 'message' => "data with {$VSFldID} 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 prepareData(array $input, string $now, string $mode = 'create'): array { $data = [ "Vset" => $input['VSet'] ?? null, "VSName" => $input['VSName'] ?? null, "VSDesc" => $input['VSDesc'] ?? null ]; return $data; } }