121 lines
3.2 KiB
PHP
121 lines
3.2 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Controllers\Api;
|
||
|
|
|
||
|
|
use App\Controllers\BaseController;
|
||
|
|
use App\Models\DictTestModel;
|
||
|
|
use App\Models\DictDeptModel;
|
||
|
|
|
||
|
|
class TestApiController extends BaseController
|
||
|
|
{
|
||
|
|
protected $dictTestModel;
|
||
|
|
protected $dictDeptModel;
|
||
|
|
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
$this->dictTestModel = new DictTestModel();
|
||
|
|
$this->dictDeptModel = new DictDeptModel();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function index()
|
||
|
|
{
|
||
|
|
$tests = $this->dictTestModel->getWithDept();
|
||
|
|
return $this->response->setJSON([
|
||
|
|
'status' => 'success',
|
||
|
|
'message' => 'Tests fetched successfully',
|
||
|
|
'data' => $tests
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function show($id)
|
||
|
|
{
|
||
|
|
$test = $this->dictTestModel->find($id);
|
||
|
|
if (!$test) {
|
||
|
|
return $this->response->setJSON([
|
||
|
|
'status' => 'error',
|
||
|
|
'message' => 'Test not found'
|
||
|
|
])->setStatusCode(404);
|
||
|
|
}
|
||
|
|
return $this->response->setJSON([
|
||
|
|
'status' => 'success',
|
||
|
|
'data' => $test
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function store()
|
||
|
|
{
|
||
|
|
$post = $this->request->getJSON(true);
|
||
|
|
|
||
|
|
$testData = [
|
||
|
|
'dept_ref_id' => $post['dept_ref_id'] ?? null,
|
||
|
|
'name' => $post['name'] ?? '',
|
||
|
|
'unit' => $post['unit'] ?? '',
|
||
|
|
'method' => $post['method'] ?? '',
|
||
|
|
'cva' => $post['cva'] ?? '',
|
||
|
|
'ba' => $post['ba'] ?? '',
|
||
|
|
'tea' => $post['tea'] ?? '',
|
||
|
|
];
|
||
|
|
|
||
|
|
$id = $this->dictTestModel->insert($testData);
|
||
|
|
|
||
|
|
if ($id) {
|
||
|
|
return $this->response->setJSON([
|
||
|
|
'status' => 'success',
|
||
|
|
'message' => 'Test saved successfully',
|
||
|
|
'data' => ['test_id' => $id]
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->response->setJSON([
|
||
|
|
'status' => 'error',
|
||
|
|
'message' => 'Failed to save test'
|
||
|
|
])->setStatusCode(500);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function update($id)
|
||
|
|
{
|
||
|
|
$post = $this->request->getJSON(true);
|
||
|
|
|
||
|
|
$testData = [
|
||
|
|
'dept_ref_id' => $post['dept_ref_id'] ?? null,
|
||
|
|
'name' => $post['name'] ?? '',
|
||
|
|
'unit' => $post['unit'] ?? '',
|
||
|
|
'method' => $post['method'] ?? '',
|
||
|
|
'cva' => $post['cva'] ?? '',
|
||
|
|
'ba' => $post['ba'] ?? '',
|
||
|
|
'tea' => $post['tea'] ?? '',
|
||
|
|
];
|
||
|
|
|
||
|
|
$success = $this->dictTestModel->update($id, $testData);
|
||
|
|
|
||
|
|
if ($success) {
|
||
|
|
return $this->response->setJSON([
|
||
|
|
'status' => 'success',
|
||
|
|
'message' => 'Test updated successfully'
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->response->setJSON([
|
||
|
|
'status' => 'error',
|
||
|
|
'message' => 'Failed to update test'
|
||
|
|
])->setStatusCode(500);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function delete($id)
|
||
|
|
{
|
||
|
|
$success = $this->dictTestModel->delete($id);
|
||
|
|
|
||
|
|
if ($success) {
|
||
|
|
return $this->response->setJSON([
|
||
|
|
'status' => 'success',
|
||
|
|
'message' => 'Test deleted successfully'
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->response->setJSON([
|
||
|
|
'status' => 'error',
|
||
|
|
'message' => 'Failed to delete test'
|
||
|
|
])->setStatusCode(500);
|
||
|
|
}
|
||
|
|
}
|