-
Notifications
You must be signed in to change notification settings - Fork 0
/
wishlist.js
100 lines (77 loc) · 3.4 KB
/
wishlist.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const express = require("express");
const { validateJSONToken } = require("../utils/auth");
const router = express.Router();
const connection = require("../DB/db");
// for user only
router.post("/add-to-whishlist", async (req, res) => {
try {
const token = req.headers.authorization.split(" ")[1]; // Extract user ID from Bearer token
const isValid = validateJSONToken(token);
const { user_id, product_id } = req.body;
if (isValid) {
const [data] = await connection
.promise()
.query(
`SELECT * FROM userwishlist WHERE user_id = ? AND product_id = ?`,
[user_id, product_id]
);
if (data.length > 0) {
const [findProduct_id] = await connection
.promise()
.query(`SELECT product_id FROM userwishlist WHERE user_id = ?`, [user_id]);
const productArray = findProduct_id.map((item) => item.product_id);
const idsString = productArray.join(", ");
const query = `SELECT * FROM products WHERE id IN (${idsString})`;
const [products] = await connection.promise().query(query);
res.status(200).json({ success: true, message: "Already added in Wish List.", updatedWishList: products });
}
if (data.length === 0) {
await connection
.promise()
.query(
`INSERT INTO userwishlist (user_id, product_id, created_at) VALUES (?, ?, ?)`,
[user_id, product_id, new Date()]
);
const [findProduct_id] = await connection
.promise()
.query(`SELECT product_id FROM userwishlist WHERE user_id = ?`, [user_id]);
const productArray = findProduct_id.map((item) => item.product_id);
const idsString = productArray.join(", ");
const query = `SELECT * FROM products WHERE id IN (${idsString})`;
const [products] = await connection.promise().query(query);
res.status(200).json({success: true, message: "Product added in wishList", updatedWishList: products });
}
} else {
res.status(401).json({ success: false, message: "User Not loged in." });
}
} catch (err) {
console.log(err);
res.status(500).json({ message: "Error in add-to-whishlist" });
}
});
// for user only
router.post("/wishlist", async (req, res) => {
try {
const { user_id } = req.body;
const token = req.headers.authorization.split(" ")[1]; // Extract user ID from Bearer token
const isValid = validateJSONToken(token);
if (!isValid) {
return res.status(401).json({ success: false, message: "Invalid User." });
}
const [product_id] = await connection
.promise()
.query(`SELECT product_id FROM userwishlist WHERE user_id = ?`, [user_id]);
if (product_id.length === 0) {
return res.status(200).json({ message: "No products found." });
}
const productArray = product_id.map((item) => item.product_id);
const idsString = productArray.join(", ");
const query = `SELECT * FROM products WHERE id IN (${idsString})`;
const [products] = await connection.promise().query(query);
res.status(200).json({ products });
} catch (err) {
console.error(err);
res.status(400).json({ success: false, message: "Error in Wishlist" });
}
});
module.exports = router