..
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, '校区删除成功');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,20 @@ use function Hyperf\Support\env;
|
||||
return [
|
||||
'mode' => SWOOLE_PROCESS,
|
||||
'servers' => [
|
||||
[
|
||||
'name' => 'http',
|
||||
'type' => Server::SERVER_HTTP,
|
||||
'host' => '0.0.0.0',
|
||||
'port' => 9501,
|
||||
'sock_type' => SWOOLE_SOCK_TCP,
|
||||
'callbacks' => [
|
||||
Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
|
||||
],
|
||||
'options' => [
|
||||
// Whether to enable request lifecycle event
|
||||
'enable_request_lifecycle' => false,
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'jsonrpc-http',
|
||||
'type' => Server::SERVER_HTTP,
|
||||
|
||||
@@ -1,18 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
use Hyperf\HttpServer\Router\Router;
|
||||
// use Hyperf\HttpServer\Router\Router;
|
||||
|
||||
Router::addRoute(['GET', 'POST', 'HEAD'], '/', 'App\Controller\IndexController@index');
|
||||
|
||||
Router::get('/favicon.ico', function () {
|
||||
return '';
|
||||
});
|
||||
// Router::addRoute(['GET', 'POST', 'HEAD'], '/', 'App\Controller\IndexController@index');
|
||||
//
|
||||
// Router::get('/favicon.ico', function () {
|
||||
// return '';
|
||||
// });
|
||||
|
||||
@@ -4,12 +4,16 @@ declare(strict_types=1);
|
||||
|
||||
namespace Aether;
|
||||
|
||||
use Hyperf\Contract\ContainerInterface;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use Hyperf\HttpServer\Contract\ResponseInterface;
|
||||
|
||||
abstract class AetherController
|
||||
{
|
||||
#[Inject]
|
||||
protected ContainerInterface $container;
|
||||
|
||||
#[Inject]
|
||||
protected RequestInterface $request;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user