diff --git a/frontend/package.json b/frontend/package.json index 0c16446..c3f35d0 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -3,6 +3,8 @@ "version": "1.0.0", "dependencies": { "express": "^4.18.2", - "node-fetch": "^2.6.7" + "node-fetch": "^2.6.7", + "swagger-jsdoc": "^6.2.8", + "swagger-ui-express":"^5.0.1" } } diff --git a/frontend/public/app.js b/frontend/public/app.js index 01bf016..f6d150e 100644 --- a/frontend/public/app.js +++ b/frontend/public/app.js @@ -10,9 +10,11 @@ async function login() { body: JSON.stringify({ username, password }), }); - const data = await res.json(); - token = data.token; + if (!res.ok) return document.getElementById("output").textContent = "Login failed."; + token = (await res.json()).token; + if (!token) return document.getElementById("output").textContent = "No token received."; document.getElementById("output").textContent = "Logged in!"; + document.getElementById("login-section").style.display = "none"; } async function placeOrder() { @@ -31,3 +33,29 @@ async function placeOrder() { const text = await res.text(); document.getElementById("output").textContent = text; } + +async function getOrderHistory() { + const res = await fetch("/api/orders/history", { + method: "GET", + headers: { + "Authorization": "Bearer " + token + } + }); + + if (!res.ok) { + document.getElementById("output").textContent = "Failed to fetch order history."; + return; + } + + const orders = await res.json(); + let output = `Order History for ${orders.username || document.getElementById("username").value}:\n`; + const orderList = orders.orders || orders; + if (orderList.length === 0) { + output += "No orders found.\n"; + } else { + orderList.forEach(order => { + output += `Order #${order.orderId}: ${order.productId} x ${order.quantity}\n`; + }); + } + document.getElementById("output").textContent = output; +} diff --git a/frontend/public/index.html b/frontend/public/index.html index c0fb3c4..31aa74c 100644 --- a/frontend/public/index.html +++ b/frontend/public/index.html @@ -3,18 +3,129 @@