47 lines
1.5 KiB
PHP
47 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\Services\DashboardStatsService;
|
|
use App\Support\DateRange;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
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');
|
|
|
|
try {
|
|
return $this->sendResponse([
|
|
'article_stats' => $this->dashboardStatsService->getStats($range),
|
|
'system_stats' => $this->dashboardStatsService->getSystemStats(),
|
|
'range' => [
|
|
'from' => $range->from->toDateString(),
|
|
'to' => $range->to->toDateString(),
|
|
],
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return $this->sendError('Failed to fetch dashboard stats: '.$e->getMessage(), [], 500);
|
|
}
|
|
}
|
|
}
|