Optimierung 3.2: API-PR-Ownership-Scope + zentrale Eager-Loads
- PressRelease::scopeOwnedByApiUser() (bewusst eng: nur user_id-Autorschaft, anders als das breitere scopeAccessibleBy) ersetzt die byte-gleiche findOwnedPressRelease-Query in PressReleaseController + ImageController. - apiResourceRelations()-Helfer ersetzt den 5x identischen Eager-Load-Set (company/category.translations/images) in Index/Show/Create/Update/Reclassify. Bewusst NICHT umgesetzt: Slug-Reimplementierung (uniqueSlug) durch HasUniqueSlug ersetzen - der API-Fallback Str::random(8) weicht vom Trait-slugFallback() ab (Verhaltensaenderung bei leer-slugifizierten Titeln). Tests: neuer Scope-Test; volle Suite gruen (802 passed, 1 skipped). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
9ddfab62c2
commit
2bf431c8ba
4 changed files with 40 additions and 7 deletions
|
|
@ -27,7 +27,7 @@ class PressReleaseController extends Controller
|
|||
|
||||
$pressReleases = PressRelease::withoutGlobalScopes()
|
||||
->where('user_id', $request->user()->id)
|
||||
->with(['company:id,name,portal,legacy_portal,legacy_id', 'category.translations', 'images'])
|
||||
->with($this->apiResourceRelations())
|
||||
->when($request->query('status'), fn ($query, string $status) => $query->where('status', $status))
|
||||
->latest()
|
||||
->paginate(min((int) $request->query('per_page', 25), 100));
|
||||
|
|
@ -58,7 +58,7 @@ class PressReleaseController extends Controller
|
|||
]);
|
||||
|
||||
return PressReleaseResource::make(
|
||||
$pressRelease->load(['company:id,name,portal,legacy_portal,legacy_id', 'category.translations', 'images'])
|
||||
$pressRelease->load($this->apiResourceRelations())
|
||||
)->response()->setStatusCode(201);
|
||||
}
|
||||
|
||||
|
|
@ -69,7 +69,7 @@ class PressReleaseController extends Controller
|
|||
abort_unless($pressRelease !== null, 403);
|
||||
|
||||
return PressReleaseResource::make(
|
||||
$pressRelease->load(['company:id,name,portal,legacy_portal,legacy_id', 'category.translations', 'images'])
|
||||
$pressRelease->load($this->apiResourceRelations())
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -117,7 +117,7 @@ class PressReleaseController extends Controller
|
|||
}
|
||||
|
||||
return PressReleaseResource::make(
|
||||
$pressRelease->load(['company:id,name,portal,legacy_portal,legacy_id', 'category.translations', 'images'])
|
||||
$pressRelease->load($this->apiResourceRelations())
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -158,7 +158,7 @@ class PressReleaseController extends Controller
|
|||
}
|
||||
|
||||
return PressReleaseResource::make(
|
||||
$pressRelease->fresh()->load(['company:id,name,portal,legacy_portal,legacy_id', 'category.translations', 'images'])
|
||||
$pressRelease->fresh()->load($this->apiResourceRelations())
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -187,11 +187,22 @@ class PressReleaseController extends Controller
|
|||
->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationen für die API-Resource-Ausgabe. Zentral, damit Index, Show,
|
||||
* Create, Update und Reclassify denselben Satz laden.
|
||||
*
|
||||
* @return list<string>
|
||||
*/
|
||||
private function apiResourceRelations(): array
|
||||
{
|
||||
return ['company:id,name,portal,legacy_portal,legacy_id', 'category.translations', 'images'];
|
||||
}
|
||||
|
||||
private function findOwnedPressRelease(int $pressReleaseId, Request $request): ?PressRelease
|
||||
{
|
||||
return PressRelease::withoutGlobalScopes()
|
||||
->whereKey($pressReleaseId)
|
||||
->where('user_id', $request->user()->id)
|
||||
->ownedByApiUser($request->user())
|
||||
->first();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -106,7 +106,7 @@ class PressReleaseImageController extends Controller
|
|||
{
|
||||
return PressRelease::withoutGlobalScopes()
|
||||
->whereKey($pressReleaseId)
|
||||
->where('user_id', $request->user()->id)
|
||||
->ownedByApiUser($request->user())
|
||||
->first();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue