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

62 lines
2.1 KiB
PHP
Raw Normal View History

2025-09-18 16:31:56 +07:00
<?php
namespace App\Controllers;
use CodeIgniter\API\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
2025-10-14 18:53:06 +07:00
class Occupation 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() {
$model = new OccupationModel();
2025-10-14 18:53:06 +07:00
$rows = $model->findAll();
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();
2025-10-14 18:53:06 +07:00
$rows = $model->find($OccupationID);
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->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
}
}
}