Simplifies Latest Version Relationship

This change removes the `latestSptVersion()` relationship, which fetched the latest mod version for the latest SPT version, with a more simple `latestVersion()` relationship that simply fetches the latest mod version based on the mod version, version number field.

This is less complicated and much less confusing.
This commit is contained in:
Refringe 2024-07-23 21:21:55 -04:00
parent 8a9e7b0573
commit 8eae71b22a
Signed by: Refringe
SSH Key Fingerprint: SHA256:t865XsQpfTeqPRBMN2G6+N8wlDjkgUCZF3WGW6O9N/k
5 changed files with 28 additions and 30 deletions

View File

@ -27,10 +27,8 @@ class ModController extends Controller
public function show(int $modId, string $slug)
{
$mod = Mod::select()
->withTotalDownloads()
->with(['latestSptVersion', 'users:id,name'])
->with('license:id,name,link')
$mod = Mod::withTotalDownloads()
->with(['latestVersion', 'users:id,name', 'license:id,name,link'])
->find($modId);
if (! $mod || $mod->slug !== $slug) {

View File

@ -59,7 +59,8 @@ class Mod extends Model
public function lastUpdatedVersion(): HasOne
{
return $this->hasOne(ModVersion::class)->orderByDesc('updated_at')->with('sptVersion');
return $this->hasOne(ModVersion::class)
->orderByDesc('updated_at');
}
/**
@ -67,7 +68,7 @@ class Mod extends Model
*/
public function toSearchableArray(): array
{
$latestSptVersion = $this->latestSptVersion()->first();
$latestVersion = $this->latestVersion()->with('sptVersion')->first();
return [
'id' => (int) $this->id,
@ -78,21 +79,17 @@ 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,
'latestVersion' => $latestVersion?->sptVersion->version,
'latestVersionColorClass' => $latestVersion?->sptVersion->color_class,
];
}
public function latestSptVersion(): HasOne
/**
* The relationship to the latest mod version, dictated by the mod version number.
*/
public function latestVersion(): 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);
}

View File

@ -29,7 +29,8 @@ class ModListSection extends Component
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'])
->with(['latestVersion', 'latestVersion.sptVersion', 'users:id,name'])
->whereHas('latestVersion')
->where('featured', true)
->latest()
->limit(6)
@ -42,7 +43,8 @@ class ModListSection extends Component
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'])
->with(['latestVersion', 'latestVersion.sptVersion', 'users:id,name'])
->whereHas('latestVersion')
->latest()
->limit(6)
->get();
@ -54,7 +56,8 @@ class ModListSection extends Component
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'])
->with(['lastUpdatedVersion', 'lastUpdatedVersion.sptVersion', 'users:id,name'])
->whereHas('lastUpdatedVersion')
->orderByDesc(
ModVersion::select('updated_at')
->whereColumn('mod_id', 'mods.id')
@ -79,12 +82,12 @@ class ModListSection extends Component
[
'title' => 'Featured Mods',
'mods' => $this->modsFeatured,
'versionScope' => 'latestSptVersion',
'versionScope' => 'latestVersion',
],
[
'title' => 'Newest Mods',
'mods' => $this->modsLatest,
'versionScope' => 'latestSptVersion',
'versionScope' => 'latestVersion',
],
[
'title' => 'Recently Updated Mods',

View File

@ -6,7 +6,7 @@
<img src="{{ Storage::url($result['thumbnail']) }}" alt="{{ $result['name'] }}" class="h-6 w-6 self-center">
@endif
<p class="flex-grow">{{ $result['name'] }}</p>
<p class="ml-auto self-center badge-version {{ $result['latestSptVersionColorClass'] }} }} inline-flex items-center rounded-md px-2 py-1 text-xs font-medium text-nowrap">
{{ $result['latestSptVersion'] }}
<p class="ml-auto self-center badge-version {{ $result['latestVersionColorClass'] }} }} inline-flex items-center rounded-md px-2 py-1 text-xs font-medium text-nowrap">
{{ $result['latestVersion'] }}
</p>
</a>

View File

@ -22,11 +22,11 @@
<h2 class="pb-1 sm:p-0 text-3xl font-bold text-gray-900 dark:text-white">
{{ $mod->name }}
<span class="font-light text-nowrap text-gray-700 dark:text-gray-400">
{{ $mod->latestSptVersion->version }}
{{ $mod->latestVersion->version }}
</span>
</h2>
<p>{{ __('Created by') }} {{ $mod->users->pluck('name')->implode(', ') }}</p>
<p>{{ $mod->latestSptVersion->sptVersion->version }} {{ __('Compatible') }}</p>
<p>{{ $mod->latestVersion->sptVersion->version }} {{ __('Compatible') }}</p>
<p>{{ $mod->total_downloads }} {{ __('Downloads') }}</p>
</div>
</div>
@ -66,8 +66,8 @@
</div>
<div class="col-span-1 flex flex-col gap-6">
<a href="{{ $mod->latestSptVersion->link }}" class="block">
<button type="button" class="w-full">{{ __('Download Latest Version') }} ({{ $mod->latestSptVersion->version }})</button>
<a href="{{ $mod->latestVersion->link }}" class="block">
<button type="button" class="w-full">{{ __('Download Latest Version') }} ({{ $mod->latestVersion->version }})</button>
</a>
<div class="p-4 sm:p-6 bg-white dark:bg-gray-950 rounded-xl shadow-md dark:shadow-gray-950 drop-shadow-2xl">
@ -93,12 +93,12 @@
</p>
</li>
@endif
@if($mod->latestSptVersion->virus_total_link)
@if($mod->latestVersion->virus_total_link)
<li class="px-4 py-4 sm:px-0">
<h3>{{ __('Latest VirusTotal Result') }}</h3>
<p class="truncate">
<a href="{{ $mod->latestSptVersion->virus_total_link }}" title="{{ $mod->latestSptVersion->virus_total_link }}" target="_blank">
{{ $mod->latestSptVersion->virus_total_link }}
<a href="{{ $mod->latestVersion->virus_total_link }}" title="{{ $mod->latestVersion->virus_total_link }}" target="_blank">
{{ $mod->latestVersion->virus_total_link }}
</a>
</p>
</li>