..
This commit is contained in:
@@ -12,94 +12,22 @@ use App\Validator\CampusValidator;
|
||||
use Exception;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\DeleteMapping;
|
||||
use Hyperf\HttpServer\Annotation\GetMapping;
|
||||
use Hyperf\HttpServer\Annotation\PostMapping;
|
||||
use Hyperf\HttpServer\Annotation\PutMapping;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
|
||||
#[Controller(
|
||||
prefix: '/api/v1/campus',
|
||||
)]
|
||||
#[Controller(prefix: '/api/v1/campus')]
|
||||
class CampusController extends AetherController
|
||||
{
|
||||
#[Inject]
|
||||
protected CampusValidator $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());
|
||||
}
|
||||
|
||||
$campus = new Campus();
|
||||
$campus->fill($data);
|
||||
$campus->save();
|
||||
|
||||
return AetherResponse::success($campus->toArray(), '校区创建成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取校区详情.
|
||||
* @GetMapping(path="/{id}")
|
||||
*/
|
||||
public function get(int $id): array
|
||||
{
|
||||
$campus = Campus::find($id);
|
||||
if (! $campus) {
|
||||
throw new BusinessException(10001);
|
||||
}
|
||||
|
||||
return AetherResponse::success($campus->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());
|
||||
}
|
||||
|
||||
$campus = Campus::find($id);
|
||||
if (! $campus) {
|
||||
throw new BusinessException(10001);
|
||||
}
|
||||
|
||||
$campus->fill($data);
|
||||
$campus->save();
|
||||
|
||||
return AetherResponse::success($campus->toArray(), '校区更新成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除校区.
|
||||
* @DeleteMapping(path="/{id}")
|
||||
* @throws Exception
|
||||
*/
|
||||
public function delete(int $id): array
|
||||
{
|
||||
$campus = Campus::find($id);
|
||||
if (! $campus) {
|
||||
throw new BusinessException(10001);
|
||||
}
|
||||
|
||||
$campus->delete();
|
||||
return AetherResponse::success(null, '校区删除成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 校区列表.
|
||||
* @GetMapping(path="/list")
|
||||
*/
|
||||
#[GetMapping(path: '/list')]
|
||||
public function list(RequestInterface $request): array
|
||||
{
|
||||
$data = $request->all();
|
||||
@@ -148,8 +76,8 @@ class CampusController extends AetherController
|
||||
|
||||
/**
|
||||
* 获取省份列表.
|
||||
* @GetMapping(path="/provinces")
|
||||
*/
|
||||
#[GetMapping(path: '/provinces')]
|
||||
public function provinces(): array
|
||||
{
|
||||
$provinces = Campus::level(1)
|
||||
@@ -163,8 +91,8 @@ class CampusController extends AetherController
|
||||
|
||||
/**
|
||||
* 获取城市列表.
|
||||
* @GetMapping(path="/cities")
|
||||
*/
|
||||
#[GetMapping(path: '/cities')]
|
||||
public function cities(RequestInterface $request): array
|
||||
{
|
||||
$province = $request->input('province');
|
||||
@@ -181,4 +109,78 @@ class CampusController extends AetherController
|
||||
|
||||
return AetherResponse::success($cities);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建校区.
|
||||
*/
|
||||
#[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());
|
||||
}
|
||||
|
||||
$campus = new Campus();
|
||||
$campus->fill($data);
|
||||
$campus->save();
|
||||
|
||||
return AetherResponse::success($campus->toArray(), '校区创建成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取校区详情.
|
||||
*/
|
||||
#[GetMapping(path: '/{id}')]
|
||||
public function get(int $id): array
|
||||
{
|
||||
$campus = Campus::find($id);
|
||||
if (! $campus) {
|
||||
throw new BusinessException(10001);
|
||||
}
|
||||
|
||||
return AetherResponse::success($campus->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());
|
||||
}
|
||||
|
||||
$campus = Campus::find($id);
|
||||
if (! $campus) {
|
||||
throw new BusinessException(10001);
|
||||
}
|
||||
|
||||
$campus->fill($data);
|
||||
$campus->save();
|
||||
|
||||
return AetherResponse::success($campus->toArray(), '校区更新成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除校区.
|
||||
* @throws Exception
|
||||
*/
|
||||
#[DeleteMapping(path: '/{id}')]
|
||||
public function delete(int $id): array
|
||||
{
|
||||
$campus = Campus::find($id);
|
||||
if (! $campus) {
|
||||
throw new BusinessException(10001);
|
||||
}
|
||||
|
||||
$campus->delete();
|
||||
return AetherResponse::success(null, '校区删除成功');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user