Files

91 lines
2.3 KiB
JavaScript
Raw Permalink Normal View History

2025-04-03 18:56:39 -06:00
const db = require("../utils/database");
// TODO: Get the recommondaed product given the userID
exports.RecommondationByUserId = async (req, res) => {
const { id } = req.body;
2025-04-03 18:56:39 -06:00
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{
2025-04-03 18:56:39 -06:00
const [data, fields] = await db.execute(
`
2025-04-04 00:02:04 -06:00
WITH RankedImages AS (
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,
ROW_NUMBER() OVER (PARTITION BY P.ProductID ORDER BY I.URL) AS RowNum
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 Recommendation R ON P.ProductID = R.RecommendedProductID
WHERE R.UserID = ?
)
2025-04-03 18:56:39 -06:00
SELECT
2025-04-04 00:02:04 -06:00
ProductID,
ProductName,
Price,
DateUploaded,
SellerName,
ProductImage,
Category
FROM RankedImages
WHERE RowNum = 1;
`,
2025-04-03 18:56:39 -06:00
[id],
);
console.log(data);
res.json({
success: true,
message: "Products fetched successfully",
data,
});
}
2025-04-03 18:56:39 -06:00
} catch (error) {
console.error("Error finding products:", error);
return res.status(500).json({
found: false,
error: "Database error occurred",
});
}
};