fixing bugs in seo generate, fix bug set publish time in post, changes in import posts command, changes in pagination

This commit is contained in:
F4ilji
2025-04-19 15:07:29 +05:00
parent 1399e2b8de
commit 03aca1725b
10 changed files with 86 additions and 57 deletions
+1 -3
View File
@@ -113,14 +113,12 @@ class PostForm
->helperText('Активируйте для публикации в указанное время'),
DateTimePicker::make('publish_setting.publish_at')
->label('Дата и время публикации')
->native(false)
->displayFormat('d/m/Y H:i')
->seconds(false)
->minutesStep(15)
->helperText('Выберите дату и время публикации')
->required(fn (Forms\Get $get) => $get('publish_setting.publish_after'))
->disabled(fn (Forms\Get $get) => !$get('publish_setting.publish_after'))
->minDate(now())
->native(true)
->maxDate(now()->addMonth()),
]),
]),
@@ -41,7 +41,7 @@ class CreatePost extends CreateRecord
protected function processPostData(array $data): array
{
return (new PostDataProcessor())->process($data);
return (new PostDataProcessor())->process($data, 'create');
}
protected function afterCreate(): void
@@ -49,7 +49,7 @@ class EditPost extends EditRecord
protected function processPostData(array $data): array
{
return (new PostDataProcessor())->process($data);
return (new PostDataProcessor())->process($data, 'edit');
}
protected function afterSave(): void
@@ -68,10 +68,12 @@ class ClientPostController extends Controller
return $query->withAnyTags($slugsArray);
})
->orderBy('publish_at', $request->input('sort', 'desc'))
->paginate(6)
->withQueryString());
->paginate(9)
->withQueryString()
);
});
$categories = Cache::remember('categories', now()->addHours(48), function () {
return CategoryResource::collection(Category::has('posts')->get());
});
+31 -19
View File
@@ -32,13 +32,13 @@ class ImportApiDataPost implements ShouldQueue
{
try {
// Получаем первую страницу данных
$response = Http::get('https://crawdad-fresh-bream.ngrok-free.app/api/posts')->object();
$response = Http::get(env('TRANSFER_PROXY_URL') . '/api/posts')->object();
$last_page = $response->last_page;
Log::info('Last page: ' . $last_page);
// Проходим по всем страницам
for ($page = 1; $page <= $last_page; $page++) {
$response = Http::get("https://crawdad-fresh-bream.ngrok-free.app/api/posts?page=$page")->object();
$response = Http::get(env('TRANSFER_PROXY_URL') . "/api/posts?page=$page")->object();
$results = $response->data;
// Обрабатываем каждую запись
@@ -70,6 +70,8 @@ class ImportApiDataPost implements ShouldQueue
// Обработка контента поста
$content = strip_tags($post->DETAIL_TEXT, '<a>');
$readingTime = $this->calculateReadingTime($content);
$contentData = [
[
'type' => 'paragraph',
@@ -83,22 +85,25 @@ class ImportApiDataPost implements ShouldQueue
$slug = $this->generateSlug($post->NAME);
// Создание нового поста
Post::create([
'id' => $post->ID,
'title' => $post->NAME,
'slug' => $slug,
'preview_text' => strip_tags($post->PREVIEW_TEXT),
'authors' => ['Без автора'],
'content' => $contentData, // Преобразуем в JSON
'status' => 'published',
'images' => $imagePaths, // Сохраняем пути к изображениям
'search_data' => $content, // Преобразуем в JSON
'reading_time' => 2,
'user_id' => 1,
'publish_at' => $post->DATE_CREATE,
'created_at' => $post->DATE_CREATE,
'updated_at' => $post->DATE_CREATE,
]);
Post::firstOrCreate(
['id' => $post->ID], // Условие поиска по ID
[
'title' => $post->NAME,
'slug' => $slug,
'preview_text' => strip_tags($post->PREVIEW_TEXT),
'authors' => ['Без автора'],
'content' => $contentData, // Преобразуем в JSON
'status' => 'published',
'images' => $imagePaths, // Сохраняем пути к изображениям
'preview' => $imagePaths[0],
'search_data' => strip_tags($content), // Преобразуем в JSON
'reading_time' => $readingTime,
'user_id' => 1,
'publish_at' => $post->DATE_CREATE,
'created_at' => $post->DATE_CREATE,
'updated_at' => $post->DATE_CREATE,
]
);
} catch (\Exception $e) {
Log::error('Error importing post ID: ' . $post->ID . ' - ' . $e->getMessage());
}
@@ -109,7 +114,7 @@ class ImportApiDataPost implements ShouldQueue
}
}
private function generateSlug($name)
private function generateSlug($name): string
{
$slug = Str::slug($name);
$originalSlug = $slug;
@@ -123,4 +128,11 @@ class ImportApiDataPost implements ShouldQueue
return $slug;
}
private function calculateReadingTime(string $text): int
{
$wordCount = str_word_count($text, 0, "АаБбВвГгДдЕеЁёЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯя");
$wordsPerMinute = 120; // Средняя скорость чтения
return max(1, round($wordCount / $wordsPerMinute));
}
}
+5 -2
View File
@@ -14,7 +14,7 @@ class SeoPageProvider
{
return $model->seo?->toArray();
}
public function getSeoForCurrentPage(): ?array
public function getSeoForCurrentPage(): array|null
{
$path = $this->getCurrentPath();
@@ -23,8 +23,11 @@ class SeoPageProvider
now()->addHours(1),
fn() => Page::where('path', $path)->first()
);
if ($page) {
return $page->seo->toArray();
}
return $page->seo?->toArray();
return null;
}
private function getCurrentPath(): string
@@ -14,7 +14,7 @@ class PostDataProcessor
* @param array $data
* @return array
*/
public function process(array $data): array
public function process(array $data, $operation): array
{
// Удаляем ненужные данные
unset($data['publication']);
@@ -23,7 +23,14 @@ class PostDataProcessor
$data['preview_text'] = $this->setPreviewText($data);
// Устанавливаем время публикации
$data['publish_at'] = $this->setPublishDateTime($data['publish_setting'], $data['status']);
if ($data['publish_setting']['publish_after'] === true) {
$data['publish_at'] = $this->setPublishDateTimeInFuture($data['publish_setting']);
}
if (($data['status'] === PostStatus::PUBLISHED->value || PostStatus::PUBLISHED) && $operation === 'create') {
$data['publish_at'] = $this->setPublishDateTime();
}
unset($data['publish_setting']);
// Генерируем данные для поиска
@@ -63,21 +70,17 @@ class PostDataProcessor
* @param string $status
* @return Carbon|null
*/
private function setPublishDateTime(array $publishSetting, $status): ?Carbon
private function setPublishDateTime(): ?Carbon
{
return Carbon::now();
}
private function setPublishDateTimeInFuture(array $publishSetting): ?Carbon
{
if ($publishSetting['publish_after'] === true) {
return Carbon::parse($publishSetting['publish_at']);
}
if ($status === PostStatus::PUBLISHED->value) {
return Carbon::now();
}
if ($status === PostStatus::PUBLISHED) {
return Carbon::now();
}
return null;
}
+4 -3
View File
@@ -9,20 +9,21 @@ trait SeoGenerate
{
public function createSeo($record): void
{
$record->seo()->create($this->generateSeo($record));
$seo = $this->generateSeoData($record);
$record->seo()->create($seo);
}
public function updateSeo($record): void
{
if ($record->seo()->exists()) {
$record->seo()->update($this->generateSeo($record));
$record->seo()->update($this->generateSeoData($record));
} else {
$this->createSeo($record);
}
}
private function generateSeo($record) {
private function generateSeoData($record) {
return app(SeoGeneratorService::class)->generate([
'title' => $record->title,
'content' => $record instanceof SeoDescriptionInterface