tinyqc/app/Controllers/Api/TestApiController.php

107 lines
3.1 KiB
PHP
Raw Normal View History

<?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;
protected $rules;
public function __construct()
{
$this->dictTestModel = new DictTestModel();
$this->dictDeptModel = new DictDeptModel();
$this->rules = [
'name' => 'required|min_length[1]',
'dept_ref_id' => 'required',
];
}
public function index()
{
try {
$rows = $this->dictTestModel->getWithDept();
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->dictTestModel->where('test_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->dictTestModel->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->dictTestModel->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->dictTestModel->delete($id);
return $this->respond([
'status' => 'success',
'message' => 'delete success'
]);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
}