Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 11dcf971 authored by 65160270's avatar 65160270
Browse files

update-address

parent 134cc1ca
No related branches found
No related tags found
No related merge requests found
...@@ -38,6 +38,8 @@ app.use(express.urlencoded({ extended: true })); ...@@ -38,6 +38,8 @@ app.use(express.urlencoded({ extended: true }));
// Middleware เช็ค Session // Middleware เช็ค Session
app.use((req, res, next) => { app.use((req, res, next) => {
console.log("Session Middleware Checked"); console.log("Session Middleware Checked");
console.log("🔹 Session ID:", req.sessionID);
console.log("🔹 Session Data:", req.session);
next(); next();
}); });
......
...@@ -11,8 +11,17 @@ function isAuthenticated(req, res, next) { ...@@ -11,8 +11,17 @@ function isAuthenticated(req, res, next) {
} }
} }
router.use((req, res, next) => {
console.log("🔹 Session ID:", req.session.id);
console.log("🔹 Session Data:", req.session);
next();
});
// แสดงประวัติออเดอร์ (เฉพาะผู้ที่ Login) // แสดงประวัติออเดอร์ (เฉพาะผู้ที่ Login)
router.get('/history', isAuthenticated, async (req, res) => { router.get('/history', isAuthenticated, async (req, res) => {
if (!req.session.id) {
return res.status(400).json({ message: "Session ID not found. Please login again." });
}
try { try {
const [orders] = await pool.query( const [orders] = await pool.query(
`SELECT `SELECT
...@@ -22,9 +31,8 @@ router.get('/history', isAuthenticated, async (req, res) => { ...@@ -22,9 +31,8 @@ router.get('/history', isAuthenticated, async (req, res) => {
orders.shipping_address, orders.shipping_address,
orders.created_at, orders.created_at,
GROUP_CONCAT( GROUP_CONCAT(
CONCAT( CONCAT(products.name, ' x ', order_items.quantity, ' (฿', order_items.price, ')')
products.name, ' x ', order_items.quantity, ' (฿', order_items.price, ')' SEPARATOR ', '
) SEPARATOR ', '
) as items_detail ) as items_detail
FROM orders FROM orders
JOIN order_items ON orders.id = order_items.order_id JOIN order_items ON orders.id = order_items.order_id
...@@ -45,8 +53,12 @@ router.get('/history', isAuthenticated, async (req, res) => { ...@@ -45,8 +53,12 @@ router.get('/history', isAuthenticated, async (req, res) => {
// แสดงรายละเอียดออเดอร์ (เฉพาะผู้ที่ Login) // แสดงรายละเอียดออเดอร์ (เฉพาะผู้ที่ Login)
router.get('/detail/:orderId', isAuthenticated, async (req, res) => { router.get('/detail/:orderId', isAuthenticated, async (req, res) => {
try { try {
if (!req.session.id) {
return res.status(400).json({ message: "Session ID not found. Please login again." });
}
const { orderId } = req.params; const { orderId } = req.params;
console.log(`Fetching order details for order ID: ${orderId}, Session ID: ${req.session.id}`); console.log(`🔹 Fetching order ID: ${orderId}, Session ID: ${req.session.id}`);
const [orderResults] = await pool.query( const [orderResults] = await pool.query(
`SELECT id, total_amount, status, shipping_address, created_at `SELECT id, total_amount, status, shipping_address, created_at
...@@ -55,23 +67,14 @@ router.get('/detail/:orderId', isAuthenticated, async (req, res) => { ...@@ -55,23 +67,14 @@ router.get('/detail/:orderId', isAuthenticated, async (req, res) => {
); );
if (orderResults.length === 0) { if (orderResults.length === 0) {
console.log("Order not found or user has no permission."); console.log("Order not found or no permission.");
return res.status(404).json({ message: "ไม่พบคำสั่งซื้อ หรือไม่มีสิทธิ์เข้าถึง" }); return res.status(404).json({ message: "ไม่พบคำสั่งซื้อ หรือไม่มีสิทธิ์เข้าถึง" });
} }
const order = orderResults[0]; console.log("Order found:", orderResults[0]);
res.json(orderResults[0]);
const [items] = await pool.query(
`SELECT products.name, products.image_url, order_items.quantity, order_items.price
FROM order_items
JOIN products ON order_items.product_id = products.id
WHERE order_items.order_id = ?`,
[orderId]
);
res.render('order-detail', { order, items });
} catch (error) { } catch (error) {
console.error("ERROR fetching order details:", error); // ดู Error Log console.error("Error fetching order:", error);
res.status(500).json({ message: "Something went wrong.", error: error.message }); res.status(500).json({ message: "Something went wrong.", error: error.message });
} }
}); });
...@@ -132,6 +135,12 @@ router.get('/checkout', isAuthenticated, async (req, res) => { ...@@ -132,6 +135,12 @@ router.get('/checkout', isAuthenticated, async (req, res) => {
// บันทึกออเดอร์ // บันทึกออเดอร์
router.post('/create', isAuthenticated, async (req, res) => { router.post('/create', isAuthenticated, async (req, res) => {
if (!req.session.id) {
return res.status(400).json({ message: "Session ID not found. Please login again." });
}
console.log("🔹 Creating order with Session ID:", req.session.id);
const { address } = req.body; const { address } = req.body;
const conn = await pool.getConnection(); const conn = await pool.getConnection();
try { try {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment