From 803b0bdfdbba76136738ed1e4668297372e40b2f Mon Sep 17 00:00:00 2001
From: 65160381 <65160381@go.buu.ac.th>
Date: Tue, 25 Mar 2025 04:02:01 +0700
Subject: [PATCH] 9.8

---
 app.js            | 16 ++++++++++++++++
 public/index.html | 38 +++++++++++---------------------------
 2 files changed, 27 insertions(+), 27 deletions(-)

diff --git a/app.js b/app.js
index 4b1d6b9..c61e7dd 100644
--- a/app.js
+++ b/app.js
@@ -149,6 +149,22 @@ app.post('/api/products', async (req, res) => {
     }
 });
 
+// ตัวอย่างของการดึงข้อมูลสินค้าใน Node.js (Express)
+app.get('/api/products', async (req, res) => {
+    const searchQuery = req.query.search || ''; // รับคำค้นจาก query string
+
+    try {
+        // ดึงข้อมูลสินค้าจากฐานข้อมูล (สามารถเพิ่มการค้นหาจาก searchQuery ได้)
+        const query = `SELECT product_id, product_name, product_img, product_price FROM products WHERE product_name LIKE ?`;
+        const [rows] = await pool.query(query, [`%${searchQuery}%`]);
+
+        res.json(rows);  // ส่งข้อมูลสินค้าเป็น JSON
+    } catch (error) {
+        console.error('Error fetching products:', error);
+        res.status(500).send('Error fetching products');
+    }
+});
+
 app.get('/api/getUser', (req, res) => {
     if (req.session.user) {
         res.json({
diff --git a/public/index.html b/public/index.html
index 214d552..6366563 100644
--- a/public/index.html
+++ b/public/index.html
@@ -46,16 +46,18 @@
                 const response = await fetch(`/api/products?search=${searchQuery}`);
                 const products = await response.json();
                 console.log('Fetched Products:', products);  // เพิ่มการตรวจสอบข้อมูล
-                renderProducts(products);
+                renderProducts(products);  // เรียกฟังก์ชัน renderProducts เพื่อนำข้อมูลไปแสดง
             } catch (error) {
                 console.error('Error fetching products:', error);
             }
         }
 
+        // ฟังก์ชันที่ใช้ในการแสดงสินค้าบนหน้าเว็บ
         function renderProducts(products) {
             const productContainer = document.querySelector('.products');
-            productContainer.innerHTML = '';
+            productContainer.innerHTML = '';  // เคลียร์รายการสินค้าก่อน
 
+            // วนลูปแสดงข้อมูลสินค้า
             products.forEach(product => {
                 const productDiv = document.createElement('div');
                 productDiv.classList.add('product');
@@ -69,13 +71,13 @@
             });
         }
 
-        // Search products function
+        // ฟังก์ชันค้นหาสินค้า
         function searchProducts() {
             const searchQuery = document.getElementById('search').value.toLowerCase();
             fetchProducts(searchQuery);
         }
 
-        // Add item to the cart
+        // ฟังก์ชันที่เพิ่มสินค้าไปยังตะกร้า
         function addToCart(productId, productName, productPrice, productImg) {
             let cart = JSON.parse(localStorage.getItem('cart')) || [];
             const existingItem = cart.find(item => item.id === productId);
@@ -97,7 +99,7 @@
             alert(`${productName} added to cart!`);
         }
 
-        // Update cart item count
+        // ฟังก์ชันอัปเดตจำนวนสินค้าที่อยู่ในตะกร้า
         function updateCart() {
             let cart = JSON.parse(localStorage.getItem('cart')) || [];
             const cartItemCount = document.getElementById('cart-item-count');
@@ -105,29 +107,11 @@
             cartItemCount.textContent = totalQuantity;
         }
 
-        // Display user's email from session
-        async function displayUsername() {
-        try {
-            const response = await fetch('/api/getUser');  // Endpoint to get the user's email
-            if (response.status === 401) {
-                console.log('User not logged in');
-                document.getElementById('welcome-message').innerText = 'Please log in';
-            } else {
-                const user = await response.json();
-                document.getElementById('welcome-message').innerText = `Welcome, ${user.email}`; // Display email here
-            }
-        } catch (error) {
-            console.log('Error fetching user info:', error);
-        }
-        }
-
         // เรียกใช้ฟังก์ชันเมื่อหน้าเว็บโหลดเสร็จ
-        window.onload = displayUsername;
-
-        // Initially load products, cart item count, and user info
-        fetchProducts();  // Fetch all products initially
-        updateCart(); // Update cart count when page loads
-        displayUsername(); // Display user's email when page loads
+        window.onload = function() {
+            fetchProducts();  // ดึงข้อมูลสินค้าทั้งหมด
+            updateCart(); // อัปเดตจำนวนสินค้าในตะกร้า
+        };
     </script>
 </body>
 </html>
-- 
GitLab