tinyqc/app/Controllers/Api/TestApiController.php
mahdahar ff90e0eb29 Initial commit: Add CodeIgniter 4 QC application with full MVC structure
- CodeIgniter 4 framework setup with SQL Server database config
- Models: Control, Test, Dept, Result, Daily/ Monthly entry models
- Controllers: Dashboard, Control, Test, Dept, Entry, Report, API endpoints
- Views: CRUD pages with modal dialogs, dashboard, reports
- Database: Migrations for control test and daily/monthly result tables
- Legacy v1 PHP application preserved in /v1 directory
- Documentation: AGENTS.md, VIEWS_RULES.md for development guidelines
2026-01-14 16:49:27 +07:00

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);
}
}