Track Filament

This commit is contained in:
Hickmeister
2025-01-05 02:28:51 +00:00
parent e31bd43eaa
commit 5a11305caf
7 changed files with 1664 additions and 2 deletions

View File

@@ -0,0 +1,68 @@
<?php
require '../../vendor/autoload.php';
require '../db.php';
require_once '../envLoader.php';
loadEnv(__DIR__ . '/../../.env');
// Check if the user is logged in
if (!isset($_SESSION['userId'])) {
echo json_encode(['status' => 'error', 'message' => 'User not authenticated.']);
exit;
}
header('Content-Type: application/json');
// Fetch all filament prices from the database
try {
$stmt = $pdo->query("
SELECT
ft.filamentName,
ft.brand,
ft.material,
ft.color,
fp.price,
fp.recordedAt
FROM
filamentTracker ft
JOIN
filamentPriceHistory fp ON ft.id = fp.filamentId
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'],
'prices' => []
];
}
$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()
]);
}
?>