Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 9d7def84 authored by 65160270's avatar 65160270
Browse files

update-address

parent a5dd9c34
No related branches found
No related tags found
No related merge requests found
...@@ -45,11 +45,9 @@ router.get('/history', isAuthenticated, async (req, res) => { ...@@ -45,11 +45,9 @@ 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 {
const sessionId = req.session.id;
const [orders] = await pool.query( const [orders] = await pool.query(
`SELECT * FROM orders WHERE id = ? AND session_id = ?`, 'SELECT * FROM orders WHERE id = ? AND session_id = ?',
[req.params.orderId, sessionId] [req.params.orderId, req.session.id]
); );
if (orders.length === 0) { if (orders.length === 0) {
...@@ -59,7 +57,8 @@ router.get('/detail/:orderId', isAuthenticated, async (req, res) => { ...@@ -59,7 +57,8 @@ router.get('/detail/:orderId', isAuthenticated, async (req, res) => {
const order = orders[0]; const order = orders[0];
const [items] = await pool.query( const [items] = await pool.query(
`SELECT order_items.*, products.name, products.image_url `SELECT
order_items.*, products.name, products.image_url
FROM order_items FROM order_items
JOIN products ON order_items.product_id = products.id JOIN products ON order_items.product_id = products.id
WHERE order_items.order_id = ?`, WHERE order_items.order_id = ?`,
...@@ -144,35 +143,6 @@ router.post('/create', isAuthenticated, async (req, res) => { ...@@ -144,35 +143,6 @@ router.post('/create', isAuthenticated, async (req, res) => {
} }
}); });
// อัปเดตที่อยู่จัดส่งของออเดอร์
router.put('/update-address/:orderId', isAuthenticated, async (req, res) => {
const { shipping_address } = req.body;
const orderId = req.params.orderId;
try {
// ตรวจสอบว่าออเดอร์เป็นของผู้ใช้ปัจจุบัน
const [order] = await pool.query(
'SELECT * FROM orders WHERE id = ? AND session_id = ?',
[orderId, req.session.id]
);
if (order.length === 0) {
return res.status(404).json({ success: false, message: 'Order not found or unauthorized' });
}
// อัปเดตที่อยู่จัดส่ง
await pool.query(
'UPDATE orders SET shipping_address = ? WHERE id = ?',
[shipping_address, orderId]
);
res.json({ success: true, message: 'Shipping address updated successfully' });
} catch (error) {
console.error(error);
res.status(500).json({ success: false, message: 'Error updating shipping address' });
}
});
router.get('/confirmation', (req, res) => { router.get('/confirmation', (req, res) => {
res.render('confirmation'); res.render('confirmation');
}); });
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
<% } else { %> <% } else { %>
<div class="order-summary"> <div class="order-summary">
<h3>Order Summary</h3> <h3>Order Summary</h3>
<% if (cartItems.length > 0) { %>
<% cartItems.forEach(item => { %> <% cartItems.forEach(item => { %>
<div class="order-item"> <div class="order-item">
<span><%= item.name %> x <%= item.quantity %></span> <span><%= item.name %> x <%= item.quantity %></span>
...@@ -19,9 +18,6 @@ ...@@ -19,9 +18,6 @@
<strong>Total:</strong> <strong>Total:</strong>
<strong>฿<%= total.toLocaleString() %></strong> <strong>฿<%= total.toLocaleString() %></strong>
</div> </div>
<% } else { %>
<p>Your cart is empty.</p>
<% } %>
</div> </div>
<form action="/order/create" method="POST" class="checkout-form"> <form action="/order/create" method="POST" class="checkout-form">
...@@ -31,34 +27,6 @@ ...@@ -31,34 +27,6 @@
</div> </div>
<button type="submit" style="margin: 0; padding: 0.5rem 1rem;" > Place Order</button> <button type="submit" style="margin: 0; padding: 0.5rem 1rem;" > Place Order</button>
</form> </form>
<h3>Your Orders</h3>
<% if (orders.length > 0) { %>
<table border="1">
<thead>
<tr>
<th>Order ID</th>
<th>Total Amount</th>
<th>Status</th>
<th>Shipping Address</th>
<th>Created At</th>
</tr>
</thead>
<tbody>
<% orders.forEach(order => { %>
<tr>
<td><%= order.id %></td>
<td>฿<%= order.total_amount.toLocaleString() %></td>
<td><%= order.status %></td>
<td><%= order.shipping_address %></td>
<td><%= order.created_at %></td>
</tr>
<% }) %>
</tbody>
</table>
<% } else { %>
<p>No orders found.</p>
<% } %>
<% } %> <% } %>
</div> </div>
......
...@@ -11,11 +11,7 @@ ...@@ -11,11 +11,7 @@
<div class="info-section"> <div class="info-section">
<h3>Shipping Address</h3> <h3>Shipping Address</h3>
<p id="currentAddress"><%= order.shipping_address %></p> <p><%= order.shipping_address %></p>
<!-- Input สำหรับแก้ไขที่อยู่ -->
<input type="text" id="newAddress" placeholder="Enter new address">
<button onclick="updateAddress(<%= order.id %>)">Update Address</button>
</div> </div>
</div> </div>
...@@ -41,32 +37,4 @@ ...@@ -41,32 +37,4 @@
<a href="/" class="continue-shopping">Continue Shopping</a> <a href="/" class="continue-shopping">Continue Shopping</a>
</div> </div>
</div> </div>
<script>
function updateAddress(orderId) {
const newAddress = document.getElementById("newAddress").value;
if (!newAddress.trim()) {
alert("Please enter a valid address.");
return;
}
fetch(`/order/update-address/${orderId}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ shipping_address: newAddress })
})
.then(response => response.json())
.then(data => {
if (data.success) {
document.getElementById("currentAddress").innerText = newAddress;
alert("Address updated successfully!");
} else {
alert("Failed to update address.");
}
})
.catch(error => console.error("Error:", error));
}
</script>
<%- include('partials/footer') %> <%- include('partials/footer') %>
\ 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