api action delete add, slug generator update
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
|
||||
namespace kernel\modules\post\service;
|
||||
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\helpers\Slug;
|
||||
use kernel\modules\post\models\Post;
|
||||
use kernel\FormModel;
|
||||
@ -14,7 +15,7 @@ class PostService
|
||||
$model->content = $form_model->getItem('content');
|
||||
$model->user_id = $form_model->getItem('user_id');
|
||||
$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()){
|
||||
return $model;
|
||||
}
|
||||
@ -22,6 +23,26 @@ class PostService
|
||||
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
|
||||
{
|
||||
$post->content = $form_model->getItem('content');
|
||||
|
Reference in New Issue
Block a user