Update recommendation.js
This commit is contained in:
@@ -51,64 +51,3 @@ exports.RecommondationByUserId = async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add this to your existing controller file
|
|
||||||
exports.submitReview = async (req, res) => {
|
|
||||||
const { productId, reviewerName, rating, comment } = req.body;
|
|
||||||
|
|
||||||
// Validate required fields
|
|
||||||
if (!productId || !reviewerName || !rating || !comment) {
|
|
||||||
return res.status(400).json({
|
|
||||||
success: false,
|
|
||||||
message: "Missing required fields",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Insert the review into the database
|
|
||||||
const [result] = await db.execute(
|
|
||||||
`
|
|
||||||
INSERT INTO Review (
|
|
||||||
ProductID,
|
|
||||||
ReviewerName,
|
|
||||||
Rating,
|
|
||||||
Comment,
|
|
||||||
ReviewDate
|
|
||||||
) VALUES (?, ?, ?, ?, NOW())
|
|
||||||
`,
|
|
||||||
[productId, reviewerName, rating, comment],
|
|
||||||
);
|
|
||||||
|
|
||||||
// Get the inserted review id
|
|
||||||
const reviewId = result.insertId;
|
|
||||||
|
|
||||||
// Fetch the newly created review to return to client
|
|
||||||
const [newReview] = await db.execute(
|
|
||||||
`
|
|
||||||
SELECT
|
|
||||||
ReviewID as id,
|
|
||||||
ProductID,
|
|
||||||
ReviewerName,
|
|
||||||
Rating,
|
|
||||||
Comment,
|
|
||||||
ReviewDate
|
|
||||||
FROM Review
|
|
||||||
WHERE ReviewID = ?
|
|
||||||
`,
|
|
||||||
[reviewId],
|
|
||||||
);
|
|
||||||
|
|
||||||
res.status(201).json({
|
|
||||||
success: true,
|
|
||||||
message: "Review submitted successfully",
|
|
||||||
data: newReview[0],
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error submitting review:", error);
|
|
||||||
return res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: "Database error occurred",
|
|
||||||
error: error.message,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|||||||
Reference in New Issue
Block a user