From 5c67b06431ed1bb9e0f07828586bd04a2c668948 Mon Sep 17 00:00:00 2001 From: mahdahar <89adham@gmail.com> Date: Thu, 18 Sep 2025 16:31:56 +0700 Subject: [PATCH] create occupation endpoint --- app/Config/Routes.php | 6 ++ app/Controllers/Occupation.php | 169 +++++++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 app/Controllers/Occupation.php diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 26bc1ed..cd0a95a 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -61,6 +61,12 @@ $routes->post('/api/contact', 'Contact::create'); $routes->patch('/api/contact', 'Contact::update'); $routes->delete('/api/contact', 'Contact::delete'); +$routes->get('/api/occupation', 'Occupation::index'); +$routes->get('/api/occupation/(:num)', 'Occupation::show/$1'); +$routes->post('/api/occupation', 'Occupation::create'); +$routes->patch('/api/occupation', 'Occupation::update'); +//$routes->delete('/api/occupation', 'Occupation::delete'); + $routes->get('/api/valueset', 'ValueSet::index'); $routes->get('/api/valueset/(:num)', 'ValueSet::show/$1'); $routes->post('/api/valueset', 'ValueSet::create'); diff --git a/app/Controllers/Occupation.php b/app/Controllers/Occupation.php new file mode 100644 index 0000000..bd1ed53 --- /dev/null +++ b/app/Controllers/Occupation.php @@ -0,0 +1,169 @@ +db = \Config\Database::connect(); + $this->rulesOccupation = [ + 'AbbTex' => 'required', + 'FullText' => 'required' + ]; + } + + public function index() { + $rows = $this->db->table('occupation')->select("*")->get()->getRowArray(); + + if (empty($rows)) { + return $this->respond([ + 'status' => 'success', + 'message' => "no Data.", + 'data' => $rows, + ], 200); + } + + return $this->respond([ + 'status' => 'success', + 'message'=> "fetch success", + 'data' => $rows, + ], 200); + } + + public function show($OccupationID = null) { + $rows=$this->db->table('occupation')->select("*")->where('occupationID', (int) $OccupationID)->get()->getRowArray(); + + 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; + } +} \ No newline at end of file