api action delete add, slug generator update

This commit is contained in:
Билай Станислав 2024-09-26 16:49:01 +03:00
parent d49416a7d2
commit 7ff9d45fe7
4 changed files with 54 additions and 3 deletions

View File

@ -38,6 +38,20 @@ class RestController
$this->renderApi($res); $this->renderApi($res);
} }
#[NoReturn] public function actionDelete($id): void
{
$model = $this->model->where("id", $id)->first();
$res = [];
if ($model){
$res = $model->toArray();
}
$model->delete();
$this->renderApi($res);
}
#[NoReturn] protected function renderApi(array $data): void #[NoReturn] protected function renderApi(array $data): void
{ {
header("Content-Type: application/json"); header("Content-Type: application/json");

View File

@ -3,6 +3,7 @@
namespace kernel\modules\post\controllers; namespace kernel\modules\post\controllers;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use JetBrains\PhpStorm\NoReturn;
use kernel\modules\post\models\Post; use kernel\modules\post\models\Post;
use kernel\RestController; use kernel\RestController;
@ -13,7 +14,7 @@ class PostRestController extends RestController
$this->model = new Post(); $this->model = new Post();
} }
public function actionView($id): void #[NoReturn] public function actionView($id): void
{ {
$model = $this->model->where("id", $id)->first(); $model = $this->model->where("id", $id)->first();
$model->load("user"); $model->load("user");
@ -24,4 +25,19 @@ class PostRestController extends RestController
$this->renderApi($res); $this->renderApi($res);
} }
#[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);
}
} }

View File

@ -27,6 +27,6 @@ App::$collector->group(["prefix" => "api"], function (RouteCollector $router){
// App::$collector->get('/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionView']); // App::$collector->get('/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionView']);
// App::$collector->any('/update/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionUpdate']); // App::$collector->any('/update/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionUpdate']);
// App::$collector->any("/edit/{id}", [\kernel\modules\post\controllers\PostController::class, 'actionEdit']); // App::$collector->any("/edit/{id}", [\kernel\modules\post\controllers\PostController::class, 'actionEdit']);
// App::$collector->get('/delete/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionDelete']); App::$collector->get('/delete/{id}', [\kernel\modules\post\controllers\PostRestController::class, 'actionDelete']);
}); });
}); });

View File

@ -2,6 +2,7 @@
namespace kernel\modules\post\service; namespace kernel\modules\post\service;
use kernel\helpers\Debug;
use kernel\helpers\Slug; use kernel\helpers\Slug;
use kernel\modules\post\models\Post; use kernel\modules\post\models\Post;
use kernel\FormModel; use kernel\FormModel;
@ -14,7 +15,7 @@ class PostService
$model->content = $form_model->getItem('content'); $model->content = $form_model->getItem('content');
$model->user_id = $form_model->getItem('user_id'); $model->user_id = $form_model->getItem('user_id');
$model->title = $form_model->getItem('title'); $model->title = $form_model->getItem('title');
$model->slug = Slug::url_slug($form_model->getItem('title'), ['transliterate' => true, 'lowercase' => true]); $model->slug = $this->createSlug($form_model);
if ($model->save()){ if ($model->save()){
return $model; return $model;
} }
@ -22,6 +23,26 @@ class PostService
return false; return false;
} }
public function createSlug(FormModel $form_model): string
{
$slug = Slug::url_slug($form_model->getItem('title'), ['transliterate' => true, 'lowercase' => true]);
if (Post::where(['slug' => $slug])->exists()) {
$id = 1;
$tmpSlug = $slug . '-' . $id++;
$slug = $this->recursiveCreateSlug($slug, $tmpSlug, $id);
}
return $slug;
}
protected function recursiveCreateSlug($slug, $tmpSlug, $id): string
{
if (Post::where(['slug' => $tmpSlug])->exists()) {
$tmpSlug = $slug . '-' . $id++;
$tmpSlug = $this->recursiveCreateSlug($slug, $tmpSlug, $id);
}
return $tmpSlug;
}
public function update(FormModel $form_model, Post $post): false|Post public function update(FormModel $form_model, Post $post): false|Post
{ {
$post->content = $form_model->getItem('content'); $post->content = $form_model->getItem('content');