rest api expsnde add
This commit is contained in:
parent
c7e113e325
commit
a3f7a90e39
@ -23,12 +23,21 @@ class RestController
|
|||||||
$page = $request->get('page') ?? 1;
|
$page = $request->get('page') ?? 1;
|
||||||
$perPage = $request->get('per_page') ?? 10;
|
$perPage = $request->get('per_page') ?? 10;
|
||||||
$query = $this->model->query();
|
$query = $this->model->query();
|
||||||
|
|
||||||
if ($page > 1) {
|
if ($page > 1) {
|
||||||
$query->skip(($page - 1) * $perPage)->take($perPage);
|
$query->skip(($page - 1) * $perPage)->take($perPage);
|
||||||
} else {
|
} else {
|
||||||
$query->take($perPage);
|
$query->take($perPage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$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();
|
$res = $query->get()->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
$this->renderApi($res);
|
$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
|
#[NoReturn] protected function renderApi(array $data): void
|
||||||
{
|
{
|
||||||
header("Content-Type: application/json");
|
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('/', [\kernel\modules\post\controllers\PostRestController::class, 'actionIndex']);
|
||||||
App::$collector->get('/{id}', [\kernel\modules\post\controllers\PostRestController::class, 'actionView']);
|
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('/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->post("/", [\kernel\modules\post\controllers\PostController::class, 'actionAdd']);
|
||||||
// App::$collector->get('/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionView']);
|
// App::$collector->get('/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionView']);
|
||||||
// App::$collector->any('/update/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionUpdate']);
|
// App::$collector->any('/update/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionUpdate']);
|
||||||
|
Loading…
Reference in New Issue
Block a user