asdasd
This commit is contained in:
109
custom/apps/boersenchecker/api/index.php
Normal file
109
custom/apps/boersenchecker/api/index.php
Normal file
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once dirname(__DIR__, 3) . '/src/App/bootstrap.php';
|
||||
require_once dirname(__DIR__) . '/bootstrap.php';
|
||||
|
||||
$requestUri = (string) ($_SERVER['REQUEST_URI'] ?? '');
|
||||
$requestPath = (string) (parse_url($requestUri, PHP_URL_PATH) ?: '');
|
||||
$prefix = '/api/boersenchecker/index.php/';
|
||||
$relativePath = trim((string) ($_GET['path'] ?? ''), '/');
|
||||
|
||||
if ($relativePath === '') {
|
||||
$relativePath = str_starts_with($requestPath, $prefix)
|
||||
? substr($requestPath, strlen($prefix))
|
||||
: ltrim((string) ($_SERVER['PATH_INFO'] ?? ''), '/');
|
||||
}
|
||||
|
||||
$normalized = strtolower(trim($relativePath, '/'));
|
||||
|
||||
if ($normalized === 'v1/chart-data') {
|
||||
require __DIR__ . '/chart_data.php';
|
||||
return;
|
||||
}
|
||||
|
||||
if ($normalized === 'v1/cron/refresh-quotes' && strtoupper((string) ($_SERVER['REQUEST_METHOD'] ?? 'GET')) === 'POST') {
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
try {
|
||||
$result = module_fn('boersenchecker', 'scheduled_refresh_quotes', [
|
||||
'trigger_source' => 'cron',
|
||||
]);
|
||||
http_response_code(!empty($result['ok']) ? 200 : 500);
|
||||
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
} catch (\Throwable $exception) {
|
||||
http_response_code(500);
|
||||
echo json_encode([
|
||||
'ok' => false,
|
||||
'message' => $exception->getMessage(),
|
||||
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if ($normalized === 'v1/widget/refresh-quotes' && strtoupper((string) ($_SERVER['REQUEST_METHOD'] ?? 'GET')) === 'POST') {
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
try {
|
||||
$result = module_fn('boersenchecker', 'scheduled_refresh_quotes', [
|
||||
'trigger_source' => 'widget',
|
||||
]);
|
||||
http_response_code(!empty($result['ok']) ? 200 : 500);
|
||||
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
} catch (\Throwable $exception) {
|
||||
http_response_code(500);
|
||||
echo json_encode([
|
||||
'ok' => false,
|
||||
'message' => $exception->getMessage(),
|
||||
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if ($normalized === 'v1/widget/status' && strtoupper((string) ($_SERVER['REQUEST_METHOD'] ?? 'GET')) === 'GET') {
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
try {
|
||||
$pdo = module_fn('boersenchecker', 'pdo');
|
||||
$quoteTable = module_fn('boersenchecker', 'table', 'quotes');
|
||||
$instrumentTable = module_fn('boersenchecker', 'table', 'instruments');
|
||||
$positionTable = module_fn('boersenchecker', 'table', 'positions');
|
||||
$settings = modules()->settings('boersenchecker');
|
||||
|
||||
$latestAt = $pdo->query('SELECT MAX(quoted_at) FROM ' . $quoteTable)->fetchColumn();
|
||||
$countStmt = $pdo->query(
|
||||
'SELECT COUNT(DISTINCT i.id)
|
||||
FROM ' . $positionTable . ' p
|
||||
INNER JOIN ' . $instrumentTable . ' i ON i.id = p.instrument_id
|
||||
WHERE i.symbol IS NOT NULL
|
||||
AND i.symbol <> \'\''
|
||||
);
|
||||
$trackedCount = (int) ($countStmt->fetchColumn() ?: 0);
|
||||
|
||||
$statusText = $latestAt
|
||||
? 'Letzter Kursabruf: ' . date('d.m.Y H:i', strtotime((string) $latestAt)) . ' · ' . $trackedCount . ' Titel'
|
||||
: 'Noch keine gespeicherten Kurse vorhanden.';
|
||||
|
||||
echo json_encode([
|
||||
'ok' => true,
|
||||
'data' => [
|
||||
'status_text' => $statusText,
|
||||
'latest_quoted_at' => $latestAt,
|
||||
'tracked_instruments' => $trackedCount,
|
||||
'report_currency' => (string) ($settings['report_currency'] ?? 'EUR'),
|
||||
],
|
||||
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
} catch (\Throwable $exception) {
|
||||
http_response_code(500);
|
||||
echo json_encode([
|
||||
'ok' => false,
|
||||
'message' => $exception->getMessage(),
|
||||
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
http_response_code(404);
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode([
|
||||
'ok' => false,
|
||||
'message' => 'Ressource nicht gefunden.',
|
||||
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
Reference in New Issue
Block a user