Added price tracking cards

This commit is contained in:
Hickmeister
2025-01-05 03:39:59 +00:00
parent 5a11305caf
commit d96fa63298
4 changed files with 316 additions and 123 deletions

View File

@@ -4,31 +4,32 @@ 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;
}
include '../src/session_check.php';
header('Content-Type: application/json');
// Fetch all filament prices from the database
try {
// Fetch all filament prices with a limit of 180 entries per filament using JOIN
$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
SELECT ft.filamentName,
ft.brand,
ft.material,
ft.color,
ft.amazonUrl,
fp.price,
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 <= 180
) filtered ON fp.filamentId = filtered.filamentId AND fp.recordedAt = filtered.recordedAt
ORDER BY ft.filamentName, fp.recordedAt ASC
");
$filaments = $stmt->fetchAll(PDO::FETCH_ASSOC);
@@ -44,6 +45,7 @@ try {
'brand' => $filament['brand'],
'material' => $filament['material'],
'color' => $filament['color'],
'amazonUrl' => $filament['amazonUrl'],
'prices' => []
];
}