This commit is contained in:
Aether
2025-09-23 15:29:55 +08:00
parent 0202f630bd
commit 485f59eac3
4 changed files with 106 additions and 94 deletions

View File

@@ -12,94 +12,22 @@ use App\Validator\CampusValidator;
use Exception; use Exception;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller; 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; use Hyperf\HttpServer\Contract\RequestInterface;
#[Controller( #[Controller(prefix: '/api/v1/campus')]
prefix: '/api/v1/campus',
)]
class CampusController extends AetherController class CampusController extends AetherController
{ {
#[Inject] #[Inject]
protected CampusValidator $validator; 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 public function list(RequestInterface $request): array
{ {
$data = $request->all(); $data = $request->all();
@@ -148,8 +76,8 @@ class CampusController extends AetherController
/** /**
* 获取省份列表. * 获取省份列表.
* @GetMapping(path="/provinces")
*/ */
#[GetMapping(path: '/provinces')]
public function provinces(): array public function provinces(): array
{ {
$provinces = Campus::level(1) $provinces = Campus::level(1)
@@ -163,8 +91,8 @@ class CampusController extends AetherController
/** /**
* 获取城市列表. * 获取城市列表.
* @GetMapping(path="/cities")
*/ */
#[GetMapping(path: '/cities')]
public function cities(RequestInterface $request): array public function cities(RequestInterface $request): array
{ {
$province = $request->input('province'); $province = $request->input('province');
@@ -181,4 +109,78 @@ class CampusController extends AetherController
return AetherResponse::success($cities); 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, '校区删除成功');
}
} }

View File

@@ -15,6 +15,20 @@ use function Hyperf\Support\env;
return [ return [
'mode' => SWOOLE_PROCESS, 'mode' => SWOOLE_PROCESS,
'servers' => [ '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', 'name' => 'jsonrpc-http',
'type' => Server::SERVER_HTTP, 'type' => Server::SERVER_HTTP,

View File

@@ -1,18 +1,10 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
/** // use Hyperf\HttpServer\Router\Router;
* 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;
Router::addRoute(['GET', 'POST', 'HEAD'], '/', 'App\Controller\IndexController@index'); // Router::addRoute(['GET', 'POST', 'HEAD'], '/', 'App\Controller\IndexController@index');
//
Router::get('/favicon.ico', function () { // Router::get('/favicon.ico', function () {
return ''; // return '';
}); // });

View File

@@ -4,12 +4,16 @@ declare(strict_types=1);
namespace Aether; namespace Aether;
use Hyperf\Contract\ContainerInterface;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface; use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface; use Hyperf\HttpServer\Contract\ResponseInterface;
abstract class AetherController abstract class AetherController
{ {
#[Inject]
protected ContainerInterface $container;
#[Inject] #[Inject]
protected RequestInterface $request; protected RequestInterface $request;