fix post crud
This commit is contained in:
parent
25e585655c
commit
f5f546ba95
@ -30,7 +30,7 @@ class PostController extends Controller
|
|||||||
$postForm = new CreatePostForm();
|
$postForm = new CreatePostForm();
|
||||||
$postService = new PostService();
|
$postService = new PostService();
|
||||||
$postForm->load($_REQUEST);
|
$postForm->load($_REQUEST);
|
||||||
if((new UserService)->check($_REQUEST['username'])) {
|
if((new UserService)->check($_REQUEST['user_id'])) {
|
||||||
if ($postForm->validate()) {
|
if ($postForm->validate()) {
|
||||||
$post = $postService->create($postForm);
|
$post = $postService->create($postForm);
|
||||||
if ($post) {
|
if ($post) {
|
||||||
@ -47,9 +47,9 @@ class PostController extends Controller
|
|||||||
|
|
||||||
public function actionIndex(): void
|
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
|
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");
|
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]);
|
$this->cgView->render("post/form.php", ['model' => $model]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
public function actionEdit($id): void
|
public function actionEdit($id): void
|
||||||
{
|
{
|
||||||
$post = Post::find($id);
|
$post = Post::find($id);
|
||||||
if (!$post){
|
if (!$post){
|
||||||
throw new Exception(message: "The user not found");
|
throw new Exception(message: "The post not found");
|
||||||
}
|
}
|
||||||
$postForm = new CreatePostForm();
|
$postForm = new CreatePostForm();
|
||||||
$postService = new PostService();
|
$postService = new PostService();
|
||||||
$postForm->load($_REQUEST);
|
$postForm->load($_REQUEST);
|
||||||
if((new UserService)->check($_REQUEST['username'])) {
|
if((new UserService)->check($_REQUEST['user_id'])) {
|
||||||
if ($postForm->validate()) {
|
if ($postForm->validate()) {
|
||||||
$post = $postService->update($postForm, $post);
|
$post = $postService->update($postForm, $post);
|
||||||
if ($post) {
|
if ($post) {
|
||||||
|
@ -5,21 +5,22 @@ use \Illuminate\Database\Eloquent\Model;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @property int $id
|
* @property int $id
|
||||||
* @property string $post
|
* @property string $content
|
||||||
* @property string $username
|
// * @property string $username
|
||||||
|
* @property int $user_id
|
||||||
* @method static where(int[] $array)
|
* @method static where(int[] $array)
|
||||||
* @method static find($id)
|
* @method static find($id)
|
||||||
*/
|
*/
|
||||||
class Post extends Model
|
class Post extends Model
|
||||||
{
|
{
|
||||||
protected $table = 'post';
|
protected $table = 'post';
|
||||||
protected $fillable = ['post', 'username'];
|
protected $fillable = ['content', 'user_id'];
|
||||||
|
|
||||||
public static function labels(): array
|
public static function labels(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'post' => 'Пост',
|
'content' => 'Контент',
|
||||||
'username' => 'Пользователь',
|
'user_id' => 'Id пользователя',
|
||||||
'created_at' => 'Создан',
|
'created_at' => 'Создан',
|
||||||
'updated_at' => 'Обновлен'
|
'updated_at' => 'Обновлен'
|
||||||
];
|
];
|
||||||
|
@ -9,8 +9,8 @@ class CreatePostForm extends FormModel
|
|||||||
public function rules(): array
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'post' => 'required|min-str-len:1|max-str-len:2048',
|
'content' => 'required|min-str-len:1|max-str-len:2048',
|
||||||
'username' => 'required|min-str-len:1|max-str-len:50',
|
'user_id' => 'required|min-str-len:1',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -10,8 +10,8 @@ class PostService
|
|||||||
public function create(FormModel $form_model): false|Post
|
public function create(FormModel $form_model): false|Post
|
||||||
{
|
{
|
||||||
$model = new Post();
|
$model = new Post();
|
||||||
$model->post = $form_model->getItem('post');
|
$model->content = $form_model->getItem('content');
|
||||||
$model->username = $form_model->getItem('username');
|
$model->user_id = $form_model->getItem('user_id');
|
||||||
if ($model->save()){
|
if ($model->save()){
|
||||||
return $model;
|
return $model;
|
||||||
}
|
}
|
||||||
@ -21,8 +21,8 @@ class PostService
|
|||||||
|
|
||||||
public function update(FormModel $form_model, Post $post): false|Post
|
public function update(FormModel $form_model, Post $post): false|Post
|
||||||
{
|
{
|
||||||
$post->post = $form_model->getItem('post');
|
$post->content = $form_model->getItem('content');
|
||||||
$post->username = $form_model->getItem('username');
|
$post->user_id = $form_model->getItem('user_id');
|
||||||
if ($post->save()){
|
if ($post->save()){
|
||||||
return $post;
|
return $post;
|
||||||
}
|
}
|
||||||
|
@ -34,9 +34,9 @@ class UserService
|
|||||||
return false;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace app\tables\columns;
|
namespace app\tables\columns;
|
||||||
|
|
||||||
|
use app\helpers\Debug;
|
||||||
use Itguild\Tables\ActionColumn\ActionColumn;
|
use Itguild\Tables\ActionColumn\ActionColumn;
|
||||||
|
|
||||||
class UserDeleteActionColumn extends ActionColumn
|
class UserDeleteActionColumn extends ActionColumn
|
||||||
@ -10,7 +11,6 @@ class UserDeleteActionColumn extends ActionColumn
|
|||||||
|
|
||||||
public function fetch(): string
|
public function fetch(): string
|
||||||
{
|
{
|
||||||
// $link = $this->baseUrl . $this->prefix . $this->id . $this->prefix . "update";
|
|
||||||
$link = $this->baseUrl . $this->prefix . "delete" . $this->prefix . $this->id;
|
$link = $this->baseUrl . $this->prefix . "delete" . $this->prefix . $this->id;
|
||||||
return " <a href='$link' class='btn btn-danger'>Удалить</a> ";
|
return " <a href='$link' class='btn btn-danger'>Удалить</a> ";
|
||||||
}
|
}
|
||||||
|
@ -18,8 +18,8 @@ class PostMigration extends Migration
|
|||||||
{
|
{
|
||||||
Manager::schema()->create('post', function (Blueprint $table) {
|
Manager::schema()->create('post', function (Blueprint $table) {
|
||||||
$table->increments('id');
|
$table->increments('id');
|
||||||
$table->string('post', 3000)->nullable(false);
|
$table->string('content', 3000)->nullable(false);
|
||||||
$table->string('username', 255)->nullable(false);
|
$table->integer('user_id');
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -8,21 +8,21 @@ use app\models\Post;
|
|||||||
$form = new \itguild\forms\ActiveForm();
|
$form = new \itguild\forms\ActiveForm();
|
||||||
$form->beginForm(isset($model) ? "/admin/post/edit/" . $model->id : "/admin/post");
|
$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",
|
'class' => "form-control",
|
||||||
'placeholder' => 'Пост',
|
'placeholder' => 'Контент',
|
||||||
'rows' => '10',
|
'rows' => '10',
|
||||||
'value' => $model->post ?? ''
|
'value' => $model->content ?? ''
|
||||||
])
|
])
|
||||||
->setLabel("Пост")
|
->setLabel("Пост")
|
||||||
->render();
|
->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",
|
'class' => "form-control",
|
||||||
'placeholder' => 'Логин',
|
'placeholder' => 'id',
|
||||||
'value' => $model->username ?? ''
|
'value' => $model->user_id ?? ''
|
||||||
])
|
])
|
||||||
->setLabel("Имя пользователя")
|
->setLabel("id пользователя")
|
||||||
->render();
|
->render();
|
||||||
?>
|
?>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Illuminate\Database\Eloquent\Collection $posts
|
* @var \Illuminate\Database\Eloquent\Collection $contents
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use app\models\Post;
|
use app\models\Post;
|
||||||
@ -12,7 +12,7 @@ use Itguild\Tables\ListJsonTable;
|
|||||||
use kernel\IGTabel\btn\PrimaryBtn;
|
use kernel\IGTabel\btn\PrimaryBtn;
|
||||||
use kernel\IGTabel\ListJsonTableEloquentCollection;
|
use kernel\IGTabel\ListJsonTableEloquentCollection;
|
||||||
|
|
||||||
$dataProvider = new ListJsonTableEloquentCollection($posts, [
|
$dataProvider = new ListJsonTableEloquentCollection($contents, [
|
||||||
'model' => Post::class,
|
'model' => Post::class,
|
||||||
'perPage' => 5,
|
'perPage' => 5,
|
||||||
'params' => ["class" => "table table-bordered", "border" => "2"],
|
'params' => ["class" => "table table-bordered", "border" => "2"],
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Illuminate\Database\Eloquent\Collection $post
|
* @var \Illuminate\Database\Eloquent\Collection $content
|
||||||
*/
|
*/
|
||||||
use Itguild\Tables\ViewJsonTable;
|
use Itguild\Tables\ViewJsonTable;
|
||||||
use kernel\IGTabel\btn\DangerBtn;
|
use kernel\IGTabel\btn\DangerBtn;
|
||||||
@ -9,15 +9,15 @@ use kernel\IGTabel\btn\PrimaryBtn;
|
|||||||
use kernel\IGTabel\btn\SuccessBtn;
|
use kernel\IGTabel\btn\SuccessBtn;
|
||||||
use kernel\IGTabel\ViewJsonTableEloquentModel;
|
use kernel\IGTabel\ViewJsonTableEloquentModel;
|
||||||
|
|
||||||
$dataProvider = new ViewJsonTableEloquentModel($post, [
|
$dataProvider = new ViewJsonTableEloquentModel($content, [
|
||||||
'params' => ["class" => "table table-bordered", "border" => "2"],
|
'params' => ["class" => "table table-bordered", "border" => "2"],
|
||||||
'baseUrl' => "/admin/user",
|
'baseUrl' => "/admin/user",
|
||||||
]);
|
]);
|
||||||
$table = new ViewJsonTable($dataProvider->getJson());
|
$table = new ViewJsonTable($dataProvider->getJson());
|
||||||
$table->beforeTable(function () use ($post) {
|
$table->beforeTable(function () use ($content) {
|
||||||
$btn = PrimaryBtn::create("Список", "/admin/post")->fetch();
|
$btn = PrimaryBtn::create("Список", "/admin/post")->fetch();
|
||||||
$btn .= SuccessBtn::create("Редактировать", "/admin/post/update/" . $post->id)->fetch();
|
$btn .= SuccessBtn::create("Редактировать", "/admin/post/update/" . $content->id)->fetch();
|
||||||
$btn .= DangerBtn::create("Удалить", "/admin/post/delete/" . $post->id)->fetch();
|
$btn .= DangerBtn::create("Удалить", "/admin/post/delete/" . $content->id)->fetch();
|
||||||
return $btn;
|
return $btn;
|
||||||
});
|
});
|
||||||
$table->create();
|
$table->create();
|
||||||
|
Loading…
Reference in New Issue
Block a user