163 lines
4.1 KiB
PHP
163 lines
4.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller;
|
|
|
|
use Aether\AetherController;
|
|
use Aether\AetherResponse;
|
|
use App\Exception\BusinessException;
|
|
use App\Model\Teacher;
|
|
use App\Validator\TeacherValidator;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Hyperf\HttpServer\Contract\RequestInterface;
|
|
|
|
/**
|
|
* @Controller(prefix="/api/v1/teacher")
|
|
*/
|
|
class TeacherController extends AetherController
|
|
{
|
|
#[Inject]
|
|
protected TeacherValidator $validator;
|
|
|
|
/**
|
|
* 创建教师.
|
|
* @PostMapping(path="")
|
|
*/
|
|
public function create(RequestInterface $request): array
|
|
{
|
|
$data = $request->all();
|
|
$validator = $this->validator->validateCreate($data);
|
|
if ($validator->fails()) {
|
|
throw new BusinessException(400, $validator->errors()->first());
|
|
}
|
|
|
|
$teacher = new Teacher();
|
|
$teacher->fill($data);
|
|
$teacher->save();
|
|
|
|
return AetherResponse::success($teacher->toArray(), '教师创建成功');
|
|
}
|
|
|
|
/**
|
|
* 获取教师详情.
|
|
* @GetMapping(path="/{id}")
|
|
*/
|
|
public function get(int $id): array
|
|
{
|
|
$teacher = Teacher::find($id);
|
|
if (! $teacher) {
|
|
throw new BusinessException(10101);
|
|
}
|
|
|
|
return AetherResponse::success($teacher->toArray());
|
|
}
|
|
|
|
/**
|
|
* 更新教师信息.
|
|
* @PutMapping(path="/{id}")
|
|
*/
|
|
public function update(int $id, RequestInterface $request): array
|
|
{
|
|
$data = $request->all();
|
|
$data['id'] = $id;
|
|
|
|
$validator = $this->validator->validateUpdate($data);
|
|
if ($validator->fails()) {
|
|
throw new BusinessException(400, $validator->errors()->first());
|
|
}
|
|
|
|
$teacher = Teacher::find($id);
|
|
if (! $teacher) {
|
|
throw new BusinessException(10101);
|
|
}
|
|
|
|
$teacher->fill($data);
|
|
$teacher->save();
|
|
|
|
return AetherResponse::success($teacher->toArray(), '教师信息更新成功');
|
|
}
|
|
|
|
/**
|
|
* 删除教师.
|
|
* @DeleteMapping(path="/{id}")
|
|
*/
|
|
public function delete(int $id): array
|
|
{
|
|
$teacher = Teacher::find($id);
|
|
if (! $teacher) {
|
|
throw new BusinessException(10101);
|
|
}
|
|
|
|
$teacher->delete();
|
|
return AetherResponse::success(null, '教师删除成功');
|
|
}
|
|
|
|
/**
|
|
* 教师列表.
|
|
* @GetMapping(path="/list")
|
|
*/
|
|
public function list(RequestInterface $request): array
|
|
{
|
|
$data = $request->all();
|
|
$validator = $this->validator->validateQuery($data);
|
|
if ($validator->fails()) {
|
|
throw new BusinessException(400, $validator->errors()->first());
|
|
}
|
|
|
|
$query = Teacher::query();
|
|
|
|
if (! empty($data['id'])) {
|
|
$query->where('id', $data['id']);
|
|
}
|
|
if (! empty($data['name'])) {
|
|
$query->where('name', 'like', "%{$data['name']}%");
|
|
}
|
|
if (isset($data['campus_id'])) {
|
|
$query->where('campus_id', $data['campus_id']);
|
|
}
|
|
if (! empty($data['major_subject'])) {
|
|
$query->where('major_subject', 'like', "%{$data['major_subject']}%");
|
|
}
|
|
if (isset($data['status'])) {
|
|
$query->where('status', $data['status']);
|
|
}
|
|
|
|
$page = $data['page'] ?? 1;
|
|
$size = $data['size'] ?? 20;
|
|
|
|
$total = $query->count();
|
|
$list = $query->forPage($page, $size)->get()->toArray();
|
|
|
|
return AetherResponse::success([
|
|
'total' => $total,
|
|
'page' => $page,
|
|
'size' => $size,
|
|
'list' => $list,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 搜索教师.
|
|
* @GetMapping(path="/search")
|
|
*/
|
|
public function search(RequestInterface $request): array
|
|
{
|
|
$keyword = $request->input('keyword', '');
|
|
$page = (int) $request->input('page', 1);
|
|
$size = (int) $request->input('size', 20);
|
|
|
|
$query = Teacher::search($keyword)->enabled();
|
|
|
|
$total = $query->count();
|
|
$list = $query->forPage($page, $size)->get()->toArray();
|
|
|
|
return AetherResponse::success([
|
|
'total' => $total,
|
|
'page' => $page,
|
|
'size' => $size,
|
|
'list' => $list,
|
|
]);
|
|
}
|
|
}
|