clqms-be/app/Controllers/Occupation.php

83 lines
2.8 KiB
PHP
Raw Normal View History

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