feat: Refactor MainController and related tasks to follow Porto architecture
- Moved logic from MainController to dedicated action and task classes. - Introduced GetMainPageDataAction, GetEducationsDataTask, GetRecentPostsTask, GetUpcomingEventsTask, and GetPageDataTask for better separation of concerns. - Updated routes to use the new MainController from the Main container. - Deprecated the old MainController with a clear message for future removal. - Updated caching mechanisms to use the new task classes. - Adjusted environment variable access to use config() instead of env() for better practice. - Added new MainPageResource for structured data response. - Updated .gitignore to exclude QWEN.md. - Added Jenkins service to docker-compose for CI/CD integration.
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Main\Actions;
|
||||
|
||||
use App\Containers\Main\Tasks\GetEducationsDataTask;
|
||||
use App\Containers\Main\Tasks\GetRecentPostsTask;
|
||||
use App\Containers\Main\Tasks\GetUpcomingEventsTask;
|
||||
use App\Containers\Main\Tasks\GetPageDataTask;
|
||||
|
||||
class GetMainPageDataAction
|
||||
{
|
||||
public function __construct(
|
||||
private readonly GetEducationsDataTask $getEducationsDataTask,
|
||||
private readonly GetRecentPostsTask $getRecentPostsTask,
|
||||
private readonly GetUpcomingEventsTask $getUpcomingEventsTask,
|
||||
private readonly GetPageDataTask $getPageDataTask,
|
||||
) {}
|
||||
|
||||
public function run(): array
|
||||
{
|
||||
$educations = $this->getEducationsDataTask->run();
|
||||
$posts = $this->getRecentPostsTask->run();
|
||||
$events = $this->getUpcomingEventsTask->run();
|
||||
$page = $this->getPageDataTask->run();
|
||||
|
||||
$seo = $page->seo ?? null;
|
||||
|
||||
return [
|
||||
'educations' => $educations,
|
||||
'posts' => $posts,
|
||||
'events' => $events,
|
||||
'seo' => $seo
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Main\Tasks;
|
||||
|
||||
use App\Containers\AdditionalEducation\Models\AdditionalEducation;
|
||||
use App\Containers\AdditionalEducation\Models\AdditionalEducationCategory;
|
||||
use App\Containers\Education\Models\AdmissionCampaign;
|
||||
use App\Ship\Enums\Education\LevelEducational;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class GetEducationsDataTask
|
||||
{
|
||||
public function run(): array
|
||||
{
|
||||
return Cache::remember('educations_data', now()->addHour(), function () {
|
||||
return $this->getEducationsData();
|
||||
});
|
||||
}
|
||||
|
||||
private function getEducationsData(): array
|
||||
{
|
||||
return [
|
||||
'admission_campaign' => $this->getAdmissionCampaign(),
|
||||
'additional_education' => [
|
||||
'educations_count' => AdditionalEducation::where('is_active', true)->count(),
|
||||
'categories_count' => AdditionalEducationCategory::where('is_active', true)->count(),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private function getAdmissionCampaign(): array
|
||||
{
|
||||
$info = AdmissionCampaign::first()->info ?? [];
|
||||
|
||||
return collect($info)->reduce(function ($carry, $a) {
|
||||
$lvl = LevelEducational::from((int)$a['edu_name'])->name;
|
||||
$carry[$lvl] = [
|
||||
'total_programs' => $a['total_programs'],
|
||||
'places' => [
|
||||
'och_count' => $a['och_count'],
|
||||
'zaoch_count' => $a['zaoch_count'],
|
||||
'budget_places' => $a['budget_places'],
|
||||
'non_budget_places' => $a['non_budget_places']
|
||||
],
|
||||
];
|
||||
return $carry;
|
||||
}, []);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Main\Tasks;
|
||||
|
||||
use App\Containers\AppStructure\Models\Page;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class GetPageDataTask
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$path = route('index', null, false);
|
||||
|
||||
return Cache::remember('page_' . $path, now()->addHour(), function () use ($path) {
|
||||
return Page::where('path', $path)->first();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Main\Tasks;
|
||||
|
||||
use App\Containers\Article\Enums\PostStatus;
|
||||
use App\Containers\Article\Models\Post;
|
||||
use App\Containers\Widget\UI\API\Transformers\PostThumbnailResource;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class GetRecentPostsTask
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return Cache::remember('posts_recent', now()->addHour(), function () {
|
||||
return $this->getRecentPosts();
|
||||
});
|
||||
}
|
||||
|
||||
private function getRecentPosts()
|
||||
{
|
||||
return PostThumbnailResource::collection(
|
||||
Post::select('title', 'slug', 'authors', 'preview_text', 'category_id', 'preview', 'search_data', 'publish_at', 'created_at')
|
||||
->with('category')
|
||||
->where('publish_at', '<', Carbon::now())
|
||||
->where('status', '=', PostStatus::PUBLISHED)
|
||||
->orderBy('publish_at', 'desc')
|
||||
->limit(3)
|
||||
->get()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Main\Tasks;
|
||||
|
||||
use App\Containers\Event\Models\Event;
|
||||
use App\Containers\Event\UI\WEB\Transformers\EventThumbnailResource;
|
||||
use DateTime;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class GetUpcomingEventsTask
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return Cache::remember('upcoming_events', now()->addHour(), function () {
|
||||
return $this->getUpcomingEvents();
|
||||
});
|
||||
}
|
||||
|
||||
private function getUpcomingEvents()
|
||||
{
|
||||
$event_date_start = (new DateTime())->format('Y-m-d');
|
||||
|
||||
return EventThumbnailResource::collection(
|
||||
Event::select('title', 'slug', 'event_date_start', 'event_time_start' , 'address', 'is_online', 'category_id')
|
||||
->where('event_date_start', '>=', $event_date_start)
|
||||
->orderBy('event_date_start', 'asc')
|
||||
->limit(3)
|
||||
->get()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Main\UI\WEB\Controllers;
|
||||
|
||||
use App\Containers\Main\Actions\GetMainPageDataAction;
|
||||
use App\Ship\Controllers\Controller;
|
||||
use Inertia\Inertia;
|
||||
|
||||
class MainController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly GetMainPageDataAction $getMainPageDataAction
|
||||
) {}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$data = $this->getMainPageDataAction->run();
|
||||
|
||||
return Inertia::render('Main', $data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Main\UI\WEB\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class MainPageResource extends JsonResource
|
||||
{
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'educations' => $this->educations,
|
||||
'posts' => $this->posts,
|
||||
'events' => $this->events,
|
||||
'seo' => $this->seo,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user