tinyqc/app/Controllers/Api/DeptApiController.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

106 lines
2.6 KiB
PHP

<?php
namespace App\Controllers\Api;
use App\Controllers\BaseController;
use App\Models\DictDeptModel;
class DeptApiController extends BaseController
{
protected $dictDeptModel;
public function __construct()
{
$this->dictDeptModel = new DictDeptModel();
}
public function index()
{
$depts = $this->dictDeptModel->findAll();
return $this->response->setJSON([
'status' => 'success',
'message' => 'Departments fetched successfully',
'data' => $depts
]);
}
public function show($id)
{
$dept = $this->dictDeptModel->find($id);
if (!$dept) {
return $this->response->setJSON([
'status' => 'error',
'message' => 'Department not found'
])->setStatusCode(404);
}
return $this->response->setJSON([
'status' => 'success',
'data' => $dept
]);
}
public function store()
{
$post = $this->request->getJSON(true);
$data = [
'name' => $post['name'] ?? '',
];
$id = $this->dictDeptModel->insert($data);
if ($id) {
return $this->response->setJSON([
'status' => 'success',
'message' => 'Department saved successfully',
'data' => ['dept_id' => $id]
]);
}
return $this->response->setJSON([
'status' => 'error',
'message' => 'Failed to save department'
])->setStatusCode(500);
}
public function update($id)
{
$post = $this->request->getJSON(true);
$data = [
'name' => $post['name'] ?? '',
];
$success = $this->dictDeptModel->update($id, $data);
if ($success) {
return $this->response->setJSON([
'status' => 'success',
'message' => 'Department updated successfully'
]);
}
return $this->response->setJSON([
'status' => 'error',
'message' => 'Failed to update department'
])->setStatusCode(500);
}
public function delete($id)
{
$success = $this->dictDeptModel->delete($id);
if ($success) {
return $this->response->setJSON([
'status' => 'success',
'message' => 'Department deleted successfully'
]);
}
return $this->response->setJSON([
'status' => 'error',
'message' => 'Failed to delete department'
])->setStatusCode(500);
}
}