db = \Config\Database::connect(); $this->rulesOccupation = [ 'AbbTex' => 'required', 'FullText' => 'required' ]; } public function index() { $model = new OccupationModel(); $rows = $model->get()->getResultArray(); 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 show($OccupationID = null) { $model = new OccupationModel(); $rows = $model->where('occupationID', (int) $OccupationID)->get()->getResultArray(); //$rows=$this->db->table('occupation')->select("*")->where('occupationID', (int) $OccupationID)->get()->getResultArray(); if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "Data 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); $dataOccupation = $this->prepareOccupationData($input); if (!$this->validateData($dataOccupation, $this->rulesOccupation)) { return $this->failValidationErrors($this->validator->getErrors()); } $this->db->transStart(); $this->db->table('contact')->insert($dataOccupation); if ($this->db->transStatus() === false) { $dbError = $this->db->error(); return $this->failServerError( 'Failed to create data (transaction rolled back): ' . ( $dbError['message'] ?? 'Unknown database error') ); } // Complete transaction $this->db->transComplete(); return $this->respondCreated([ 'status' => 'success', 'message' => 'data created successfully', 'data' => $dataOccupation, ], 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); $dataOccupation = $this->prepareOccupationData($input); if (!$this->validateData($dataOccupation, $this->rulesOccupation)) { return $this->failValidationErrors( $this->validator->getErrors()); } $this->db->transStart(); $this->db->table('occupation')->where('OccupationID', $dataOccupation["OccupationID"])->update($dataOccupation); if ($this->db->transStatus() === false) { $dbError = $this->db->error(); return $this->failServerError( 'Failed to update data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown database error') ); } $this->db->transComplete(); return $this->respondCreated([ 'status' => 'success', 'message' => 'Occupation updated successfully', 'data' => $dataContact, ], 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); $OccupationID = $input["OccupationID"]; if (!$OccupationID) { return $this->failValidationError('ContactID is required.'); } $occupation = $this->db->table('occupation')->where('OccupationID', $OccupationID)->get()->getRow(); if (!$occupation) { return $this->failNotFound("data with {$OccupationID} not found."); } $this->db->table('occupation')->where('OccupationID', $OccupationID)->update(['EndDate' => NOW()]); return $this->respondDeleted([ 'status' => 'success', 'message' => "{$ContactID} 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 prepareOccupationData(array $input): array { $data = [ "AbbTex" => $input['AbbTex'] ?? null, "FullText" => $input['FullText'] ?? null, "Description" => $input['Description'] ?? null, ]; if(!empty($input["OccupationID"])) { $data["OccupationID"] = $input["OccupationID"]; } return $data; } }