Optimierung 4.3/4.4 (Newsrooms): Page-Data-Service + Invokable Controller
Newsrooms-Verzeichnis-Closure in NewsroomsPageData + Web\NewsroomsController verschoben. Verhalten unveraendert, 92 Web-Tests gruen. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
fef6bbc9a6
commit
a2179ea47f
3 changed files with 97 additions and 61 deletions
78
app/Services/Web/NewsroomsPageData.php
Normal file
78
app/Services/Web/NewsroomsPageData.php
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services\Web;
|
||||
|
||||
use App\Models\Company;
|
||||
use App\Models\PressRelease;
|
||||
use App\Scopes\PortalScope;
|
||||
use App\Support\EditorialClock;
|
||||
use App\Support\WebPortalContext;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
/**
|
||||
* Datenaufbereitung des Newsrooms-Verzeichnisses je Ausgabe: alle aktiven
|
||||
* Unternehmen mit veröffentlichten Meldungen (optional gefiltert per `q`).
|
||||
*/
|
||||
class NewsroomsPageData
|
||||
{
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function build(WebPortalContext $context): array
|
||||
{
|
||||
$portalValues = $context->portalValues;
|
||||
$language = $context->language;
|
||||
$referenceNow = EditorialClock::reference($portalValues, $language);
|
||||
$recentSince = $referenceNow->copy()->subDays(30);
|
||||
|
||||
$term = trim((string) request('q', ''));
|
||||
|
||||
$published = static function (Builder $query) use ($portalValues, $language): void {
|
||||
$query
|
||||
->forPortalValues($portalValues)
|
||||
->forLanguage($language)
|
||||
->visibleInArchive();
|
||||
};
|
||||
|
||||
$activeCompanies = static fn (): Builder => Company::query()
|
||||
->withoutGlobalScope(PortalScope::class)
|
||||
->whereIn('portal', $portalValues)
|
||||
->where('is_active', true)
|
||||
->whereHas('pressReleases', fn (Builder $query) => $published($query));
|
||||
|
||||
$newsrooms = $activeCompanies()
|
||||
->when($term !== '', fn (Builder $query) => $query->whereLike('name', '%'.$term.'%'))
|
||||
->withCount([
|
||||
'pressReleases as releases_count' => fn (Builder $query) => $published($query),
|
||||
'pressReleases as recent_count' => function (Builder $query) use ($published, $recentSince): void {
|
||||
$published($query);
|
||||
$query->where('published_at', '>=', $recentSince);
|
||||
},
|
||||
])
|
||||
->withMax(['pressReleases as latest_published_at' => fn (Builder $query) => $published($query)], 'published_at')
|
||||
->orderByDesc('recent_count')
|
||||
->orderByDesc('releases_count')
|
||||
->orderBy('name')
|
||||
->paginate(24)
|
||||
->withQueryString();
|
||||
|
||||
$archiveTotal = PressRelease::query()
|
||||
->withoutGlobalScope(PortalScope::class)
|
||||
->tap(fn (Builder $query) => $published($query))
|
||||
->count();
|
||||
|
||||
return [
|
||||
'newsrooms' => $newsrooms,
|
||||
'term' => $term,
|
||||
'referenceNow' => $referenceNow,
|
||||
'totalNewsrooms' => (clone $activeCompanies())->count(),
|
||||
'activeNewsroomsCount' => $activeCompanies()
|
||||
->whereHas('pressReleases', function (Builder $query) use ($published, $recentSince): void {
|
||||
$published($query);
|
||||
$query->where('published_at', '>=', $recentSince);
|
||||
})
|
||||
->count(),
|
||||
'archiveTotal' => $archiveTotal,
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue