forge/app/View/Components/ModListSection.php

97 lines
3.0 KiB
PHP
Raw Normal View History

2024-05-17 23:54:03 -04:00
<?php
namespace App\View\Components;
use App\Models\Mod;
use App\Models\ModVersion;
2024-05-17 23:54:03 -04:00
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Cache;
2024-05-17 23:54:03 -04:00
use Illuminate\View\Component;
class ModListSection extends Component
{
2024-05-21 21:02:49 -04:00
public Collection $modsFeatured;
2024-05-22 01:00:37 -04:00
2024-05-17 23:54:03 -04:00
public Collection $modsLatest;
2024-05-22 01:00:37 -04:00
2024-05-17 23:54:03 -04:00
public Collection $modsUpdated;
public function __construct()
{
2024-05-21 21:02:49 -04:00
$this->modsFeatured = $this->fetchFeaturedMods();
2024-05-17 23:54:03 -04:00
$this->modsLatest = $this->fetchLatestMods();
$this->modsUpdated = $this->fetchUpdatedMods();
}
2024-05-21 21:02:49 -04:00
private function fetchFeaturedMods(): Collection
2024-05-17 23:54:03 -04:00
{
return Cache::remember('homepage-featured-mods', now()->addMinutes(5), function () {
return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured'])
->withTotalDownloads()
->with(['latestVersion', 'latestVersion.sptVersion', 'users:id,name'])
->where('featured', true)
->latest()
->limit(6)
->get();
});
2024-05-17 23:54:03 -04:00
}
private function fetchLatestMods(): Collection
{
return Cache::remember('homepage-latest-mods', now()->addMinutes(5), function () {
return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured', 'created_at'])
->withTotalDownloads()
->with(['latestVersion', 'latestVersion.sptVersion', 'users:id,name'])
->latest()
->limit(6)
->get();
});
2024-05-17 23:54:03 -04:00
}
private function fetchUpdatedMods(): Collection
{
return Cache::remember('homepage-updated-mods', now()->addMinutes(5), function () {
return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured'])
->withTotalDownloads()
->with(['lastUpdatedVersion', 'lastUpdatedVersion.sptVersion', 'users:id,name'])
->orderByDesc(
ModVersion::select('updated_at')
->whereColumn('mod_id', 'mods.id')
->orderByDesc('updated_at')
->take(1)
)
->limit(6)
->get();
});
2024-05-17 23:54:03 -04:00
}
public function render(): View
{
return view('components.mod-list-section', [
'sections' => $this->getSections(),
]);
}
2024-05-17 23:54:03 -04:00
public function getSections(): array
{
return [
[
2024-05-21 21:02:49 -04:00
'title' => 'Featured Mods',
'mods' => $this->modsFeatured,
'versionScope' => 'latestVersion',
2024-05-17 23:54:03 -04:00
],
[
'title' => 'Newest Mods',
2024-05-17 23:54:03 -04:00
'mods' => $this->modsLatest,
'versionScope' => 'latestVersion',
2024-05-17 23:54:03 -04:00
],
[
'title' => 'Recently Updated Mods',
'mods' => $this->modsUpdated,
'versionScope' => 'lastUpdatedVersion',
2024-05-17 23:54:03 -04:00
],
];
}
}