This commit is contained in:
f4ilji
2024-11-24 14:55:12 +05:00
parent 048f046dcf
commit 77cc7b2c76
4 changed files with 129 additions and 10 deletions
@@ -0,0 +1,19 @@
<?php
namespace App\Console\Commands;
use App\Jobs\ImportApiDataPost;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
class TransferPostsToNewTable extends Command
{
protected $signature = 'import:api';
protected $description = 'Импортировать данные из статического SQL файла в базу данных';
public function handle()
{
ImportApiDataPost::dispatch();
}
}
+107
View File
@@ -0,0 +1,107 @@
<?php
namespace App\Jobs;
use App\Enums\PostStatus;
use App\Models\Post;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
class ImportApiDataPost implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*/
public function __construct()
{
//
}
/**
* Execute the job.
*/
public function handle()
{
try {
$response = Http::get('https://crawdad-fresh-bream.ngrok-free.app/api/posts')->object();
foreach ($response as $item) {
try {
$postResponse = Http::get('https://crawdad-fresh-bream.ngrok-free.app/api/posts/' . $item->ID)->object();
// Проверка на наличие данных
if (!isset($postResponse->post)) {
Log::error('Post data not found for ID: ' . $item->ID);
continue;
}
$postData = $postResponse->post;
$images = $postResponse->images ?? []; // Обработка отсутствия изображений
// Обработка изображения
$imagePaths = [];
foreach ($images as $image) {
$imagePaths[] = 'upload/' . $image->SUBDIR . '/' . $image->FILE_NAME;
}
$content = strip_tags($postData->DETAIL_TEXT, '<a>');
$contentData = [
[
'type' => 'paragraph',
'data' => [
'content' => $content,
],
],
];
$slug = $this->generateSlug($postData->NAME);
// Создание нового поста
Post::create([
'id' => $postData->ID,
'title' => $postData->NAME,
'slug' => $slug,
'preview_text' => strip_tags($postData->PREVIEW_TEXT),
'authors' => array('Без автора'),
'content' => $contentData, // Преобразуем в JSON
'status' => 'published',
'images' => $imagePaths,
'search_data' => $content, // Преобразуем в JSON
'reading_time' => 2,
'user_id' => 1,
'publish_at' => $postData->DATE_CREATE,
'created_at' => $postData->DATE_CREATE,
'updated_at' => $postData->DATE_CREATE,
]);
} catch (\Exception $e) {
Log::error('Error importing post ID: ' . $item->ID . ' - ' . $e->getMessage());
}
}
} catch (\Exception $e) {
Log::error('Error fetching posts: ' . $e->getMessage());
}
}
private function generateSlug($name)
{
$slug = Str::slug($name);
$originalSlug = $slug;
$count = 1;
while (Post::where('slug', $slug)->exists()) {
$slug = $originalSlug . '-' . $count;
$count++;
}
return $slug;
}
}
-9
View File
@@ -8,9 +8,6 @@ use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Laravel\Fortify\Events\TwoFactorAuthenticationChallenged;
use Laravel\Fortify\Events\TwoFactorAuthenticationEnabled;
use Vormkracht10\TwoFactorAuth\Listeners\SendTwoFactorCodeListener;
class EventServiceProvider extends ServiceProvider class EventServiceProvider extends ServiceProvider
{ {
@@ -23,12 +20,6 @@ class EventServiceProvider extends ServiceProvider
Registered::class => [ Registered::class => [
SendEmailVerificationNotification::class, SendEmailVerificationNotification::class,
], ],
TwoFactorAuthenticationChallenged::class => [
SendTwoFactorCodeListener::class,
],
TwoFactorAuthenticationEnabled::class => [
SendTwoFactorCodeListener::class,
],
]; ];
/** /**
+3 -1
View File
@@ -63,7 +63,9 @@
> >
<span> <span>
<div class="flex items-center gap-3"> <div class="flex items-center gap-3">
<span v-if="post" class="truncate text-sm">{{ textLimit(post.authors[0], 20) }}</span> <span v-if="post && post.authors && post.authors.length > 0" class="truncate text-sm">
{{ textLimit(post.authors[0], 20) }}
</span>
</div> </div>
</span> </span>
<span class="text-xs text-gray-300 dark:text-gray-600" <span class="text-xs text-gray-300 dark:text-gray-600"