- Add BaseModel with automatic camel/snake case conversion - Add stringcase_helper with camel_to_snake(), snake_to_camel() functions - Update all models to extend BaseModel for consistent data handling - Update API controllers with standardized JSON response format - Remove legacy v1 PHP application directory - Consolidate documentation into AGENTS.md, delete VIEWS_RULES.md
103 lines
2.9 KiB
PHP
103 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Api;
|
|
|
|
use App\Controllers\BaseController;
|
|
use App\Models\DictDeptModel;
|
|
|
|
class DeptApiController extends BaseController
|
|
{
|
|
protected $dictDeptModel;
|
|
protected $rules;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->dictDeptModel = new DictDeptModel();
|
|
$this->rules = [
|
|
'name' => 'required|min_length[1]',
|
|
];
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
try {
|
|
$rows = $this->dictDeptModel->findAll();
|
|
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->dictDeptModel->where('dept_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 create()
|
|
{
|
|
$input = $this->request->getJSON(true);
|
|
if (!$this->validate($this->rules)) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
try {
|
|
$id = $this->dictDeptModel->insert($input, true);
|
|
return $this->respondCreated([
|
|
'status' => 'success',
|
|
'message' => $id
|
|
]);
|
|
} 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->dictDeptModel->update($id, $input);
|
|
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->dictDeptModel->delete($id);
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'delete success'
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|