From 2d8f945cada909894a5987672251c45363d9f0b7 Mon Sep 17 00:00:00 2001 From: Kavalar Date: Fri, 26 Jul 2024 12:42:44 +0300 Subject: [PATCH] post crud fix --- app/controllers/PostController.php | 2 +- app/models/forms/CreatePostForm.php | 2 +- app/services/UserService.php | 3 ++- app/tables/columns/PostEditActionColumn.php | 16 ++++++++++++++++ app/tables/columns/UserEditActionColumn.php | 2 +- kernel/IGTabel/JSONCreator.php | 5 ++--- views/admin/post/index.php | 3 ++- 7 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 app/tables/columns/PostEditActionColumn.php diff --git a/app/controllers/PostController.php b/app/controllers/PostController.php index 1b903e6..5837fdc 100644 --- a/app/controllers/PostController.php +++ b/app/controllers/PostController.php @@ -30,7 +30,7 @@ class PostController extends Controller $postForm = new CreatePostForm(); $postService = new PostService(); $postForm->load($_REQUEST); - if((new UserService)->check($_REQUEST['user_id'])) { + if(UserService::check($_REQUEST['user_id'])) { if ($postForm->validate()) { $post = $postService->create($postForm); if ($post) { diff --git a/app/models/forms/CreatePostForm.php b/app/models/forms/CreatePostForm.php index 3e5fbca..8eb195f 100644 --- a/app/models/forms/CreatePostForm.php +++ b/app/models/forms/CreatePostForm.php @@ -9,7 +9,7 @@ class CreatePostForm extends FormModel public function rules(): array { return [ - 'content' => 'required|min-str-len:1|max-str-len:2048', + 'content' => 'required|min-str-len:1', 'user_id' => 'required|min-str-len:1', ]; } diff --git a/app/services/UserService.php b/app/services/UserService.php index 854aaa5..0f0142e 100644 --- a/app/services/UserService.php +++ b/app/services/UserService.php @@ -34,8 +34,9 @@ class UserService return false; } - public function check(int $user_id): bool + public static function check(int $user_id): bool { + if (User::where(['id' => $user_id])->first()) { return true; diff --git a/app/tables/columns/PostEditActionColumn.php b/app/tables/columns/PostEditActionColumn.php new file mode 100644 index 0000000..264cc34 --- /dev/null +++ b/app/tables/columns/PostEditActionColumn.php @@ -0,0 +1,16 @@ +baseUrl . $this->prefix . $this->id; + return " Редактировать "; + } +} \ No newline at end of file diff --git a/app/tables/columns/UserEditActionColumn.php b/app/tables/columns/UserEditActionColumn.php index f0db24d..84ce168 100644 --- a/app/tables/columns/UserEditActionColumn.php +++ b/app/tables/columns/UserEditActionColumn.php @@ -11,7 +11,7 @@ class UserEditActionColumn extends ActionColumn public function fetch(): string { // $link = $this->baseUrl . $this->prefix . $this->id . $this->prefix . "update"; - $link = $this->baseUrl . $this->prefix . "update" . $this->prefix . $this->id; + $link = $this->baseUrl . $this->prefix . $this->id; return " Редактировать "; } } \ No newline at end of file diff --git a/kernel/IGTabel/JSONCreator.php b/kernel/IGTabel/JSONCreator.php index 4bcd5fe..4d96b6c 100644 --- a/kernel/IGTabel/JSONCreator.php +++ b/kernel/IGTabel/JSONCreator.php @@ -10,9 +10,8 @@ class JSONCreator public function __construct(array $meta, array $data) { - $params = empty($meta['params']) ? ["class" => "table table-bordered", "border" => "1"] : $meta['params']; - if ($meta && $data) { + if ($meta) { $this->informationArray = [ "meta" => [ "columns" => $meta['columns'] ?? [], @@ -24,7 +23,7 @@ class JSONCreator "params" => $params ], - "data" => $data + "data" => $data ?? [] ]; } } diff --git a/views/admin/post/index.php b/views/admin/post/index.php index b5802fd..58409e2 100644 --- a/views/admin/post/index.php +++ b/views/admin/post/index.php @@ -18,13 +18,14 @@ $dataProvider = new ListJsonTableEloquentCollection($contents, [ 'params' => ["class" => "table table-bordered", "border" => "2"], 'baseUrl' => "/admin/post", ]); + $table = new ListJsonTable($dataProvider->getJson()); $table->beforePrint(function (){ return PrimaryBtn::create("Создать", "/admin/post/create")->fetch(); //return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch(); }); $table->addAction(UserViewActionColumn::class); -$table->addAction(UserEditActionColumn::class); +$table->addAction(\app\tables\columns\PostEditActionColumn::class); $table->addAction(UserDeleteActionColumn::class); $table->create(); $table->render(); \ No newline at end of file