diff --git a/backend/controllers/category.js b/backend/controllers/category.js index 98a6c25..31776f7 100644 --- a/backend/controllers/category.js +++ b/backend/controllers/category.js @@ -4,7 +4,6 @@ exports.getAllCategory = async (req, res) => { try { const [data, fields] = await db.execute(`SELECT * FROM Category`); - // Format as { ID: "", ID: "" } const formattedData = {}; data.forEach((row) => { formattedData[row.CategoryID] = row.Name; diff --git a/backend/controllers/product.js b/backend/controllers/product.js index 9684513..bbe4f1f 100644 --- a/backend/controllers/product.js +++ b/backend/controllers/product.js @@ -1,22 +1,34 @@ const db = require("../utils/database"); exports.addProduct = async (req, res) => { - const { userID, name, price, stockQty, Description } = req.body; - console.log(userID); + const { userID, name, price, qty, description, category, images } = req.body; + try { - // Use parameterized query to prevent SQL injection const [result] = await db.execute( - `INSERT INTO Favorites (UserID, ProductID) VALUES (?, ?)`, - [userID, productID], + `INSERT INTO Product (Name, Price, StockQuantity, UserID, Description, CategoryID) VALUES (?, ?, ?, ?, ?, ?)`, + [name, price, qty, userID, description, category], ); + const productID = result.insertId; + if (images && images.length > 0) { + const imageInsertPromises = images.map((imagePath) => + db.execute(`INSERT INTO Image_URL (URL, ProductID) VALUES (?, ?)`, [ + imagePath, + productID, + ]), + ); + + await Promise.all(imageInsertPromises); //perallel + } + res.json({ success: true, - message: "Product added to favorites successfully", + message: "Product and images added successfully", }); } catch (error) { - console.error("Error adding favorite product:", error); - return res.json({ error: "Could not add favorite product" }); + console.error("Error adding product or images:", error); + console.log(error); + return res.json({ error: "Could not add product or images" }); } }; @@ -64,7 +76,7 @@ exports.myProduct = async (req, res) => { const { userID } = req.body; try { - const [favorites] = await db.execute( + const [result] = await db.execute( ` SELECT p.ProductID, @@ -95,7 +107,7 @@ exports.myProduct = async (req, res) => { res.json({ success: true, - favorites: favorites, + data: result, }); } catch (error) { console.error("Error retrieving favorites:", error); diff --git a/frontend/src/pages/Home.jsx b/frontend/src/pages/Home.jsx index 22dc205..9782f87 100644 --- a/frontend/src/pages/Home.jsx +++ b/frontend/src/pages/Home.jsx @@ -201,7 +201,7 @@ const Home = () => { University of Calgary {/* Dark overlay for better text readability */} @@ -217,7 +217,7 @@ const Home = () => {

diff --git a/frontend/src/pages/ProductDetail.jsx b/frontend/src/pages/ProductDetail.jsx index a5f0d73..ebec8f2 100644 --- a/frontend/src/pages/ProductDetail.jsx +++ b/frontend/src/pages/ProductDetail.jsx @@ -1,7 +1,6 @@ import { useState, useEffect } from "react"; import { useParams, Link } from "react-router-dom"; import { - Heart, ArrowLeft, Tag, User, @@ -26,7 +25,6 @@ const ProductDetail = () => { reviews: null, submit: null, }); - const [isFavorite, setIsFavorite] = useState(false); const [showContactOptions, setShowContactOptions] = useState(false); const [currentImage, setCurrentImage] = useState(0); const [reviews, setReviews] = useState([]); @@ -52,7 +50,6 @@ const ProductDetail = () => { if (data.success) { setShowAlert(true); } - console.log(`Add Product -> History: ${id}`); }; const [reviewForm, setReviewForm] = useState({ @@ -248,7 +245,7 @@ const ProductDetail = () => { if (loading.product) { return (
-
+
); } @@ -262,7 +259,7 @@ const ProductDetail = () => {

{error.product}

Back to Listings @@ -279,7 +276,7 @@ const ProductDetail = () => {

Product Not Found

Back to Listings @@ -291,15 +288,15 @@ const ProductDetail = () => { // Render product details return (
-
+ {/*
Back -
+
*/} {showAlert && ( { {product.images.map((image, index) => (
selectImage(index)} > { e.preventDefault(); toggleFavorite(product.ProductID); }} - className="top-0 p-2 rounded-bl-md bg-emerald-600 hover:bg-emerald-500 transition shadow-sm" + className="top-0 p-2 rounded-bl-md bg-emerald-700 hover:bg-emerald-600 transition shadow-sm" >
-
+
$ {typeof product.Price === "number" ? product.Price.toFixed(2) @@ -418,7 +415,7 @@ const ProductDetail = () => {
@@ -430,7 +427,7 @@ const ProductDetail = () => { href={`tel:${product.SellerPhone}`} className="flex items-center gap-2 p-3 hover:bg-gray-50 border-b border-gray-100" > - + Call Seller )} @@ -440,7 +437,7 @@ const ProductDetail = () => { href={`mailto:${product.SellerEmail}`} className="flex items-center gap-2 p-3 hover:bg-gray-50" > - + Email Seller )} @@ -477,7 +474,7 @@ const ProductDetail = () => {
{loading.reviews ? (
-
+
) : error.reviews ? (
@@ -524,7 +521,7 @@ const ProductDetail = () => {
@@ -582,7 +579,7 @@ const ProductDetail = () => { id="comment" value={reviewForm.comment} onChange={handleReviewInputChange} - className="w-full p-3 border border-gray-300 focus:outline-none focus:border-green-500" + className="w-full p-3 border border-gray-300 focus:outline-none focus:border-emerald-600" rows="4" required > @@ -598,7 +595,7 @@ const ProductDetail = () => { - + {product.categories && product.categories.length > 0 && ( +
+ {product.CategoryID.map((category) => ( + + {category} + + ))} +
+ )} + +

+ {product.Description} +

+ +
+ + +
-
+ ))}
)}