149 lines
4.6 KiB
PHP
149 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Api;
|
|
|
|
use App\Controllers\BaseController;
|
|
use App\Models\DictControlModel;
|
|
use App\Models\ControlTestModel;
|
|
|
|
class ControlApiController extends BaseController
|
|
{
|
|
protected $dictControlModel;
|
|
protected $controlTestModel;
|
|
protected $rules;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->dictControlModel = new DictControlModel();
|
|
$this->controlTestModel = new ControlTestModel();
|
|
$this->rules = [
|
|
'name' => 'required|min_length[1]',
|
|
'dept_ref_id' => 'required',
|
|
];
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$keyword = $this->request->getGet('keyword');
|
|
$deptId = $this->request->getGet('deptId');
|
|
try {
|
|
$rows = $this->dictControlModel->getWithDept($keyword, $deptId);
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'fetch success',
|
|
'data' => $rows
|
|
], 200);
|
|
} catch (\Exception $e) {
|
|
return $this->failServerError('Exception: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function show($id = null)
|
|
{
|
|
try {
|
|
$rows = $this->dictControlModel->where('control_id', $id)->findAll();
|
|
if (empty($rows)) {
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'data not found.'
|
|
], 200);
|
|
}
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'fetch success',
|
|
'data' => $rows
|
|
], 200);
|
|
} catch (\Exception $e) {
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function getTests($id = null)
|
|
{
|
|
try {
|
|
$rows = $this->controlTestModel->where('control_ref_id', $id)->findAll();
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'fetch success',
|
|
'data' => $rows
|
|
], 200);
|
|
} catch (\Exception $e) {
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$input = $this->request->getJSON(true);
|
|
if (!$this->validate($this->rules)) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
try {
|
|
$controlId = $this->dictControlModel->insert($input, true);
|
|
|
|
if (!empty($input['test_ids'])) {
|
|
foreach ($input['test_ids'] as $testId) {
|
|
$this->controlTestModel->insert([
|
|
'control_ref_id' => $controlId,
|
|
'test_ref_id' => $testId,
|
|
'mean' => 0,
|
|
'sd' => 0,
|
|
]);
|
|
}
|
|
}
|
|
|
|
return $this->respondCreated([
|
|
'status' => 'success',
|
|
'message' => $controlId
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function update($id = null)
|
|
{
|
|
$input = $this->request->getJSON(true);
|
|
if (!$this->validate($this->rules)) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
try {
|
|
$this->dictControlModel->update($id, $input);
|
|
|
|
if (!empty($input['test_ids'])) {
|
|
$this->controlTestModel->where('control_ref_id', $id)->delete();
|
|
foreach ($input['test_ids'] as $testId) {
|
|
$this->controlTestModel->insert([
|
|
'control_ref_id' => $id,
|
|
'test_ref_id' => $testId,
|
|
'mean' => 0,
|
|
'sd' => 0,
|
|
]);
|
|
}
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'update success',
|
|
'data' => $id
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function delete($id = null)
|
|
{
|
|
try {
|
|
$this->controlTestModel->where('control_ref_id', $id)->delete();
|
|
$this->dictControlModel->delete($id);
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'delete success'
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|