79 lines
1.8 KiB
PHP
79 lines
1.8 KiB
PHP
<?php
|
|
|
|
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;
|
|
|
|
#[Inject]
|
|
protected ResponseInterface $response;
|
|
|
|
|
|
/**
|
|
* 获取资源列表 (RESTFul: GET /resources)
|
|
*/
|
|
public function index(): array
|
|
{
|
|
$params = $this->request->all();
|
|
$result = $this->getService()->list($params);
|
|
return AetherResponse::success($result);
|
|
}
|
|
|
|
/**
|
|
* 获取单个资源 (RESTFul: GET /resources/{id})
|
|
*/
|
|
public function show(int $id): array
|
|
{
|
|
$result = $this->getService()->detail($id);
|
|
return AetherResponse::success($result);
|
|
}
|
|
|
|
/**
|
|
* 创建资源 (RESTFul: POST /resources)
|
|
*/
|
|
public function store()
|
|
{
|
|
$data = $this->request->all();
|
|
$id = $this->getService()->create($data);
|
|
return AetherResponse::success(['id' => $id], '创建成功');
|
|
}
|
|
|
|
/**
|
|
* 更新资源 (RESTFul: PUT /resources/{id})
|
|
*/
|
|
public function update(int $id): array
|
|
{
|
|
$data = $this->request->all();
|
|
$this->getService()->update($id, $data);
|
|
return AetherResponse::success(null, '更新成功');
|
|
}
|
|
|
|
/**
|
|
* 删除资源 (RESTFul: DELETE /resources/{id})
|
|
*/
|
|
public function destroy(int $id): array
|
|
{
|
|
$this->getService()->delete($id);
|
|
return AetherResponse::success(null, '删除成功');
|
|
}
|
|
|
|
/**
|
|
* 获取对应的服务类
|
|
* @return \Aether\AbstractService
|
|
*/
|
|
abstract protected function getService();
|
|
|
|
}
|