Skip to content

Commit 3c18f22

Browse files
committed
fix: fix API exception on non-existent builds
1 parent 0f3ac00 commit 3c18f22

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

app/Http/Controllers/ApiController.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Models\Build;
56
use App\Models\Client;
67
use App\Models\Key;
78
use App\Models\Mod;
@@ -228,27 +229,34 @@ private function fetchModpack($slug)
228229

229230
private function fetchBuild($modpackSlug, $buildName)
230231
{
231-
$modpack = Cache::remember('modpack:'.$modpackSlug, now()->addMinutes(5), function () use ($modpackSlug) {
232-
return Modpack::with('builds')
233-
->where('slug', $modpackSlug)
234-
->first();
235-
});
232+
$buildCacheKey = 'modpack:'.$modpackSlug.':build:'.$buildName;
236233

237-
if (! $modpack) {
238-
return ['error' => 'Modpack does not exist'];
239-
}
234+
$build = Cache::get($buildCacheKey);
240235

241-
$build = Cache::remember('modpack:'.$modpackSlug.':build:'.$buildName,
242-
now()->addMinutes(5),
243-
function () use ($modpack, $buildName) {
244-
$build = $modpack->builds->firstWhere('version', '===', $buildName);
236+
if (! $build) {
237+
$modpack = Cache::remember('modpack:'.$modpackSlug, now()->addMinutes(5), function () use ($modpackSlug) {
238+
return Modpack::with('builds')->where('slug', $modpackSlug)->first();
239+
});
245240

246-
$build->load(['modversions', 'modversions.mod']);
241+
if (! $modpack) {
242+
return ['error' => 'Modpack does not exist'];
243+
}
247244

248-
return $build;
249-
});
245+
$build = $modpack->builds->firstWhere('version', '===', $buildName);
250246

251-
if (! $build) {
247+
if ($build) {
248+
// Cache the found build with its relationships for 5 minutes
249+
$build->load(['modversions', 'modversions.mod']);
250+
Cache::put($buildCacheKey, $build, now()->addMinutes(5));
251+
} else {
252+
// Cache the "not found" result for 1 minute
253+
$build = Build::NOT_FOUND_CACHE_VALUE;
254+
Cache::put($buildCacheKey, $build, now()->addMinute());
255+
}
256+
}
257+
258+
// Handle the "not found" case
259+
if ($build === Build::NOT_FOUND_CACHE_VALUE) {
252260
return ['error' => 'Build does not exist'];
253261
}
254262

app/Models/Build.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
*/
4242
class Build extends Model
4343
{
44+
const NOT_FOUND_CACHE_VALUE = 'not_found';
45+
4446
protected $guarded = [];
4547

4648
public function modpack(): BelongsTo

0 commit comments

Comments
 (0)