Merge branch 'mann-Branch'
This commit is contained in:
@@ -7,7 +7,7 @@ exports.getAllCategoriesWithPagination = async (req, res) => {
|
||||
try {
|
||||
const [data, _] = await db.execute(
|
||||
"SELECT * FROM Category C ORDER BY C.CategoryID ASC LIMIT ? OFFSET ?",
|
||||
[limit.toString(), offset.toString()]
|
||||
[limit.toString(), offset.toString()],
|
||||
);
|
||||
|
||||
const [result] = await db.execute("SELECT COUNT(*) AS count FROM Category");
|
||||
@@ -24,7 +24,7 @@ exports.addCategory = async (req, res) => {
|
||||
try {
|
||||
const [result] = await db.execute(
|
||||
"INSERT INTO Category (Name) VALUES (?)",
|
||||
[name]
|
||||
[name],
|
||||
);
|
||||
res.json({ message: "Adding new category successfully!" });
|
||||
} catch (error) {
|
||||
@@ -38,10 +38,33 @@ exports.removeCategory = async (req, res) => {
|
||||
try {
|
||||
const [result] = await db.execute(
|
||||
`DELETE FROM Category WHERE CategoryID = ?`,
|
||||
[id]
|
||||
[id],
|
||||
);
|
||||
res.json({ message: "Delete category successfully!" });
|
||||
} catch (error) {
|
||||
res.json({ error: "Cannot remove category from database!" });
|
||||
}
|
||||
};
|
||||
|
||||
exports.getAllCategory = async (req, res) => {
|
||||
try {
|
||||
const [data, fields] = await db.execute(`SELECT * FROM Category`);
|
||||
|
||||
const formattedData = {};
|
||||
data.forEach((row) => {
|
||||
formattedData[row.CategoryID] = row.Name;
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: "Categories fetched successfully",
|
||||
data: formattedData,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error fetching categories:", error);
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
error: "Database error occurred",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -6,7 +6,7 @@ exports.addProduct = async (req, res) => {
|
||||
try {
|
||||
const [result] = await db.execute(
|
||||
`INSERT INTO Product (Name, Price, StockQuantity, UserID, Description, CategoryID) VALUES (?, ?, ?, ?, ?, ?)`,
|
||||
[name, price, qty, userID, description, category]
|
||||
[name, price, qty, userID, description, category],
|
||||
);
|
||||
|
||||
const productID = result.insertId;
|
||||
@@ -15,7 +15,7 @@ exports.addProduct = async (req, res) => {
|
||||
db.execute(`INSERT INTO Image_URL (URL, ProductID) VALUES (?, ?)`, [
|
||||
imagePath,
|
||||
productID,
|
||||
])
|
||||
]),
|
||||
);
|
||||
|
||||
await Promise.all(imageInsertPromises); //perallel
|
||||
@@ -32,14 +32,52 @@ exports.addProduct = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
exports.removeProduct = async (req, res) => {
|
||||
const { userID, productID } = req.body;
|
||||
console.log(userID);
|
||||
|
||||
try {
|
||||
// First delete images
|
||||
await db.execute(`DELETE FROM Image_URL WHERE ProductID = ?`, [productID]);
|
||||
await db.execute(`DELETE FROM History WHERE ProductID = ?`, [productID]);
|
||||
await db.execute(`DELETE FROM Favorites WHERE ProductID = ?`, [productID]);
|
||||
await db.execute(`DELETE FROM Product_Category WHERE ProductID = ?`, [
|
||||
productID,
|
||||
]);
|
||||
await db.execute(`DELETE FROM Product_Category WHERE ProductID = ?`, [
|
||||
productID,
|
||||
]);
|
||||
await db.execute(`DELETE FROM Transaction WHERE ProductID = ?`, [
|
||||
productID,
|
||||
]);
|
||||
await db.execute(
|
||||
`DELETE FROM Recommendation WHERE RecommendedProductID = ?`,
|
||||
[productID],
|
||||
);
|
||||
|
||||
// Then delete the product
|
||||
await db.execute(`DELETE FROM Product WHERE UserID = ? AND ProductID = ?`, [
|
||||
userID,
|
||||
productID,
|
||||
]);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: "Product removed successfully",
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error removing product:", error);
|
||||
return res.json({ error: "Could not remove product" });
|
||||
}
|
||||
};
|
||||
|
||||
exports.addFavorite = async (req, res) => {
|
||||
const { userID, productID } = req.body;
|
||||
console.log(userID);
|
||||
try {
|
||||
// Use parameterized query to prevent SQL injection
|
||||
const [result] = await db.execute(
|
||||
`INSERT INTO Favorites (UserID, ProductID) VALUES (?, ?)`,
|
||||
[userID, productID]
|
||||
[userID, productID],
|
||||
);
|
||||
|
||||
res.json({
|
||||
@@ -59,7 +97,7 @@ exports.removeFavorite = async (req, res) => {
|
||||
// Use parameterized query to prevent SQL injection
|
||||
const [result] = await db.execute(
|
||||
`DELETE FROM Favorites WHERE UserID = ? AND ProductID = ?`,
|
||||
[userID, productID]
|
||||
[userID, productID],
|
||||
);
|
||||
|
||||
res.json({
|
||||
@@ -72,6 +110,103 @@ exports.removeFavorite = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
exports.updateProduct = async (req, res) => {
|
||||
const { productId } = req.params;
|
||||
const { name, description, price, category, images } = req.body;
|
||||
|
||||
console.log(productId);
|
||||
|
||||
const connection = await db.getConnection();
|
||||
try {
|
||||
await connection.beginTransaction();
|
||||
|
||||
// Step 1: Check if the product exists
|
||||
const [checkProduct] = await connection.execute(
|
||||
"SELECT * FROM Product WHERE ProductID = ?",
|
||||
[productId],
|
||||
);
|
||||
if (checkProduct.length === 0) {
|
||||
await connection.rollback();
|
||||
return res.status(404).json({ error: "Product not found" });
|
||||
}
|
||||
|
||||
// Step 2: Update the product
|
||||
await connection.execute(
|
||||
`
|
||||
UPDATE Product
|
||||
SET Name = ?, Description = ?, Price = ?, CategoryID = ?
|
||||
WHERE ProductID = ?
|
||||
`,
|
||||
[name, description, price, category, productId],
|
||||
);
|
||||
|
||||
// Step 3: Delete existing images
|
||||
await connection.execute(`DELETE FROM Image_URL WHERE ProductID = ?`, [
|
||||
productId,
|
||||
]);
|
||||
|
||||
// Step 4: Insert new image URLs
|
||||
for (const imageUrl of images) {
|
||||
await connection.execute(
|
||||
`INSERT INTO Image_URL (ProductID, URL) VALUES (?, ?)`,
|
||||
[productId, imageUrl],
|
||||
);
|
||||
}
|
||||
|
||||
await connection.commit();
|
||||
res.json({ success: true, message: "Product updated successfully" });
|
||||
} catch (error) {
|
||||
await connection.rollback();
|
||||
console.error("Update product error:", error);
|
||||
res.status(500).json({ error: "Failed to update product" });
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
};
|
||||
|
||||
exports.myProduct = async (req, res) => {
|
||||
const { userID } = req.body;
|
||||
|
||||
try {
|
||||
const [result] = await db.execute(
|
||||
`
|
||||
SELECT
|
||||
p.ProductID,
|
||||
p.Name,
|
||||
p.Description,
|
||||
p.Price,
|
||||
p.CategoryID,
|
||||
p.UserID,
|
||||
p.Date,
|
||||
u.Name AS SellerName,
|
||||
MIN(i.URL) AS image_url
|
||||
FROM Product p
|
||||
JOIN User u ON p.UserID = u.UserID
|
||||
LEFT JOIN Image_URL i ON p.ProductID = i.ProductID
|
||||
WHERE p.UserID = ?
|
||||
GROUP BY
|
||||
p.ProductID,
|
||||
p.Name,
|
||||
p.Description,
|
||||
p.Price,
|
||||
p.CategoryID,
|
||||
p.UserID,
|
||||
p.Date,
|
||||
u.Name;
|
||||
`,
|
||||
[userID],
|
||||
);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: result,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error retrieving favorites:", error);
|
||||
res.status(500).json({ error: "Could not retrieve favorite products" });
|
||||
}
|
||||
};
|
||||
|
||||
exports.getFavorites = async (req, res) => {
|
||||
const { userID } = req.body;
|
||||
|
||||
@@ -103,7 +238,7 @@ exports.getFavorites = async (req, res) => {
|
||||
p.Date,
|
||||
u.Name;
|
||||
`,
|
||||
[userID]
|
||||
[userID],
|
||||
);
|
||||
|
||||
res.json({
|
||||
@@ -168,7 +303,7 @@ exports.getProductById = async (req, res) => {
|
||||
JOIN User U ON p.UserID = U.UserID
|
||||
WHERE p.ProductID = ?
|
||||
`,
|
||||
[id]
|
||||
[id],
|
||||
);
|
||||
|
||||
// Log raw data for debugging
|
||||
@@ -242,11 +377,11 @@ exports.getProductWithPagination = async (req, res) => {
|
||||
ORDER BY P.ProductID ASC
|
||||
LIMIT ? OFFSET ?
|
||||
`,
|
||||
[limit.toString(), offset.toString()]
|
||||
[limit.toString(), offset.toString()],
|
||||
);
|
||||
|
||||
const [result] = await db.execute(
|
||||
`SELECT COUNT(*) AS totalProd FROM Product`
|
||||
`SELECT COUNT(*) AS totalProd FROM Product`,
|
||||
);
|
||||
const { totalProd } = result[0];
|
||||
|
||||
@@ -262,40 +397,10 @@ exports.removeProduct = async (req, res) => {
|
||||
try {
|
||||
const [result] = await db.execute(
|
||||
`DELETE FROM Product WHERE ProductID = ?`,
|
||||
[id]
|
||||
[id],
|
||||
);
|
||||
res.json({ message: "Delete product successfully!" });
|
||||
} catch (error) {
|
||||
res.json({ error: "Cannot remove product from database!" });
|
||||
}
|
||||
};
|
||||
|
||||
// db_con.query(
|
||||
// "SELECT ProductID FROM product WHERE ProductID = ?",
|
||||
// [productID],
|
||||
// (err, results) => {
|
||||
// if (err) {
|
||||
// console.error("Error checking product:", err);
|
||||
// return res.json({ error: "Database error" });
|
||||
// }
|
||||
|
||||
// if (results.length === 0) {
|
||||
// return res.json({ error: "Product does not exist" });
|
||||
// }
|
||||
// },
|
||||
// );
|
||||
|
||||
// db_con.query(
|
||||
// "INSERT INTO Favorites (UserID, ProductID) VALUES (?, ?)",
|
||||
// [userID, productID],
|
||||
// (err, result) => {
|
||||
// if (err) {
|
||||
// console.error("Error adding favorite product:", err);
|
||||
// return res.json({ error: "Could not add favorite product" });
|
||||
// }
|
||||
// res.json({
|
||||
// success: true,
|
||||
// message: "Product added to favorites successfully",
|
||||
// });
|
||||
// },
|
||||
// );
|
||||
|
||||
@@ -46,6 +46,7 @@ app.use("/api/history", history);
|
||||
app.use("/api/review", review);
|
||||
app.use("/api/category", categoryRouter);
|
||||
app.use("/api/transaction", transactionRouter);
|
||||
app.use("/api/category", categoryRouter);
|
||||
|
||||
// Set up a scheduler to run cleanup every hour
|
||||
clean_up_time = 30 * 60 * 1000;
|
||||
|
||||
@@ -3,6 +3,7 @@ const {
|
||||
getAllCategoriesWithPagination,
|
||||
addCategory,
|
||||
removeCategory,
|
||||
getAllCategory,
|
||||
} = require("../controllers/category");
|
||||
|
||||
const router = express.Router();
|
||||
@@ -10,5 +11,6 @@ const router = express.Router();
|
||||
router.get("/getCategories", getAllCategoriesWithPagination);
|
||||
router.post("/addCategory", addCategory);
|
||||
router.delete("/:id", removeCategory);
|
||||
router.get("/", getAllCategory);
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -9,6 +9,8 @@ const {
|
||||
addProduct,
|
||||
removeProduct,
|
||||
getProductWithPagination,
|
||||
myProduct,
|
||||
updateProduct,
|
||||
} = require("../controllers/product");
|
||||
const router = express.Router();
|
||||
|
||||
@@ -22,6 +24,8 @@ router.post("/addFavorite", addFavorite);
|
||||
router.post("/getFavorites", getFavorites);
|
||||
router.post("/delFavorite", removeFavorite);
|
||||
|
||||
router.post("/delProduct", removeProduct);
|
||||
router.post("/myProduct", myProduct);
|
||||
router.post("/addProduct", addProduct);
|
||||
router.get("/getProduct", getAllProducts);
|
||||
|
||||
@@ -32,4 +36,6 @@ router.get("/getProductWithPagination", getProductWithPagination);
|
||||
|
||||
router.get("/:id", getProductById); // Simplified route
|
||||
|
||||
router.put("/update/:productId", updateProduct);
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -7,4 +7,8 @@ const pool = mysql.createPool({
|
||||
password: "12345678",
|
||||
});
|
||||
|
||||
// const pool = mysql.createPool(
|
||||
// "singlestore://mann-619d0:<mann-619d0 Password>@svc-3482219c-a389-4079-b18b-d50662524e8a-shared-dml.aws-virginia-6.svc.singlestore.com:3333/db_mann_48ba9?ssl={}",
|
||||
// );
|
||||
|
||||
module.exports = pool.promise();
|
||||
|
||||
Reference in New Issue
Block a user