add prod mylistings
This commit is contained in:
@@ -1,5 +1,37 @@
|
||||
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) => {
|
||||
const { userID, productID } = req.body;
|
||||
console.log(userID);
|
||||
|
||||
@@ -6,6 +6,7 @@ const {
|
||||
removeFavorite,
|
||||
getAllProducts,
|
||||
getProductById,
|
||||
addProduct,
|
||||
} = require("../controllers/product");
|
||||
const router = express.Router();
|
||||
|
||||
@@ -19,6 +20,7 @@ router.post("/addFavorite", addFavorite);
|
||||
router.post("/getFavorites", getFavorites);
|
||||
router.post("/delFavorite", removeFavorite);
|
||||
|
||||
router.post("/addProduct", addProduct);
|
||||
router.get("/getProduct", getAllProducts);
|
||||
router.get("/:id", getProductById); // Simplified route
|
||||
|
||||
|
||||
Reference in New Issue
Block a user