add prod mylistings
This commit is contained in:
@@ -1,5 +1,37 @@
|
|||||||
const db = require("../utils/database");
|
const db = require("../utils/database");
|
||||||
|
|
||||||
|
exports.addProduct = async (req, res) => {
|
||||||
|
const { userID, name, price, qty, description, category, images } = req.body;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const [result] = await db.execute(
|
||||||
|
`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 and images added successfully",
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error adding product or images:", error);
|
||||||
|
console.log(error);
|
||||||
|
return res.json({ error: "Could not add product or images" });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
exports.addFavorite = async (req, res) => {
|
exports.addFavorite = async (req, res) => {
|
||||||
const { userID, productID } = req.body;
|
const { userID, productID } = req.body;
|
||||||
console.log(userID);
|
console.log(userID);
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ const {
|
|||||||
removeFavorite,
|
removeFavorite,
|
||||||
getAllProducts,
|
getAllProducts,
|
||||||
getProductById,
|
getProductById,
|
||||||
|
addProduct,
|
||||||
} = require("../controllers/product");
|
} = require("../controllers/product");
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
@@ -19,6 +20,7 @@ router.post("/addFavorite", addFavorite);
|
|||||||
router.post("/getFavorites", getFavorites);
|
router.post("/getFavorites", getFavorites);
|
||||||
router.post("/delFavorite", removeFavorite);
|
router.post("/delFavorite", removeFavorite);
|
||||||
|
|
||||||
|
router.post("/addProduct", addProduct);
|
||||||
router.get("/getProduct", getAllProducts);
|
router.get("/getProduct", getAllProducts);
|
||||||
router.get("/:id", getProductById); // Simplified route
|
router.get("/:id", getProductById); // Simplified route
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ const ProductForm = ({
|
|||||||
onCancel,
|
onCancel,
|
||||||
}) => {
|
}) => {
|
||||||
const [selectedCategory, setSelectedCategory] = useState("");
|
const [selectedCategory, setSelectedCategory] = useState("");
|
||||||
|
const storedUser = JSON.parse(sessionStorage.getItem("user"));
|
||||||
|
|
||||||
const categories = [
|
const categories = [
|
||||||
"Electronics",
|
"Electronics",
|
||||||
@@ -28,6 +29,92 @@ const ProductForm = ({
|
|||||||
"Other",
|
"Other",
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Map category names to their respective IDs
|
||||||
|
const categoryMapping = {
|
||||||
|
Electronics: 1,
|
||||||
|
Clothing: 2,
|
||||||
|
"Home & Garden": 3,
|
||||||
|
"Toys & Games": 4,
|
||||||
|
Books: 5,
|
||||||
|
"Sports & Outdoors": 6,
|
||||||
|
Automotive: 7,
|
||||||
|
"Beauty & Personal Care": 8,
|
||||||
|
"Health & Wellness": 9,
|
||||||
|
Jewelry: 10,
|
||||||
|
"Art & Collectibles": 11,
|
||||||
|
"Food & Beverages": 12,
|
||||||
|
"Office Supplies": 13,
|
||||||
|
"Pet Supplies": 14,
|
||||||
|
"Music & Instruments": 15,
|
||||||
|
Other: 16,
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSave = async () => {
|
||||||
|
// Check if the user has selected at least one category
|
||||||
|
if (!(editingProduct.categories || []).length) {
|
||||||
|
alert("Please select at least one category");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// First, upload images if there are any
|
||||||
|
const imagePaths = [];
|
||||||
|
|
||||||
|
// If we have files to upload, we'd handle the image upload here
|
||||||
|
// This is a placeholder for where you'd implement image uploads
|
||||||
|
// For now, we'll simulate the API expecting paths:
|
||||||
|
if (editingProduct.images && editingProduct.images.length > 0) {
|
||||||
|
// Simulating image paths for demo purposes
|
||||||
|
// In a real implementation, you would upload these files first
|
||||||
|
// and then use the returned paths
|
||||||
|
editingProduct.images.forEach((file, index) => {
|
||||||
|
const simulatedPath = `/public/uploads/${file.name}`;
|
||||||
|
imagePaths.push(simulatedPath);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the category ID from the first selected category
|
||||||
|
const categoryName = (editingProduct.categories || [])[0];
|
||||||
|
const categoryID = categoryMapping[categoryName] || 3; // Default to 3 if not found
|
||||||
|
|
||||||
|
// Prepare payload according to API expectations
|
||||||
|
const payload = {
|
||||||
|
name: editingProduct.name || "",
|
||||||
|
price: parseFloat(editingProduct.price) || 0,
|
||||||
|
qty: 1, // Hardcoded as per your requirement
|
||||||
|
userID: storedUser.ID,
|
||||||
|
description: editingProduct.description || "",
|
||||||
|
category: categoryID,
|
||||||
|
images: imagePaths,
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log("Sending payload:", payload);
|
||||||
|
|
||||||
|
const response = await fetch(
|
||||||
|
"http://localhost:3030/api/product/addProduct",
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(payload),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorData = await response.text();
|
||||||
|
throw new Error(`Failed to add product: ${errorData}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
console.log("Product added:", data);
|
||||||
|
if (onSave) onSave(data);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error saving product:", error);
|
||||||
|
alert(`Error saving product: ${error.message}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const addCategory = () => {
|
const addCategory = () => {
|
||||||
if (
|
if (
|
||||||
selectedCategory &&
|
selectedCategory &&
|
||||||
@@ -309,7 +396,7 @@ const ProductForm = ({
|
|||||||
Cancel
|
Cancel
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={onSave}
|
onClick={handleSave}
|
||||||
className="bg-emerald-600 text-white px-6 py-2 hover:bg-emerald-700 rounded-md"
|
className="bg-emerald-600 text-white px-6 py-2 hover:bg-emerald-700 rounded-md"
|
||||||
>
|
>
|
||||||
{editingProduct.id ? "Update Product" : "Add Product"}
|
{editingProduct.id ? "Update Product" : "Add Product"}
|
||||||
|
|||||||
Reference in New Issue
Block a user