tinyqc/app/Controllers/Qc/TestCommentsController.php
mahdahar 87ff4c8d85 feat: Add user authentication system and secure all routes
- Implement AuthController with login/logout functionality
- Create UsersModel with bcrypt password hashing
- Add AuthFilter to protect all application routes
- Create login page with error handling
- Add users database migration with email/username fields
- Rename ResultComments to TestComments for consistency
- Update all routes to require authentication filter
- Enhance EntryApiController with comment deletion and better error handling
- Update seeder to include demo users and improved test data
- Fix BaseController to handle auth sessions properly
- Update entry views (daily/monthly) with new API endpoints
- Update layout with logout button and user info display
- Refactor control test index view for better organization
2026-02-09 11:12:12 +07:00

100 lines
2.9 KiB
PHP

<?php
namespace App\Controllers\Qc;
use CodeIgniter\API\ResponseTrait;
use App\Controllers\BaseController;
use App\Models\Qc\TestCommentsModel;
class TestCommentsController extends BaseController {
use ResponseTrait;
protected $model;
protected $rules;
public function __construct() {
$this->model = new TestCommentsModel();
$this->rules = [];
}
public function index() {
$keyword = $this->request->getGet('keyword');
try {
$rows = $this->model->search($keyword);
return $this->respond([
'status' => 'success',
'message' => 'fetch success',
'data' => $rows
], 200);
} catch (\Exception $e) {
return $this->failServerError($e->getMessage());
}
}
public function show($id = null) {
try {
$row = $this->model->find($id);
if (!$row) {
return $this->respond([
'status' => 'success',
'message' => 'data not found.'
], 200);
}
return $this->respond([
'status' => 'success',
'message' => 'fetch success',
'data' => [$row]
], 200);
} catch (\Exception $e) {
return $this->failServerError($e->getMessage());
}
}
public function create() {
$input = $this->request->getJSON(true);
$input = camel_to_snake_array($input);
if (!$this->validate($this->rules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
try {
$id = $this->model->insert($input, true);
return $this->respondCreated([
'status' => 'success',
'message' => $id
]);
} catch (\Exception $e) {
return $this->failServerError($e->getMessage());
}
}
public function update($id = null) {
$input = $this->request->getJSON(true);
$input = camel_to_snake_array($input);
if (!$this->validate($this->rules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
try {
$this->model->update($id, $input);
return $this->respond([
'status' => 'success',
'message' => 'update success',
'data' => [$id]
], 200);
} catch (\Exception $e) {
return $this->failServerError($e->getMessage());
}
}
public function delete($id = null) {
try {
$this->model->delete($id);
return $this->respond([
'status' => 'success',
'message' => 'delete success',
'data' => [$id]
], 200);
} catch (\Exception $e) {
return $this->failServerError($e->getMessage());
}
}
}