Step 3 migration: frontend reads API with local fallback

This commit is contained in:
Ponte
2026-02-11 15:00:40 +01:00
parent 89d9275e1a
commit adc7ec193e
7 changed files with 177 additions and 2 deletions

View File

@@ -122,6 +122,60 @@ async function getCatalogTree() {
return Array.from(brandMap.values());
}
async function getCatalogFull() {
const brandConsoleRows = await pool.query(`
SELECT
b.name AS brand_name,
c.name AS console_name
FROM brands b
LEFT JOIN consoles c ON c.brand_id = b.id
ORDER BY b.name ASC, c.name ASC;
`);
const gameRows = await pool.query(`
SELECT
c.name AS console_name,
g.id::text AS id,
g.title,
g.genre,
g.publisher,
g.release_year,
g.estimated_value,
g.loaned_to,
g.created_at
FROM games g
JOIN consoles c ON c.id = g.console_id
ORDER BY g.created_at DESC;
`);
const brands = {};
for (const row of brandConsoleRows.rows) {
const brand = row.brand_name;
brands[brand] = brands[brand] || [];
if (row.console_name && !brands[brand].includes(row.console_name)) {
brands[brand].push(row.console_name);
}
}
const gamesByConsole = {};
for (const row of gameRows.rows) {
const consoleName = row.console_name;
gamesByConsole[consoleName] = gamesByConsole[consoleName] || [];
gamesByConsole[consoleName].push({
id: row.id,
title: row.title,
genre: row.genre || "",
publisher: row.publisher || "",
year: row.release_year || null,
value: row.estimated_value != null ? Number(row.estimated_value) : null,
loanedTo: row.loaned_to || "",
createdAt: row.created_at,
});
}
return { brands, gamesByConsole };
}
async function handleRequest(request, response) {
const url = new URL(request.url || "/", `http://${request.headers.host || "localhost"}`);
@@ -165,6 +219,16 @@ async function handleRequest(request, response) {
return;
}
if (request.method === "GET" && url.pathname === "/api/catalog/full") {
try {
const full = await getCatalogFull();
sendJson(response, 200, full);
} catch (error) {
sendJson(response, 500, { status: "error", message: error.message });
}
return;
}
sendJson(response, 404, {
status: "not_found",
message: "Route not found",