diff --git a/kernel/RestController.php b/kernel/RestController.php index 0b090e4..fad2037 100644 --- a/kernel/RestController.php +++ b/kernel/RestController.php @@ -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"); diff --git a/kernel/modules/post/routs/post.php b/kernel/modules/post/routs/post.php index e99c834..f98082e 100644 --- a/kernel/modules/post/routs/post.php +++ b/kernel/modules/post/routs/post.php @@ -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']);