create occupation endpoint
This commit is contained in:
parent
7e86462b4f
commit
5c67b06431
@ -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');
|
||||
|
||||
169
app/Controllers/Occupation.php
Normal file
169
app/Controllers/Occupation.php
Normal file
@ -0,0 +1,169 @@
|
||||
<?php
|
||||
namespace App\Controllers;
|
||||
|
||||
use CodeIgniter\API\ResponseTrait;
|
||||
use CodeIgniter\Controller;
|
||||
use CodeIgniter\Database\RawSql;
|
||||
|
||||
class Occupation extends Controller {
|
||||
use ResponseTrait;
|
||||
|
||||
public function __construct() {
|
||||
$this->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;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user