update recommendation.js, generate 5 random products for new user
This commit is contained in:
@@ -3,7 +3,43 @@ const db = require("../utils/database");
|
|||||||
// TODO: Get the recommondaed product given the userID
|
// TODO: Get the recommondaed product given the userID
|
||||||
exports.RecommondationByUserId = async (req, res) => {
|
exports.RecommondationByUserId = async (req, res) => {
|
||||||
const { id } = req.body;
|
const { id } = req.body;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const [recommendation] = await db.execute(
|
||||||
|
"select * from Recommendation where UserID = ? limit 1", [id]
|
||||||
|
);
|
||||||
|
if (recommendation.length === 0) {
|
||||||
|
const [data] = await db.execute(
|
||||||
|
`
|
||||||
|
SELECT
|
||||||
|
P.ProductID,
|
||||||
|
P.Name AS ProductName,
|
||||||
|
P.Price,
|
||||||
|
P.Date AS DateUploaded,
|
||||||
|
U.Name AS SellerName,
|
||||||
|
I.URL AS ProductImage,
|
||||||
|
C.Name AS Category
|
||||||
|
FROM Product P
|
||||||
|
JOIN Image_URL I ON P.ProductID = I.ProductID
|
||||||
|
JOIN User U ON P.UserID = U.UserID
|
||||||
|
JOIN Category C ON P.CategoryID = C.CategoryID
|
||||||
|
JOIN (
|
||||||
|
SELECT ProductID
|
||||||
|
FROM Product
|
||||||
|
ORDER BY RAND()
|
||||||
|
LIMIT 5
|
||||||
|
) RandomProducts ON P.ProductID = RandomProducts.ProductID
|
||||||
|
WHERE I.URL IS NOT NULL;
|
||||||
|
`
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log("Random products for new user:", data);
|
||||||
|
return res.json({
|
||||||
|
success: true,
|
||||||
|
message: "Random products fetched for new user",
|
||||||
|
data,
|
||||||
|
});
|
||||||
|
} else{
|
||||||
const [data, fields] = await db.execute(
|
const [data, fields] = await db.execute(
|
||||||
`
|
`
|
||||||
WITH RankedImages AS (
|
WITH RankedImages AS (
|
||||||
@@ -43,6 +79,7 @@ exports.RecommondationByUserId = async (req, res) => {
|
|||||||
message: "Products fetched successfully",
|
message: "Products fetched successfully",
|
||||||
data,
|
data,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error finding products:", error);
|
console.error("Error finding products:", error);
|
||||||
return res.status(500).json({
|
return res.status(500).json({
|
||||||
|
|||||||
Reference in New Issue
Block a user