fedi-feed-router/app/Http/Controllers/Api/V1/DashboardController.php

48 lines
1.5 KiB
PHP
Raw Permalink Normal View History

2025-08-02 15:20:09 +02:00
<?php
namespace App\Http\Controllers\Api\V1;
use App\Services\DashboardStatsService;
use App\Support\DateRange;
2025-08-02 15:20:09 +02:00
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
2025-08-02 15:20:09 +02:00
class DashboardController extends BaseController
{
public function __construct(
private DashboardStatsService $dashboardStatsService
) {}
/**
* Get dashboard statistics
*/
public function stats(Request $request): JsonResponse
{
$validated = $request->validate([
'from' => ['nullable', 'date', 'required_with:to'],
'to' => ['nullable', 'date', 'after_or_equal:from', 'required_with:from'],
]);
$range = isset($validated['from'], $validated['to'])
? new DateRange(
Carbon::parse($validated['from'])->startOfDay(),
Carbon::parse($validated['to'])->endOfDay(),
)
: DateRange::preset('today');
2025-08-10 01:26:56 +02:00
2025-08-02 15:20:09 +02:00
try {
return $this->sendResponse([
'article_stats' => $this->dashboardStatsService->getStats($range),
'system_stats' => $this->dashboardStatsService->getSystemStats(),
'range' => [
'from' => $range->from->toDateString(),
'to' => $range->to->toDateString(),
],
2025-08-02 15:20:09 +02:00
]);
} catch (\Exception $e) {
return $this->sendError('Failed to fetch dashboard stats: '.$e->getMessage(), [], 500);
2025-08-02 15:20:09 +02:00
}
}
2025-08-10 01:26:56 +02:00
}