From f5f546ba95de333cc48b274217f7355feec261a8 Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Fri, 26 Jul 2024 11:57:05 +0300 Subject: [PATCH] fix post crud --- app/controllers/PostController.php | 19 +++++++++++-------- app/models/Post.php | 11 ++++++----- app/models/forms/CreatePostForm.php | 4 ++-- app/services/PostService.php | 8 ++++---- app/services/UserService.php | 4 ++-- app/tables/columns/UserDeleteActionColumn.php | 2 +- migrations/PostMigration.php | 4 ++-- views/admin/post/form.php | 14 +++++++------- views/admin/post/index.php | 4 ++-- views/admin/post/view.php | 10 +++++----- 10 files changed, 42 insertions(+), 38 deletions(-) diff --git a/app/controllers/PostController.php b/app/controllers/PostController.php index 729e7a7..1b903e6 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['username'])) { + if((new UserService)->check($_REQUEST['user_id'])) { if ($postForm->validate()) { $post = $postService->create($postForm); if ($post) { @@ -47,9 +47,9 @@ class PostController extends Controller public function actionIndex(): void { - $posts = Post::all(); + $contents = Post::all(); - $this->cgView->render("post/index.php", ['posts' => $posts]); + $this->cgView->render("post/index.php", ['contents' => $contents]); } /** @@ -57,12 +57,12 @@ class PostController extends Controller */ public function actionView($id): void { - $post = Post::find($id); + $content = Post::find($id); - if (!$post){ + if (!$content){ throw new Exception(message: "The post not found"); } - $this->cgView->render("post/view.php", ['post' => $post]); + $this->cgView->render("post/view.php", ['content' => $content]); } /** @@ -78,16 +78,19 @@ class PostController extends Controller $this->cgView->render("post/form.php", ['model' => $model]); } + /** + * @throws Exception + */ public function actionEdit($id): void { $post = Post::find($id); if (!$post){ - throw new Exception(message: "The user not found"); + throw new Exception(message: "The post not found"); } $postForm = new CreatePostForm(); $postService = new PostService(); $postForm->load($_REQUEST); - if((new UserService)->check($_REQUEST['username'])) { + if((new UserService)->check($_REQUEST['user_id'])) { if ($postForm->validate()) { $post = $postService->update($postForm, $post); if ($post) { diff --git a/app/models/Post.php b/app/models/Post.php index b8aaa3a..175edc7 100644 --- a/app/models/Post.php +++ b/app/models/Post.php @@ -5,21 +5,22 @@ use \Illuminate\Database\Eloquent\Model; /** * @property int $id - * @property string $post - * @property string $username + * @property string $content +// * @property string $username + * @property int $user_id * @method static where(int[] $array) * @method static find($id) */ class Post extends Model { protected $table = 'post'; - protected $fillable = ['post', 'username']; + protected $fillable = ['content', 'user_id']; public static function labels(): array { return [ - 'post' => 'Пост', - 'username' => 'Пользователь', + 'content' => 'Контент', + 'user_id' => 'Id пользователя', 'created_at' => 'Создан', 'updated_at' => 'Обновлен' ]; diff --git a/app/models/forms/CreatePostForm.php b/app/models/forms/CreatePostForm.php index e07d4fe..3e5fbca 100644 --- a/app/models/forms/CreatePostForm.php +++ b/app/models/forms/CreatePostForm.php @@ -9,8 +9,8 @@ class CreatePostForm extends FormModel public function rules(): array { return [ - 'post' => 'required|min-str-len:1|max-str-len:2048', - 'username' => 'required|min-str-len:1|max-str-len:50', + 'content' => 'required|min-str-len:1|max-str-len:2048', + 'user_id' => 'required|min-str-len:1', ]; } } \ No newline at end of file diff --git a/app/services/PostService.php b/app/services/PostService.php index 92f4a92..b69eb6d 100644 --- a/app/services/PostService.php +++ b/app/services/PostService.php @@ -10,8 +10,8 @@ class PostService public function create(FormModel $form_model): false|Post { $model = new Post(); - $model->post = $form_model->getItem('post'); - $model->username = $form_model->getItem('username'); + $model->content = $form_model->getItem('content'); + $model->user_id = $form_model->getItem('user_id'); if ($model->save()){ return $model; } @@ -21,8 +21,8 @@ class PostService public function update(FormModel $form_model, Post $post): false|Post { - $post->post = $form_model->getItem('post'); - $post->username = $form_model->getItem('username'); + $post->content = $form_model->getItem('content'); + $post->user_id = $form_model->getItem('user_id'); if ($post->save()){ return $post; } diff --git a/app/services/UserService.php b/app/services/UserService.php index ce9a901..854aaa5 100644 --- a/app/services/UserService.php +++ b/app/services/UserService.php @@ -34,9 +34,9 @@ class UserService return false; } - public function check(string $username): bool + public function check(int $user_id): bool { - if (User::where(['username' => $username])->first()) + if (User::where(['id' => $user_id])->first()) { return true; } diff --git a/app/tables/columns/UserDeleteActionColumn.php b/app/tables/columns/UserDeleteActionColumn.php index cf17acb..969ec40 100644 --- a/app/tables/columns/UserDeleteActionColumn.php +++ b/app/tables/columns/UserDeleteActionColumn.php @@ -2,6 +2,7 @@ namespace app\tables\columns; +use app\helpers\Debug; use Itguild\Tables\ActionColumn\ActionColumn; class UserDeleteActionColumn extends ActionColumn @@ -10,7 +11,6 @@ class UserDeleteActionColumn extends ActionColumn public function fetch(): string { -// $link = $this->baseUrl . $this->prefix . $this->id . $this->prefix . "update"; $link = $this->baseUrl . $this->prefix . "delete" . $this->prefix . $this->id; return " Удалить "; } diff --git a/migrations/PostMigration.php b/migrations/PostMigration.php index 3924ab8..a8e5df9 100644 --- a/migrations/PostMigration.php +++ b/migrations/PostMigration.php @@ -18,8 +18,8 @@ class PostMigration extends Migration { Manager::schema()->create('post', function (Blueprint $table) { $table->increments('id'); - $table->string('post', 3000)->nullable(false); - $table->string('username', 255)->nullable(false); + $table->string('content', 3000)->nullable(false); + $table->integer('user_id'); $table->timestamps(); }); } diff --git a/views/admin/post/form.php b/views/admin/post/form.php index 8226ef3..0eb591d 100644 --- a/views/admin/post/form.php +++ b/views/admin/post/form.php @@ -8,21 +8,21 @@ use app\models\Post; $form = new \itguild\forms\ActiveForm(); $form->beginForm(isset($model) ? "/admin/post/edit/" . $model->id : "/admin/post"); -$form->field(class: \itguild\forms\inputs\TextArea::class, name: "post", params: [ +$form->field(class: \itguild\forms\inputs\TextArea::class, name: "content", params: [ 'class' => "form-control", - 'placeholder' => 'Пост', + 'placeholder' => 'Контент', 'rows' => '10', - 'value' => $model->post ?? '' + 'value' => $model->content ?? '' ]) ->setLabel("Пост") ->render(); -$form->field(class: \itguild\forms\inputs\TextInput::class, name: "username", params: [ +$form->field(class: \itguild\forms\inputs\TextInput::class, name: "user_id", params: [ 'class' => "form-control", - 'placeholder' => 'Логин', - 'value' => $model->username ?? '' + 'placeholder' => 'id', + 'value' => $model->user_id ?? '' ]) - ->setLabel("Имя пользователя") + ->setLabel("id пользователя") ->render(); ?>
diff --git a/views/admin/post/index.php b/views/admin/post/index.php index fed1ced..b5802fd 100644 --- a/views/admin/post/index.php +++ b/views/admin/post/index.php @@ -1,7 +1,7 @@ Post::class, 'perPage' => 5, 'params' => ["class" => "table table-bordered", "border" => "2"], diff --git a/views/admin/post/view.php b/views/admin/post/view.php index a7dcbc5..110b2bd 100644 --- a/views/admin/post/view.php +++ b/views/admin/post/view.php @@ -1,7 +1,7 @@ ["class" => "table table-bordered", "border" => "2"], 'baseUrl' => "/admin/user", ]); $table = new ViewJsonTable($dataProvider->getJson()); -$table->beforeTable(function () use ($post) { +$table->beforeTable(function () use ($content) { $btn = PrimaryBtn::create("Список", "/admin/post")->fetch(); - $btn .= SuccessBtn::create("Редактировать", "/admin/post/update/" . $post->id)->fetch(); - $btn .= DangerBtn::create("Удалить", "/admin/post/delete/" . $post->id)->fetch(); + $btn .= SuccessBtn::create("Редактировать", "/admin/post/update/" . $content->id)->fetch(); + $btn .= DangerBtn::create("Удалить", "/admin/post/delete/" . $content->id)->fetch(); return $btn; }); $table->create();