From 5fe6bd9629a06ea5a6ded66459c81302eeedcd46 Mon Sep 17 00:00:00 2001 From: Kavalar Date: Mon, 7 Oct 2024 11:31:16 +0300 Subject: [PATCH] rest api expand --- kernel/RestController.php | 10 ++++++++ .../post/controllers/PostRestController.php | 25 ++----------------- kernel/modules/post/models/Post.php | 3 +-- 3 files changed, 13 insertions(+), 25 deletions(-) diff --git a/kernel/RestController.php b/kernel/RestController.php index e13d436..5bb63b6 100644 --- a/kernel/RestController.php +++ b/kernel/RestController.php @@ -11,6 +11,12 @@ class RestController protected Model $model; + + protected function expand(): array + { + return []; + } + #[NoReturn] public function actionIndex(): void { $request = new Request(); @@ -29,7 +35,11 @@ class RestController #[NoReturn] public function actionView($id): void { + $expand = $this->expand(); $model = $this->model->where("id", $id)->first(); + if ($expand){ + $model->load($expand); + } $res = []; if ($model){ $res = $model->toArray(); diff --git a/kernel/modules/post/controllers/PostRestController.php b/kernel/modules/post/controllers/PostRestController.php index 6552daf..c640608 100644 --- a/kernel/modules/post/controllers/PostRestController.php +++ b/kernel/modules/post/controllers/PostRestController.php @@ -14,30 +14,9 @@ class PostRestController extends RestController $this->model = new Post(); } - #[NoReturn] public function actionView($id): void + protected function expand(): array { - $model = $this->model->where("id", $id)->first(); - $model->load("user"); - $res = []; - if ($model){ - $res = $model->toArray(); - } - - $this->renderApi($res); + return ["user"]; } - #[NoReturn] public function actionDelete($id): void - { - $model = $this->model->where("id", $id)->first(); - $model->load("user"); - $res = []; - if ($model){ - $res = $model->toArray(); - } - - $model->delete(); - - $this->renderApi($res); - - } } \ No newline at end of file diff --git a/kernel/modules/post/models/Post.php b/kernel/modules/post/models/Post.php index b3482d1..aa58ca2 100644 --- a/kernel/modules/post/models/Post.php +++ b/kernel/modules/post/models/Post.php @@ -9,7 +9,6 @@ use kernel\modules\user\models\User; * @property string $content * @property string $title * @property string $slug -// * @property string $username * @property int $user_id * @method static where(int[] $array) * @method static find($id) @@ -31,7 +30,7 @@ class Post extends Model ]; } - public function user() + public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(User::class); }