169 lines
5.6 KiB
PHP
169 lines
5.6 KiB
PHP
<?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()->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) {
|
|
$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;
|
|
}
|
|
} |