clqms-be/app/Controllers/Contact/OccupationController.php

65 lines
2.2 KiB
PHP
Raw Normal View History

2025-09-18 16:31:56 +07:00
<?php
2025-10-16 11:09:36 +07:00
namespace App\Controllers\Contact;
2025-09-18 16:31:56 +07:00
use App\Traits\ResponseTrait;
use App\Controllers\BaseController;
2025-10-14 18:53:06 +07:00
use App\Models\Contact\OccupationModel;
2025-09-18 16:31:56 +07:00
class OccupationController extends BaseController {
2025-09-18 16:31:56 +07:00
use ResponseTrait;
protected $db;
2025-10-14 18:53:06 +07:00
protected $model;
protected $rules;
2025-09-18 16:31:56 +07:00
public function __construct() {
$this->db = \Config\Database::connect();
2025-10-14 18:53:06 +07:00
$this->model = new OccupationModel();
$this->rules = [ 'OccCode' => 'required','OccText' => 'required' ];
2025-09-18 16:31:56 +07:00
}
public function index() {
2025-12-01 16:47:52 +07:00
$OccCode = $this->request->getVar('OccCode');
$OccText = $this->request->getVar('OccText');
$rows = $this->model->getOccupations($OccCode,$OccText);
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();
$row = $model->find($OccupationID);
if (empty($row)) {
return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => null ], 200);
2025-09-18 16:31:56 +07:00
}
return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $row ], 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->model->insert($input);
$id = $this->model->getInsertID();
return $this->respondCreated([ 'status' => 'success', 'message' => 'data created successfully', 'data' => $id ], 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 {
$this->model->update($input['OccupationID'], $input);
return $this->respondCreated([ 'status' => 'success', 'message' => 'Data updated successfully', 'data' => $input['OccupationID'] ], 201);
2025-09-18 16:31:56 +07:00
} catch (\Throwable $e) {
return $this->failServerError('Exception : ' . $e->getMessage());
2025-09-18 16:31:56 +07:00
}
}
}