Gitlab@Informatics

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

update-address

parent fb7d0c86
Branches
No related tags found
No related merge requests found
...@@ -42,44 +42,39 @@ router.get('/history', isAuthenticated, async (req, res) => { ...@@ -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 { try {
console.log("Checking Order Details..."); const { orderId } = req.params;
console.log("Session ID:", req.session.id); const { shipping_address } = req.body;
console.log("Order ID:", req.params.orderId);
const [orders] = await pool.query( console.log("Updating Shipping Address...");
'SELECT * FROM orders WHERE id = ? AND session_id = ?', console.log("Session ID:", req.session.id);
[req.params.orderId, req.session.id] console.log("Order ID:", orderId);
); console.log("New Address:", shipping_address);
console.log("Orders Result:", orders);
if (orders.length === 0) { if (!shipping_address) {
return res.status(404).json({ message: 'Order not found' }); return res.status(400).json({ message: "กรุณากรอกที่อยู่จัดส่ง" });
} }
const order = orders[0]; // อัปเดตที่อยู่เฉพาะของคำสั่งซื้อที่เป็นของ session นั้นๆ
const [result] = await pool.query(
const [items] = await pool.query( "UPDATE orders SET shipping_address = ? WHERE id = ? AND session_id = ?",
`SELECT [shipping_address, orderId, req.session.id]
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]
); );
console.log("Order Items:", items); if (result.affectedRows === 0) {
return res.status(404).json({ message: "ไม่พบคำสั่งซื้อนี้ หรือไม่มีสิทธิ์แก้ไข" });
}
res.json({ order, items }); res.json({ message: "อัปเดตที่อยู่สำเร็จ!" });
} catch (error) { } catch (error) {
console.error("ERROR:", 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 // ป้องกันไม่ให้เข้า Checkout ถ้าไม่ได้ Login
router.get('/checkout', isAuthenticated, async (req, res) => { router.get('/checkout', isAuthenticated, async (req, res) => {
try { try {
......
...@@ -11,7 +11,12 @@ ...@@ -11,7 +11,12 @@
<div class="info-section"> <div class="info-section">
<h3>Shipping Address</h3> <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>
</div> </div>
...@@ -38,3 +43,37 @@ ...@@ -38,3 +43,37 @@
</div> </div>
</div> </div>
<%- include('partials/footer') %> <%- 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