100 lines
3.2 KiB
PHP
Executable File
100 lines
3.2 KiB
PHP
Executable File
<?php
|
|
namespace App\Controllers\Master;
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
use App\Controllers\BaseController;
|
|
use App\Models\Master\MasterControlsModel;
|
|
|
|
class MasterControlsController extends BaseController {
|
|
use ResponseTrait;
|
|
|
|
protected $model;
|
|
protected $rules;
|
|
|
|
public function __construct() {
|
|
$this->model = new MasterControlsModel();
|
|
$this->rules = [
|
|
'controlName' => 'required|min_length[1]',
|
|
'lot' => 'required|min_length[1]',
|
|
];
|
|
}
|
|
|
|
public function index() { $keyword = $this->request->getGet('keyword'); $deptId = $this->request->getGet('dept_id'); try { $rows = $this->model->search($keyword, $deptId);
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'fetch success',
|
|
'data' => $rows
|
|
], 200)->setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
|
|
} catch (\Exception $e) {
|
|
return $this->failServerError($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function show($id = null) {
|
|
try {
|
|
$row = $this->model->find($id);
|
|
if (!$row) {
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'data not found.'
|
|
], 200);
|
|
}
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'fetch success',
|
|
'data' => [$row]
|
|
], 200);
|
|
} catch (\Exception $e) {
|
|
return $this->failServerError($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function create() {
|
|
$input = $this->request->getJSON(true);
|
|
$input = camel_to_snake_array($input);
|
|
if (!$this->validate($this->rules)) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
try {
|
|
$id = $this->model->insert($input, true);
|
|
return $this->respondCreated([
|
|
'status' => 'success',
|
|
'message' => $id
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return $this->failServerError($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function update($id = null) {
|
|
$input = $this->request->getJSON(true);
|
|
if (!$this->validate($this->rules)) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
$input = camel_to_snake_array($input);
|
|
try {
|
|
$this->model->update($id, $input);
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'update success',
|
|
'data' => [$id]
|
|
], 200);
|
|
} catch (\Exception $e) {
|
|
return $this->failServerError($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function delete($id = null) {
|
|
try {
|
|
$this->model->delete($id);
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'delete success',
|
|
'data' => [$id]
|
|
], 200);
|
|
} catch (\Exception $e) {
|
|
return $this->failServerError($e->getMessage());
|
|
}
|
|
}
|
|
}
|