fix post crud

This commit is contained in:
Билай Станислав 2024-07-26 11:57:05 +03:00
parent 25e585655c
commit f5f546ba95
10 changed files with 42 additions and 38 deletions

View File

@ -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) {

View File

@ -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' => 'Обновлен'
];

View File

@ -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',
];
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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 " <a href='$link' class='btn btn-danger'>Удалить</a> ";
}

View File

@ -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();
});
}

View File

@ -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();
?>
<div class="row">

View File

@ -1,7 +1,7 @@
<?php
/**
* @var \Illuminate\Database\Eloquent\Collection $posts
* @var \Illuminate\Database\Eloquent\Collection $contents
*/
use app\models\Post;
@ -12,7 +12,7 @@ use Itguild\Tables\ListJsonTable;
use kernel\IGTabel\btn\PrimaryBtn;
use kernel\IGTabel\ListJsonTableEloquentCollection;
$dataProvider = new ListJsonTableEloquentCollection($posts, [
$dataProvider = new ListJsonTableEloquentCollection($contents, [
'model' => Post::class,
'perPage' => 5,
'params' => ["class" => "table table-bordered", "border" => "2"],

View File

@ -1,7 +1,7 @@
<?php
/**
* @var \Illuminate\Database\Eloquent\Collection $post
* @var \Illuminate\Database\Eloquent\Collection $content
*/
use Itguild\Tables\ViewJsonTable;
use kernel\IGTabel\btn\DangerBtn;
@ -9,15 +9,15 @@ use kernel\IGTabel\btn\PrimaryBtn;
use kernel\IGTabel\btn\SuccessBtn;
use kernel\IGTabel\ViewJsonTableEloquentModel;
$dataProvider = new ViewJsonTableEloquentModel($post, [
$dataProvider = new ViewJsonTableEloquentModel($content, [
'params' => ["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();