Track Filament
This commit is contained in:
68
src/filamentTracker/getFilamentPrices.php
Normal file
68
src/filamentTracker/getFilamentPrices.php
Normal 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()
|
||||
]);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user