83 lines
2.8 KiB
PHP
83 lines
2.8 KiB
PHP
<?php
|
|
namespace App\Controllers;
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
use CodeIgniter\Controller;
|
|
use App\Models\OccupationModel;
|
|
|
|
class Occupation extends Controller {
|
|
use ResponseTrait;
|
|
|
|
protected $db;
|
|
protected $modelOccupation;
|
|
protected $rulesOccupation;
|
|
|
|
public function __construct() {
|
|
$this->db = \Config\Database::connect();
|
|
$this->modelOccupation = new OccupationModel();
|
|
$this->rulesOccupation = [ 'OccCode' => 'required','OccText' => 'required' ];
|
|
}
|
|
|
|
public function index() {
|
|
$model = new OccupationModel();
|
|
$rows = $model->get()->getResultArray();
|
|
|
|
if (empty($rows)) {
|
|
return $this->respond([ 'status' => 'success', 'message' => "no 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();
|
|
|
|
if (empty($rows)) {
|
|
return $this->respond([ 'status' => 'success', 'message' => "no Data."], 200);
|
|
}
|
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $rows ], 200);
|
|
}
|
|
|
|
public function create() {
|
|
$input = $this->request->getJSON(true);
|
|
try {
|
|
$this->db->transStart();
|
|
$insert = $this->modelOccupation->insert($input);
|
|
$this->db->transComplete();
|
|
|
|
if ($this->db->transStatus() === false || !$insert) {
|
|
return $this->fail();
|
|
}
|
|
|
|
return $this->respondCreated([ 'status' => 'success', 'message' => 'data created successfully', 'data' => $input ], 201);
|
|
} catch (\Throwable $e) {
|
|
$this->db->transRollback();
|
|
return $this->failServerError('Exception : ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function update() {
|
|
$input = $this->request->getJSON(true);
|
|
try {
|
|
if (!$this->modelOccupation->find($input['OccupationID'])) {
|
|
return $this->failNotFound('Data not found');
|
|
}
|
|
|
|
$this->db->transStart();
|
|
$update = $this->modelOccupation->update($input['OccupationID'], $input);
|
|
$this->db->transComplete();
|
|
|
|
if ($this->db->transStatus() === false || !$insert) {
|
|
return $this->fail();
|
|
}
|
|
|
|
return $this->respondCreated([ 'status' => 'success', 'message' => 'Data updated successfully', 'data' => $input ], 201);
|
|
} catch (\Throwable $e) {
|
|
$this->db->transRollback();
|
|
return $this->failServerError('Exception : ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
} |