2025-03-19 02:09:30 -06:00
|
|
|
const db = require("../utils/database");
|
|
|
|
|
|
2025-03-19 04:10:41 -06:00
|
|
|
exports.addToFavorite = async (req, res) => {
|
2025-03-19 02:09:30 -06:00
|
|
|
const { userID, productsID } = req.body;
|
|
|
|
|
|
2025-03-19 04:10:41 -06:00
|
|
|
try {
|
|
|
|
|
// Use parameterized query to prevent SQL injection
|
|
|
|
|
const [result] = await db.execute(
|
|
|
|
|
"INSERT INTO Favorites (UserID, ProductID) VALUES (?, ?)",
|
|
|
|
|
[userID, productsID]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
success: true,
|
|
|
|
|
message: "Product added to favorites successfully",
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error adding favorite product:", error);
|
|
|
|
|
return res.json({ error: "Could not add favorite product" });
|
|
|
|
|
}
|
2025-03-19 02:09:30 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//Get all products
|
2025-03-19 04:10:41 -06:00
|
|
|
exports.getAllProducts = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const [data, fields] = await db.execute("SELECT * FROM Product");
|
|
|
|
|
|
2025-03-19 02:09:30 -06:00
|
|
|
res.json({
|
|
|
|
|
success: true,
|
|
|
|
|
message: "Product added to favorites successfully",
|
|
|
|
|
data,
|
|
|
|
|
});
|
2025-03-19 04:10:41 -06:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error finding user:", error);
|
|
|
|
|
return res.status(500).json({
|
|
|
|
|
found: false,
|
|
|
|
|
error: "Database error occurred",
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-03-19 02:09:30 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 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",
|
|
|
|
|
// });
|
|
|
|
|
// },
|
|
|
|
|
// );
|