Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 05dc6b11 authored by 65160270's avatar 65160270
Browse files

update-address

parent fb7d0c86
No related branches found
No related tags found
No related merge requests found
......@@ -42,44 +42,39 @@ router.get('/history', isAuthenticated, async (req, res) => {
}
});
// ดูรายละเอียดออเดอร์ (เฉพาะผู้ที่ Login)
router.get('/detail/:orderId', async (req, res) => {
// อัปเดตที่อยู่จัดส่งของคำสั่งซื้อ
router.put('/update/:orderId', async (req, res) => {
try {
console.log("Checking Order Details...");
console.log("Session ID:", req.session.id);
console.log("Order ID:", req.params.orderId);
const { orderId } = req.params;
const { shipping_address } = req.body;
const [orders] = await pool.query(
'SELECT * FROM orders WHERE id = ? AND session_id = ?',
[req.params.orderId, req.session.id]
);
console.log("Orders Result:", orders);
console.log("Updating Shipping Address...");
console.log("Session ID:", req.session.id);
console.log("Order ID:", orderId);
console.log("New Address:", shipping_address);
if (orders.length === 0) {
return res.status(404).json({ message: 'Order not found' });
if (!shipping_address) {
return res.status(400).json({ message: "กรุณากรอกที่อยู่จัดส่ง" });
}
const order = orders[0];
const [items] = await pool.query(
`SELECT
order_items.*, products.name, products.image_url
FROM order_items
JOIN products ON order_items.product_id = products.id
WHERE order_items.order_id = ?`,
[req.params.orderId]
// อัปเดตที่อยู่เฉพาะของคำสั่งซื้อที่เป็นของ session นั้นๆ
const [result] = await pool.query(
"UPDATE orders SET shipping_address = ? WHERE id = ? AND session_id = ?",
[shipping_address, orderId, req.session.id]
);
console.log("Order Items:", items);
if (result.affectedRows === 0) {
return res.status(404).json({ message: "ไม่พบคำสั่งซื้อนี้ หรือไม่มีสิทธิ์แก้ไข" });
}
res.json({ order, items });
res.json({ message: "อัปเดตที่อยู่สำเร็จ!" });
} catch (error) {
console.error("ERROR:", error);
res.status(500).json({ message: 'Something went wrong.', error: error.message });
res.status(500).json({ message: "เกิดข้อผิดพลาด", error: error.message });
}
});
// ป้องกันไม่ให้เข้า Checkout ถ้าไม่ได้ Login
router.get('/checkout', isAuthenticated, async (req, res) => {
try {
......
......@@ -11,7 +11,12 @@
<div class="info-section">
<h3>Shipping Address</h3>
<p><%= order.shipping_address %></p>
<p>
<span id="shipping-address"><%= order.shipping_address %></span>
<input type="text" id="new-address" value="<%= order.shipping_address %>" style="display:none;">
</p>
<button id="edit-btn">แก้ไขที่อยู่</button>
<button id="save-btn" style="display:none;">บันทึก</button>
</div>
</div>
......@@ -38,3 +43,37 @@
</div>
</div>
<%- include('partials/footer') %>
<script>
document.getElementById("edit-btn").addEventListener("click", function() {
document.getElementById("new-address").style.display = "inline";
document.getElementById("save-btn").style.display = "inline";
document.getElementById("edit-btn").style.display = "none"; // ซ่อนปุ่มแก้ไข
});
document.getElementById("save-btn").addEventListener("click", async function() {
const newAddress = document.getElementById("new-address").value.trim();
if (!newAddress) {
alert("กรุณากรอกที่อยู่ใหม่");
return;
}
const orderId = "<%= order.id %>";
const response = await fetch(`/order/update/${orderId}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ shipping_address: newAddress })
});
if (response.ok) {
document.getElementById("shipping-address").innerText = newAddress;
document.getElementById("new-address").style.display = "none";
document.getElementById("save-btn").style.display = "none";
document.getElementById("edit-btn").style.display = "inline"; // แสดงปุ่มแก้ไขกลับมา
alert("อัปเดตที่อยู่สำเร็จ!");
} else {
alert("เกิดข้อผิดพลาด!");
}
});
</script>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment