From 6ef4a22e9f224b57a8edb164c6f6724ff4dfae1b Mon Sep 17 00:00:00 2001
From: Mann Patel <130435633+MannPatel0@users.noreply.github.com>
Date: Sun, 20 Apr 2025 12:50:46 -0600
Subject: [PATCH] update on category
---
backend/controllers/category.js | 1 -
backend/controllers/product.js | 32 ++++--
frontend/src/pages/Home.jsx | 4 +-
frontend/src/pages/ProductDetail.jsx | 35 +++---
frontend/src/pages/Selling.jsx | 164 ++++++++++++++-------------
5 files changed, 127 insertions(+), 109 deletions(-)
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 = () => {
{/* 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 (
-
*/}
{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 = () => {