-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwishlist.controller.ts
104 lines (88 loc) · 2.97 KB
/
wishlist.controller.ts
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
101
102
103
104
import { Request, Response } from "express";
import Wishlist from "../database/models/wishlist";
import WishlistItem from "../database/models/wishlistItem";
import User from "../database/models/user";
import Product from "../database/models/product";
export const fetchWishlist = async (req: Request, res: Response) => {
try {
const { userId } = req.params;
const wishlist = await Wishlist.findOne({ where: { userId } });
if (!wishlist) {
return res.status(404).json({ message: "Wishlist not found" });
}
const wishlistItems = await WishlistItem.findAll({
where: { wishlistId: wishlist.wishlistId },
});
return res.status(200).json({ wishlist: wishlistItems });
} catch (error: any) {
res.status(500).json({ success: false, message: error.message });
}
};
export const addToWishlist = async (req: Request, res: Response) => {
try {
const { userId, productId, price } = req.body;
let wishlist = await Wishlist.findOne({ where: { userId } });
if (!wishlist) {
wishlist = await Wishlist.create({ userId });
await User.update({ wishlistId: wishlist.wishlistId }, { where: { userId } });
const wishlistItem = await WishlistItem.create({
wishlistId: wishlist.wishlistId,
productId,
});
if (wishlistItem) {
return res
.status(200)
.json({ message: "Wishlist added successfully!", wishlist: wishlistItem });
}
} else {
const existedProduct = await WishlistItem.findOne({
where: {
wishlistId: wishlist.wishlistId,
productId,
},
});
if (existedProduct) {
return res.status(409).json({ message: "Product already exists in wishlist" });
}
const wishlistItem = await WishlistItem.create({
wishlistId: wishlist.wishlistId,
productId,
price,
});
if (wishlistItem) {
return res
.status(200)
.json({ message: "Wishlist item added successfully!", wishlist: wishlistItem });
}
}
} catch (error: any) {
res.status(500).json({ success: false, message: error.message });
}
};
export const removeFromWishlist = async (req: Request, res: Response) => {
try {
const { userId, productId } = req.body;
const wishlist = await Wishlist.findOne({ where: { userId } });
if (!wishlist) {
return res.status(404).json({ message: "Wishlist not found" });
}
const wishlistItem = await WishlistItem.findOne({
where: {
wishlistId: wishlist.wishlistId,
productId,
},
});
if (!wishlistItem) {
return res.status(404).json({ message: "Product not found in wishlist" });
}
await WishlistItem.destroy({
where: {
wishlistId: wishlist.wishlistId,
productId,
},
});
return res.status(200).json({ message: "Product removed from wishlist successfully" });
} catch (error: any) {
res.status(500).json({ success: false, message: error.message });
}
};