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

62 lines
2.1 KiB
PHP

<?php
namespace App\Controllers;
use CodeIgniter\API\ResponseTrait;
use App\Controllers\BaseController;
use App\Models\Contact\OccupationModel;
class Occupation extends BaseController {
use ResponseTrait;
protected $db;
protected $model;
protected $rules;
public function __construct() {
$this->db = \Config\Database::connect();
$this->model = new OccupationModel();
$this->rules = [ 'OccCode' => 'required','OccText' => 'required' ];
}
public function index() {
$model = new OccupationModel();
$rows = $model->findAll();
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->find($OccupationID);
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->model->insert($input);
$id = $this->model->getInsertID();
return $this->respondCreated([ 'status' => 'success', 'message' => 'data created successfully', 'data' => $id ], 201);
} catch (\Throwable $e) {
$this->db->transRollback();
return $this->failServerError('Exception : ' . $e->getMessage());
}
}
public function update() {
$input = $this->request->getJSON(true);
try {
$this->model->update($input['OccupationID'], $input);
return $this->respondCreated([ 'status' => 'success', 'message' => 'Data updated successfully', 'data' => $input['OccupationID'] ], 201);
} catch (\Throwable $e) {
return $this->failServerError('Exception : ' . $e->getMessage());
}
}
}