add review and read review now done
This commit is contained in:
@@ -37,7 +37,6 @@ exports.HistoryByUserId = async (req, res) => {
|
||||
[id],
|
||||
);
|
||||
|
||||
console.log(data);
|
||||
res.json({
|
||||
success: true,
|
||||
message: "Products fetched successfully",
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
const db = require("../utils/database");
|
||||
|
||||
exports.addToFavorite = async (req, res) => {
|
||||
exports.addFavorite = async (req, res) => {
|
||||
const { userID, productsID } = req.body;
|
||||
|
||||
try {
|
||||
// Use parameterized query to prevent SQL injection
|
||||
const [result] = await db.execute(
|
||||
"INSERT INTO Favorites (UserID, ProductID) VALUES unique(?, ?)",
|
||||
"INSERT INTO Favorites (UserID, ProductID) VALUES (?, ?)",
|
||||
[userID, productsID],
|
||||
);
|
||||
|
||||
@@ -14,13 +14,41 @@ exports.addToFavorite = async (req, res) => {
|
||||
success: true,
|
||||
message: "Product added to favorites successfully",
|
||||
});
|
||||
console.log(result);
|
||||
} catch (error) {
|
||||
console.error("Error adding favorite product:", error);
|
||||
return res.json({ error: "Could not add favorite product" });
|
||||
}
|
||||
};
|
||||
|
||||
exports.getFavorites = async (req, res) => {
|
||||
const { userID } = req.body;
|
||||
|
||||
try {
|
||||
const [favorites] = await db.execute(
|
||||
`
|
||||
SELECT
|
||||
p.*,
|
||||
u.Name AS SellerName,
|
||||
i.URL AS image_url
|
||||
FROM Favorites f
|
||||
JOIN Product p ON f.ProductID = p.ProductID
|
||||
JOIN User u ON p.UserID = u.UserID
|
||||
LEFT JOIN Image_URL i ON p.ProductID = i.ProductID
|
||||
WHERE f.UserID = ?
|
||||
`,
|
||||
[userID],
|
||||
);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
favorites: favorites,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error retrieving favorites:", error);
|
||||
res.status(500).json({ error: "Could not retrieve favorite products" });
|
||||
}
|
||||
};
|
||||
|
||||
// Get all products along with their image URLs
|
||||
exports.getAllProducts = async (req, res) => {
|
||||
try {
|
||||
@@ -52,7 +80,6 @@ exports.getAllProducts = async (req, res) => {
|
||||
WHERE RowNum = 1;
|
||||
`);
|
||||
|
||||
console.log(data);
|
||||
res.json({
|
||||
success: true,
|
||||
message: "Products fetched successfully",
|
||||
@@ -74,7 +101,7 @@ exports.getProductById = async (req, res) => {
|
||||
try {
|
||||
const [data] = await db.execute(
|
||||
`
|
||||
SELECT p.*,U.Name AS SellerName, i.URL AS image_url
|
||||
SELECT p.*,U.Name AS SellerName,U.Email as SellerEmail,U.Phone as SellerPhone, i.URL AS image_url
|
||||
FROM Product p
|
||||
LEFT JOIN Image_URL i ON p.ProductID = i.ProductID
|
||||
JOIN User U ON p.UserID = U.UserID
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
const db = require("../utils/database");
|
||||
|
||||
exports.getreview = async (req, res) => {
|
||||
/**
|
||||
* Get reviews for a specific product
|
||||
* Returns both reviews for the product and reviews by the product owner for other products
|
||||
*/
|
||||
exports.getReviews = async (req, res) => {
|
||||
const { id } = req.params;
|
||||
console.log("Received Product ID:", id);
|
||||
|
||||
try {
|
||||
const [data] = await db.execute(
|
||||
`
|
||||
SELECT
|
||||
// First query: Get reviews for this specific product
|
||||
const [productReviews] = await db.execute(
|
||||
`SELECT
|
||||
R.ReviewID,
|
||||
R.UserID,
|
||||
R.ProductID,
|
||||
@@ -15,44 +19,49 @@ exports.getreview = async (req, res) => {
|
||||
R.Rating,
|
||||
R.Date AS ReviewDate,
|
||||
U.Name AS ReviewerName,
|
||||
P.Name AS ProductName
|
||||
P.Name AS ProductName,
|
||||
'product' AS ReviewType
|
||||
FROM Review R
|
||||
JOIN User U ON R.UserID = U.UserID
|
||||
JOIN Product P ON R.ProductID = P.ProductID
|
||||
WHERE R.ProductID = ?
|
||||
|
||||
UNION
|
||||
|
||||
SELECT
|
||||
R.ReviewID,
|
||||
R.UserID,
|
||||
R.ProductID,
|
||||
R.Comment,
|
||||
R.Rating,
|
||||
R.Date AS ReviewDate,
|
||||
U.Name AS ReviewerName,
|
||||
P.Name AS ProductName
|
||||
FROM Review R
|
||||
JOIN User U ON R.UserID = U.UserID
|
||||
JOIN Product P ON R.ProductID = P.ProductID
|
||||
WHERE P.UserID = (
|
||||
SELECT UserID
|
||||
FROM Product
|
||||
WHERE ProductID = ?
|
||||
)
|
||||
AND R.UserID != P.UserID;
|
||||
`,
|
||||
[id, id],
|
||||
WHERE R.ProductID = ?`,
|
||||
[id],
|
||||
);
|
||||
|
||||
// Log raw data for debugging
|
||||
console.log("Raw Database Result:", data);
|
||||
// // Second query: Get reviews written by the product owner for other products
|
||||
// const [sellerReviews] = await db.execute(
|
||||
// `SELECT
|
||||
// R.ReviewID,
|
||||
// R.UserID,
|
||||
// R.ProductID,
|
||||
// R.Comment,
|
||||
// R.Rating,
|
||||
// R.Date AS ReviewDate,
|
||||
// U.Name AS ReviewerName,
|
||||
// P.Name AS ProductName,
|
||||
// 'seller' AS ReviewType
|
||||
// FROM Review R
|
||||
// JOIN User U ON R.UserID = U.UserID
|
||||
// JOIN Product P ON R.ProductID = P.ProductID
|
||||
// WHERE R.UserID = (
|
||||
// SELECT UserID
|
||||
// FROM Product
|
||||
// WHERE ProductID = ?
|
||||
// )
|
||||
// AND R.ProductID != ?`,
|
||||
// [id, id],
|
||||
// );
|
||||
|
||||
// Combine the results
|
||||
const combinedReviews = [...productReviews];
|
||||
|
||||
// Log data for debugging
|
||||
console.log("Combined Reviews:", combinedReviews);
|
||||
|
||||
console.log(data);
|
||||
res.json({
|
||||
success: true,
|
||||
message: "Products fetched successfully",
|
||||
data,
|
||||
message: "Reviews fetched successfully",
|
||||
data: combinedReviews,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Full Error Details:", error);
|
||||
@@ -64,7 +73,9 @@ exports.getreview = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
// Add this to your existing controller file
|
||||
/**
|
||||
* Submit a new review for a product
|
||||
*/
|
||||
exports.submitReview = async (req, res) => {
|
||||
const { productId, userId, rating, comment } = req.body;
|
||||
|
||||
@@ -85,16 +96,41 @@ exports.submitReview = async (req, res) => {
|
||||
}
|
||||
|
||||
try {
|
||||
// Check if user has already reviewed this product
|
||||
const [existingReview] = await db.execute(
|
||||
`SELECT ReviewID FROM Review WHERE ProductID = ? AND UserID = ?`,
|
||||
[productId, userId],
|
||||
);
|
||||
|
||||
if (existingReview.length > 0) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "You have already reviewed this product",
|
||||
});
|
||||
}
|
||||
|
||||
// Check if user is trying to review their own product
|
||||
const [productOwner] = await db.execute(
|
||||
`SELECT UserID FROM Product WHERE ProductID = ?`,
|
||||
[productId],
|
||||
);
|
||||
|
||||
if (productOwner.length > 0 && productOwner[0].UserID === userId) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "You cannot review your own product",
|
||||
});
|
||||
}
|
||||
|
||||
// Insert the review into the database
|
||||
const [result] = await db.execute(
|
||||
`
|
||||
INSERT INTO Review (
|
||||
`INSERT INTO Review (
|
||||
ProductID,
|
||||
UserID,
|
||||
Rating,
|
||||
Comment
|
||||
) VALUES (?, ?, ?, ?)
|
||||
`,
|
||||
Comment,
|
||||
Date
|
||||
) VALUES (?, ?, ?, ?, NOW())`,
|
||||
[productId, userId, rating, comment],
|
||||
);
|
||||
|
||||
@@ -103,22 +139,24 @@ exports.submitReview = async (req, res) => {
|
||||
|
||||
// Fetch the newly created review to return to client
|
||||
const [newReview] = await db.execute(
|
||||
`
|
||||
SELECT
|
||||
ReviewID as id,
|
||||
ProductID,
|
||||
UserID,
|
||||
Rating,
|
||||
Comment,
|
||||
Date as ReviewDate
|
||||
FROM Review
|
||||
WHERE ReviewID = ?
|
||||
`,
|
||||
`SELECT
|
||||
R.ReviewID,
|
||||
R.ProductID,
|
||||
R.UserID,
|
||||
R.Rating,
|
||||
R.Comment,
|
||||
R.Date AS ReviewDate,
|
||||
U.Name AS ReviewerName,
|
||||
P.Name AS ProductName
|
||||
FROM Review R
|
||||
JOIN User U ON R.UserID = U.UserID
|
||||
JOIN Product P ON R.ProductID = P.ProductID
|
||||
WHERE R.ReviewID = ?`,
|
||||
[reviewId],
|
||||
);
|
||||
|
||||
res.status(201).json({
|
||||
success: false,
|
||||
success: true, // Fixed from false to true
|
||||
message: "Review submitted successfully",
|
||||
data: newReview[0],
|
||||
});
|
||||
@@ -131,3 +169,134 @@ exports.submitReview = async (req, res) => {
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// /**
|
||||
// * Update an existing review
|
||||
// */
|
||||
// exports.updateReview = async (req, res) => {
|
||||
// const { reviewId } = req.params;
|
||||
// const { rating, comment } = req.body;
|
||||
// const userId = req.body.userId; // Assuming you have middleware that validates the user
|
||||
|
||||
// // Validate required fields
|
||||
// if (!reviewId || !rating || !comment) {
|
||||
// return res.status(400).json({
|
||||
// success: false,
|
||||
// message: "Missing required fields",
|
||||
// });
|
||||
// }
|
||||
|
||||
// // Validate rating is between 1 and 5
|
||||
// if (rating < 1 || rating > 5) {
|
||||
// return res.status(400).json({
|
||||
// success: false,
|
||||
// message: "Rating must be between 1 and 5",
|
||||
// });
|
||||
// }
|
||||
|
||||
// try {
|
||||
// // Check if review exists and belongs to the user
|
||||
// const [existingReview] = await db.execute(
|
||||
// `SELECT ReviewID, UserID FROM Review WHERE ReviewID = ?`,
|
||||
// [reviewId],
|
||||
// );
|
||||
|
||||
// if (existingReview.length === 0) {
|
||||
// return res.status(404).json({
|
||||
// success: false,
|
||||
// message: "Review not found",
|
||||
// });
|
||||
// }
|
||||
|
||||
// if (existingReview[0].UserID !== userId) {
|
||||
// return res.status(403).json({
|
||||
// success: false,
|
||||
// message: "You can only update your own reviews",
|
||||
// });
|
||||
// }
|
||||
|
||||
// // Update the review
|
||||
// await db.execute(
|
||||
// `UPDATE Review
|
||||
// SET Rating = ?, Comment = ?, Date = NOW()
|
||||
// WHERE ReviewID = ?`,
|
||||
// [rating, comment, reviewId],
|
||||
// );
|
||||
|
||||
// // Fetch the updated review
|
||||
// const [updatedReview] = await db.execute(
|
||||
// `SELECT
|
||||
// R.ReviewID,
|
||||
// R.ProductID,
|
||||
// R.UserID,
|
||||
// R.Rating,
|
||||
// R.Comment,
|
||||
// R.Date AS ReviewDate,
|
||||
// U.Name AS ReviewerName,
|
||||
// P.Name AS ProductName
|
||||
// FROM Review R
|
||||
// JOIN User U ON R.UserID = U.UserID
|
||||
// JOIN Product P ON R.ProductID = P.ProductID
|
||||
// WHERE R.ReviewID = ?`,
|
||||
// [reviewId],
|
||||
// );
|
||||
|
||||
// res.json({
|
||||
// success: true,
|
||||
// message: "Review updated successfully",
|
||||
// data: updatedReview[0],
|
||||
// });
|
||||
// } catch (error) {
|
||||
// console.error("Error updating review:", error);
|
||||
// return res.status(500).json({
|
||||
// success: false,
|
||||
// message: "Database error occurred",
|
||||
// error: error.message,
|
||||
// });
|
||||
// }
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * Delete a review
|
||||
// */
|
||||
// exports.deleteReview = async (req, res) => {
|
||||
// const { reviewId } = req.params;
|
||||
// const userId = req.body.userId; // Assuming you have middleware that validates the user
|
||||
|
||||
// try {
|
||||
// // Check if review exists and belongs to the user
|
||||
// const [existingReview] = await db.execute(
|
||||
// `SELECT ReviewID, UserID FROM Review WHERE ReviewID = ?`,
|
||||
// [reviewId],
|
||||
// );
|
||||
|
||||
// if (existingReview.length === 0) {
|
||||
// return res.status(404).json({
|
||||
// success: false,
|
||||
// message: "Review not found",
|
||||
// });
|
||||
// }
|
||||
|
||||
// if (existingReview[0].UserID !== userId) {
|
||||
// return res.status(403).json({
|
||||
// success: false,
|
||||
// message: "You can only delete your own reviews",
|
||||
// });
|
||||
// }
|
||||
|
||||
// // Delete the review
|
||||
// await db.execute(`DELETE FROM Review WHERE ReviewID = ?`, [reviewId]);
|
||||
|
||||
// res.json({
|
||||
// success: true,
|
||||
// message: "Review deleted successfully",
|
||||
// });
|
||||
// } catch (error) {
|
||||
// console.error("Error deleting review:", error);
|
||||
// return res.status(500).json({
|
||||
// success: false,
|
||||
// message: "Database error occurred",
|
||||
// error: error.message,
|
||||
// });
|
||||
// }
|
||||
// };
|
||||
|
||||
@@ -39,9 +39,9 @@ checkDatabaseConnection(db);
|
||||
//Routes
|
||||
app.use("/api/user", userRouter); //prefix with /api/user
|
||||
app.use("/api/product", productRouter); //prefix with /api/product
|
||||
app.use("/api/search_products", searchRouter); //prefix with /api/product
|
||||
app.use("/api/Engine", recommendedRouter); //prefix with /api/
|
||||
app.use("/api/get", history); //prefix with /api/
|
||||
app.use("/api/search", searchRouter); //prefix with /api/product
|
||||
app.use("/api/engine", recommendedRouter); //prefix with /api/
|
||||
app.use("/api/history", history); //prefix with /api/
|
||||
app.use("/api/review", review); //prefix with /api/
|
||||
|
||||
// Set up a scheduler to run cleanup every hour
|
||||
|
||||
@@ -3,6 +3,6 @@ const express = require("express");
|
||||
const { HistoryByUserId } = require("../controllers/history");
|
||||
const router = express.Router();
|
||||
|
||||
router.post("/history", HistoryByUserId);
|
||||
router.post("/getHistory", HistoryByUserId);
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
// routes/product.js
|
||||
const express = require("express");
|
||||
const {
|
||||
addToFavorite,
|
||||
addFavorite,
|
||||
getFavorites,
|
||||
getAllProducts,
|
||||
getProductById,
|
||||
} = require("../controllers/product");
|
||||
@@ -13,8 +14,10 @@ router.use((req, res, next) => {
|
||||
next();
|
||||
});
|
||||
|
||||
router.post("/add_fav_product", addToFavorite);
|
||||
router.get("/get_product", getAllProducts);
|
||||
router.post("/addFavorite", addFavorite);
|
||||
router.post("/getFavorites", getFavorites);
|
||||
|
||||
router.get("/getProduct", getAllProducts);
|
||||
router.get("/:id", getProductById); // Simplified route
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
// routes/product.js
|
||||
const express = require("express");
|
||||
const { getreview, submitReview } = require("../controllers/review");
|
||||
const { getReviews, submitReview } = require("../controllers/review");
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/:id", getreview);
|
||||
router.get("/:id", getReviews);
|
||||
router.post("/add", submitReview);
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -9,6 +9,6 @@ router.use((req, res, next) => {
|
||||
next();
|
||||
});
|
||||
|
||||
router.get("/search", searchProductsByName);
|
||||
router.get("/getProduct", searchProductsByName);
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user