76 lines
2.3 KiB
PHP
76 lines
2.3 KiB
PHP
<?php
|
|
require '../../vendor/autoload.php';
|
|
require '../db.php';
|
|
require_once '../envLoader.php';
|
|
loadEnv(__DIR__ . '/../../.env');
|
|
|
|
include '../src/session_check.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
try {
|
|
// Fetch all filament prices with the latest discount
|
|
$stmt = $pdo->query("
|
|
SELECT ft.filamentName,
|
|
ft.brand,
|
|
ft.material,
|
|
ft.color,
|
|
ft.amazonUrl,
|
|
fp.price,
|
|
fp.currentDiscount,
|
|
fp.recordedAt
|
|
FROM filamentTracker ft
|
|
JOIN filamentPriceHistory fp ON ft.id = fp.filamentId
|
|
JOIN (
|
|
SELECT filamentId, recordedAt
|
|
FROM (
|
|
SELECT filamentId, recordedAt,
|
|
ROW_NUMBER() OVER (PARTITION BY filamentId ORDER BY recordedAt DESC) as rn
|
|
FROM filamentPriceHistory
|
|
) ranked
|
|
WHERE rn <= 380
|
|
) filtered ON fp.filamentId = filtered.filamentId AND fp.recordedAt = filtered.recordedAt
|
|
ORDER BY ft.filamentName, fp.recordedAt ASC
|
|
");
|
|
|
|
$filaments = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$result = [];
|
|
|
|
// Format data for charts (grouped by filament)
|
|
foreach ($filaments as $filament) {
|
|
$name = $filament['filamentName'];
|
|
|
|
if (!isset($result[$name])) {
|
|
$result[$name] = [
|
|
'brand' => $filament['brand'],
|
|
'material' => $filament['material'],
|
|
'color' => $filament['color'],
|
|
'amazonUrl' => $filament['amazonUrl'],
|
|
'prices' => [],
|
|
'currentDiscount' => $filament['currentDiscount'] ? json_decode($filament['currentDiscount'], true) : [
|
|
'discount' => ['value' => 0, 'type' => 'none'],
|
|
'voucher' => ['value' => 0, 'type' => 'none']
|
|
] // Decode JSON format and provide fallback
|
|
];
|
|
}
|
|
|
|
$result[$name]['prices'][] = [
|
|
'price' => $filament['price'],
|
|
'recordedAt' => $filament['recordedAt']
|
|
];
|
|
}
|
|
|
|
// Return as JSON
|
|
echo json_encode([
|
|
'status' => 'success',
|
|
'data' => $result
|
|
], JSON_PRETTY_PRINT);
|
|
} catch (PDOException $e) {
|
|
echo json_encode([
|
|
'status' => 'error',
|
|
'message' => 'Failed to fetch filament prices: ' . $e->getMessage()
|
|
]);
|
|
}
|
|
?>
|