rest api expsnde add
This commit is contained in:
parent
c7e113e325
commit
a3f7a90e39
@ -23,12 +23,21 @@ class RestController
|
||||
$page = $request->get('page') ?? 1;
|
||||
$perPage = $request->get('per_page') ?? 10;
|
||||
$query = $this->model->query();
|
||||
|
||||
if ($page > 1) {
|
||||
$query->skip(($page - 1) * $perPage)->take($perPage);
|
||||
} else {
|
||||
$query->take($perPage);
|
||||
}
|
||||
$res = $query->get()->toArray();
|
||||
|
||||
$expand = $this->expand();
|
||||
$expandParams = explode( ",", $request->get('expand') ?? "");
|
||||
$finalExpand = array_intersect($expandParams, $expand);
|
||||
if ($finalExpand) {
|
||||
$res = $query->get()->load($finalExpand)->toArray();
|
||||
} else {
|
||||
$res = $query->get()->toArray();
|
||||
}
|
||||
|
||||
$this->renderApi($res);
|
||||
}
|
||||
@ -65,6 +74,19 @@ class RestController
|
||||
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionCreate(): void
|
||||
{
|
||||
$request = new Request();
|
||||
$data = $request->post();
|
||||
$data = [
|
||||
'title' => 'title'
|
||||
];
|
||||
Debug::prn($data);
|
||||
$model = $this->model;
|
||||
Debug::prn($model->toArray());
|
||||
$model->save($data);
|
||||
}
|
||||
|
||||
#[NoReturn] protected function renderApi(array $data): void
|
||||
{
|
||||
header("Content-Type: application/json");
|
||||
|
@ -22,7 +22,7 @@ App::$collector->group(["prefix" => "api"], function (RouteCollector $router){
|
||||
App::$collector->get('/', [\kernel\modules\post\controllers\PostRestController::class, 'actionIndex']);
|
||||
App::$collector->get('/{id}', [\kernel\modules\post\controllers\PostRestController::class, 'actionView']);
|
||||
// App::$collector->get('/page/{page_number}', [\kernel\modules\post\controllers\PostController::class, 'actionIndex']);
|
||||
// App::$collector->get('/create', [\kernel\modules\post\controllers\PostController::class, 'actionCreate']);
|
||||
App::$collector->post('/create', [\kernel\modules\post\controllers\PostRestController::class, 'actionCreate']);
|
||||
// App::$collector->post("/", [\kernel\modules\post\controllers\PostController::class, 'actionAdd']);
|
||||
// App::$collector->get('/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionView']);
|
||||
// App::$collector->any('/update/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionUpdate']);
|
||||
|
Loading…
Reference in New Issue
Block a user