Optimierung 4.3/4.4 (Newsroom-Detail): Page-Data-Service + Invokable Controller
Newsroom-Markenseite in NewsroomDetailPageData + Web\NewsroomDetailController.
Controller-Signatur fuehrt {edition} vor {slug} (positionsgebundene
Route-Parameter-Aufloesung). Verhalten unveraendert, 92 Web-Tests gruen.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
e36f91704a
commit
f17e9a96bc
3 changed files with 152 additions and 111 deletions
131
app/Services/Web/NewsroomDetailPageData.php
Normal file
131
app/Services/Web/NewsroomDetailPageData.php
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services\Web;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\Company;
|
||||
use App\Models\PressRelease;
|
||||
use App\Scopes\PortalScope;
|
||||
use App\Support\WebPortalContext;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
/**
|
||||
* Datenaufbereitung der Newsroom-Markenseite eines Unternehmens je Ausgabe:
|
||||
* alle veröffentlichten Meldungen der Firma plus Kennzahlen. Ohne aktives
|
||||
* Unternehmen oder ohne veröffentlichte Meldung gibt es keine öffentliche Seite
|
||||
* (404).
|
||||
*/
|
||||
class NewsroomDetailPageData
|
||||
{
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function build(WebPortalContext $context, string $slug): array
|
||||
{
|
||||
$portalValues = $context->portalValues;
|
||||
$language = $context->language;
|
||||
|
||||
$company = Company::query()
|
||||
->withoutGlobalScope(PortalScope::class)
|
||||
->whereIn('portal', $portalValues)
|
||||
->where('is_active', true)
|
||||
->where('slug', $slug)
|
||||
->with(['contacts'])
|
||||
->first();
|
||||
|
||||
if (! $company) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$base = static fn (): Builder => PressRelease::query()
|
||||
->withoutGlobalScope(PortalScope::class)
|
||||
->where('company_id', $company->id)
|
||||
->forPortalValues($portalValues)
|
||||
->forLanguage($language)
|
||||
->visibleInArchive();
|
||||
|
||||
// Keine öffentliche Newsroom-Seite ohne veröffentlichte Meldung.
|
||||
if (! $base()->exists()) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$latest = $base()->max('published_at');
|
||||
$referenceNow = $latest ? Carbon::parse($latest) : now();
|
||||
if ($referenceNow->gt(now())) {
|
||||
$referenceNow = now();
|
||||
}
|
||||
$recentSince = $referenceNow->copy()->subDays(30);
|
||||
$oldest = $base()->min('published_at');
|
||||
|
||||
$with = [
|
||||
'company',
|
||||
'category.translations' => fn ($query) => $query->where('locale', $language),
|
||||
'images' => fn ($query) => $query->orderByDesc('is_preview')->orderBy('sort_order')->limit(1),
|
||||
];
|
||||
|
||||
$leadRelease = $base()->with($with)->orderByDesc('published_at')->first();
|
||||
|
||||
$releases = $base()
|
||||
->with($with)
|
||||
->when($leadRelease, fn (Builder $query) => $query->whereKeyNot($leadRelease->getKey()))
|
||||
->orderByDesc('published_at')
|
||||
->paginate(10)
|
||||
->withQueryString();
|
||||
|
||||
$mostReadReleases = $base()
|
||||
->orderByDesc('hits')
|
||||
->orderByDesc('published_at')
|
||||
->limit(5)
|
||||
->get(['id', 'slug', 'title', 'hits', 'portal', 'language']);
|
||||
|
||||
$companyFilter = static function (Builder $query) use ($company, $portalValues, $language): void {
|
||||
$query
|
||||
->withoutGlobalScope(PortalScope::class)
|
||||
->where('company_id', $company->id)
|
||||
->forPortalValues($portalValues)
|
||||
->forLanguage($language)
|
||||
->published();
|
||||
};
|
||||
|
||||
$categories = Category::query()
|
||||
->whereIn('portal', $portalValues)
|
||||
->whereHas('pressReleases', fn (Builder $query) => $companyFilter($query))
|
||||
->with(['translations' => fn ($query) => $query->where('locale', $language)])
|
||||
->withCount(['pressReleases as releases_count' => fn (Builder $query) => $companyFilter($query)])
|
||||
->orderByDesc('releases_count')
|
||||
->limit(8)
|
||||
->get()
|
||||
->map(function (Category $category): ?array {
|
||||
$translation = $category->translations->first();
|
||||
|
||||
if (! $translation) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'name' => $translation->name,
|
||||
'href' => $translation->slug ? route('kategorie', ['slug' => $translation->slug]) : route('web.home'),
|
||||
'count' => (int) ($category->releases_count ?? 0),
|
||||
];
|
||||
})
|
||||
->filter()
|
||||
->values();
|
||||
|
||||
return [
|
||||
'company' => $company,
|
||||
'referenceNow' => $referenceNow,
|
||||
'leadRelease' => $leadRelease,
|
||||
'releases' => $releases,
|
||||
'mostReadReleases' => $mostReadReleases,
|
||||
'categories' => $categories,
|
||||
'newsroomStats' => [
|
||||
'total' => $base()->count(),
|
||||
'recent' => $base()->where('published_at', '>=', $recentSince)->count(),
|
||||
'today' => $base()->whereDate('published_at', $referenceNow->toDateString())->count(),
|
||||
'totalHits' => (int) $base()->sum('hits'),
|
||||
'sinceYear' => $oldest ? (int) Carbon::parse($oldest)->format('Y') : null,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue