diff --git a/app/Http/Controllers/ModController.php b/app/Http/Controllers/ModController.php index 3f29240..40bfb0a 100644 --- a/app/Http/Controllers/ModController.php +++ b/app/Http/Controllers/ModController.php @@ -28,9 +28,8 @@ class ModController extends Controller public function show(int $modId, string $slug) { $mod = Mod::select() - ->withLatestSptVersion() ->withTotalDownloads() - ->with('users:id,name') + ->with(['latestSptVersion', 'users:id,name']) ->with('license:id,name,link') ->find($modId); diff --git a/app/Jobs/ImportHubData.php b/app/Jobs/ImportHubData.php index 7136567..faaa631 100644 --- a/app/Jobs/ImportHubData.php +++ b/app/Jobs/ImportHubData.php @@ -56,6 +56,7 @@ class ImportHubData implements ShouldBeUnique, ShouldQueue Artisan::call('scout:delete-all-indexes'); Artisan::call('scout:sync-index-settings'); Artisan::call('scout:import', ['model' => '\App\Models\Mod']); + Artisan::call('scout:import', ['model' => '\App\Models\User']); } /** diff --git a/app/Livewire/GlobalSearch.php b/app/Livewire/GlobalSearch.php index b3dae26..991f5d1 100644 --- a/app/Livewire/GlobalSearch.php +++ b/app/Livewire/GlobalSearch.php @@ -42,8 +42,8 @@ class GlobalSearch extends Component if (Str::length($query)) { $results['data'] = [ - 'user' => User::search($query)->get(), - 'mod' => Mod::search($query)->get(), + 'user' => collect(User::search($query)->raw()['hits']), + 'mod' => collect(Mod::search($query)->raw()['hits']), ]; $results['total'] = $this->countTotalResults($results['data']); } diff --git a/app/Models/Mod.php b/app/Models/Mod.php index 97eed4b..b44c52b 100644 --- a/app/Models/Mod.php +++ b/app/Models/Mod.php @@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; @@ -50,55 +51,15 @@ class Mod extends Model */ public function scopeWithTotalDownloads($query) { - return $query->addSelect(['total_downloads' => ModVersion::selectRaw('SUM(downloads) AS total_downloads') - ->whereColumn('mod_id', 'mods.id'), + return $query->addSelect([ + 'total_downloads' => ModVersion::selectRaw('SUM(downloads) AS total_downloads') + ->whereColumn('mod_id', 'mods.id'), ]); } - public function latestSptVersion(): BelongsTo + public function lastUpdatedVersion(): HasOne { - return $this->belongsTo(ModVersion::class, 'latest_spt_version_id'); - } - - public function scopeWithLatestSptVersion($query) - { - return $query - ->addSelect(['latest_spt_version_id' => ModVersion::select('id') - ->whereColumn('mod_id', 'mods.id') - ->orderByDesc( - SptVersion::select('version') - ->whereColumn('mod_versions.spt_version_id', 'spt_versions.id') - ->orderByDesc('version') - ->take(1), - ) - ->orderByDesc('version') - ->take(1), - ]) - ->havingNotNull('latest_spt_version_id') - ->with(['latestSptVersion', 'latestSptVersion.sptVersion']); - } - - public function lastUpdatedVersion(): BelongsTo - { - return $this->belongsTo(ModVersion::class, 'last_updated_spt_version_id'); - } - - public function scopeWithLastUpdatedVersion($query) - { - return $query - ->addSelect(['last_updated_spt_version_id' => ModVersion::select('id') - ->whereColumn('mod_id', 'mods.id') - ->orderByDesc('updated_at') - ->take(1), - ]) - ->orderByDesc( - ModVersion::select('updated_at') - ->whereColumn('mod_id', 'mods.id') - ->orderByDesc('updated_at') - ->take(1) - ) - ->havingNotNull('last_updated_spt_version_id') - ->with(['lastUpdatedVersion', 'lastUpdatedVersion.sptVersion']); + return $this->hasOne(ModVersion::class)->orderByDesc('updated_at')->with('sptVersion'); } /** @@ -106,6 +67,8 @@ class Mod extends Model */ public function toSearchableArray(): array { + $latestSptVersion = $this->latestSptVersion()->first(); + return [ 'id' => (int) $this->id, 'name' => $this->name, @@ -115,9 +78,25 @@ class Mod extends Model 'featured' => $this->featured, 'created_at' => strtotime($this->created_at), 'updated_at' => strtotime($this->updated_at), + 'latestSptVersion' => $latestSptVersion?->sptVersion->version, + 'latestSptVersionColorClass' => $latestSptVersion?->sptVersion->color_class, ]; } + public function latestSptVersion(): HasOne + { + return $this->hasOne(ModVersion::class) + ->orderByDesc( + SptVersion::select('version') + ->whereColumn('mod_versions.spt_version_id', 'spt_versions.id') + ->orderByDesc('version') + ->take(1), + ) + ->with('sptVersion') + ->orderByDesc('version') + ->take(1); + } + /** * Determine if the model should be searchable. */ diff --git a/app/View/Components/ModListSection.php b/app/View/Components/ModListSection.php index 82335a7..4805b0d 100644 --- a/app/View/Components/ModListSection.php +++ b/app/View/Components/ModListSection.php @@ -3,8 +3,10 @@ namespace App\View\Components; use App\Models\Mod; +use App\Models\ModVersion; use Illuminate\Contracts\View\View; use Illuminate\Database\Eloquent\Collection; +use Illuminate\Support\Facades\Cache; use Illuminate\View\Component; class ModListSection extends Component @@ -24,36 +26,44 @@ class ModListSection extends Component private function fetchFeaturedMods(): Collection { - return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured']) - ->withLatestSptVersion() - ->withTotalDownloads() - ->with('users:id,name') - ->where('featured', true) - ->latest() - ->limit(6) - ->get(); + return Cache::remember('homepage-featured-mods', now()->addMinutes(5), function () { + return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured']) + ->withTotalDownloads() + ->with(['latestSptVersion', 'users:id,name']) + ->where('featured', true) + ->latest() + ->limit(6) + ->get(); + }); } private function fetchLatestMods(): Collection { - return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured', 'created_at']) - ->withLatestSptVersion() - ->withTotalDownloads() - ->with('users:id,name') - ->latest() - ->limit(6) - ->get(); + return Cache::remember('homepage-latest-mods', now()->addMinutes(5), function () { + return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured', 'created_at']) + ->withTotalDownloads() + ->with(['latestSptVersion', 'users:id,name']) + ->latest() + ->limit(6) + ->get(); + }); } private function fetchUpdatedMods(): Collection { - return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured']) - ->withLastUpdatedVersion() - ->withTotalDownloads() - ->with('users:id,name') - ->latest() - ->limit(6) - ->get(); + return Cache::remember('homepage-updated-mods', now()->addMinutes(5), function () { + return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured']) + ->withTotalDownloads() + ->with(['lastUpdatedVersion', 'users:id,name']) + ->orderByDesc( + ModVersion::select('updated_at') + ->whereColumn('mod_id', 'mods.id') + ->orderByDesc('updated_at') + ->take(1) + ) + ->limit(6) + ->get(); + }); } public function render(): View diff --git a/resources/views/components/global-search-result-mod.blade.php b/resources/views/components/global-search-result-mod.blade.php index 588b922..9200f2d 100644 --- a/resources/views/components/global-search-result-mod.blade.php +++ b/resources/views/components/global-search-result-mod.blade.php @@ -1,9 +1,12 @@ - + @if(empty($result->thumbnail)) - {{ $result->name }} - + {{ $result['name'] }} + @else - {{ $result->name }} + {{ $result['name'] }} @endif -

{{ $result->name }}

+

{{ $result['name'] }}

+

+ {{ $result['latestSptVersion'] }} +

diff --git a/resources/views/components/global-search-result-user.blade.php b/resources/views/components/global-search-result-user.blade.php index 96aadb1..93f5cce 100644 --- a/resources/views/components/global-search-result-user.blade.php +++ b/resources/views/components/global-search-result-user.blade.php @@ -1,3 +1,3 @@ - -

{{ $result->name }}

+
+

{{ $result['name'] }}

diff --git a/resources/views/components/global-search-results.blade.php b/resources/views/components/global-search-results.blade.php index 20c5d91..4f6cc65 100644 --- a/resources/views/components/global-search-results.blade.php +++ b/resources/views/components/global-search-results.blade.php @@ -10,13 +10,15 @@ - @foreach($typeResults as $result) - @component('components.global-search-result-' . Str::lower($typeName), [ - 'result' => $result, - 'linkClass' => 'group/global-search-link flex flex-row gap-3 py-1.5 px-4 text-gray-900 dark:text-gray-100 hover:bg-gray-200 dark:hover:bg-gray-800 transition-colors duration-100 ease-in-out', - ]) - @endcomponent - @endforeach +
+ @foreach($typeResults as $result) + @component('components.global-search-result-' . Str::lower($typeName), [ + 'result' => $result, + 'linkClass' => 'group/global-search-link flex flex-row gap-3 py-1.5 px-4 text-gray-900 dark:text-gray-100 hover:bg-gray-200 dark:hover:bg-gray-800 transition-colors duration-200 ease-in-out', + ]) + @endcomponent + @endforeach +
@endif @endforeach diff --git a/resources/views/components/mod-list.blade.php b/resources/views/components/mod-list.blade.php index f8ba548..519feae 100644 --- a/resources/views/components/mod-list.blade.php +++ b/resources/views/components/mod-list.blade.php @@ -16,9 +16,6 @@
- @if(is_null($mod->{$versionScope})) - @dd($mod) - @endif

{{ $mod->name }}

{{ $mod->{$versionScope}->sptVersion->version }}