Skip to content

Commit e6dbe60

Browse files
committed
componenets: wishlist[controller + routes]
1 parent 206a6b4 commit e6dbe60

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { catchAsyncError } from "../../utils/catchAsyncError.js";
2+
import { AppError } from "../../utils/AppError.js";
3+
import { userModel } from "../../../Database/models/user.model.js";
4+
5+
const addToWishList = catchAsyncError(async (req, res, next) => {
6+
const { productId } = req.body;
7+
8+
const addToWishList = await userModel.findByIdAndUpdate(
9+
req.user._id,
10+
{ $addToSet: { wishlist: productId } },
11+
{
12+
new: true,
13+
}
14+
);
15+
16+
addToWishList &&
17+
res
18+
.status(201)
19+
.json({ message: "success", addToWishList: addToWishList.wishlist });
20+
21+
!addToWishList && next(new AppError("WishList was not found", 404));
22+
});
23+
24+
const removeFromWishList = catchAsyncError(async (req, res, next) => {
25+
const { productId } = req.body;
26+
27+
const removeFromWishList = await userModel.findByIdAndUpdate(
28+
req.user._id,
29+
{ $pull: { wishlist: productId } },
30+
{
31+
new: true,
32+
}
33+
);
34+
35+
removeFromWishList &&
36+
res
37+
.status(201)
38+
.json({
39+
message: "success",
40+
removeFromWishList: removeFromWishList.wishlist,
41+
});
42+
43+
!removeFromWishList && next(new AppError("WishList was not found", 404));
44+
});
45+
46+
const getAllUserWishList = catchAsyncError(async (req, res, next) => {
47+
const getAllUserWishList = await userModel
48+
.findOne({ _id: req.user._id })
49+
.populate("wishlist");
50+
51+
getAllUserWishList &&
52+
res
53+
.status(201)
54+
.json({
55+
message: "success",
56+
getAllUserWishList: getAllUserWishList.wishlist,
57+
});
58+
59+
!getAllUserWishList && next(new AppError("WishList was not found", 404));
60+
});
61+
62+
export { addToWishList, removeFromWishList, getAllUserWishList };
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import express from "express";
2+
import { validate } from '../../middleware/validation.js';
3+
import {
4+
addToWishListValidation,
5+
deleteFromWishListValidation,
6+
} from "./wishlist_validation.js";
7+
import { allowedTo, protectedRoutes } from "../auth/auth_controller.js";
8+
import * as wishlist from "../wishlist/wishlist_controller.js";
9+
10+
const wishListRouter = express.Router();
11+
12+
wishListRouter.route("/").patch(protectedRoutes, allowedTo("user"), validate(addToWishListValidation), wishlist.addToWishList).delete(protectedRoutes, allowedTo("user"), validate(deleteFromWishListValidation), wishlist.removeFromWishList).get(protectedRoutes, allowedTo("user"), wishlist.getAllUserWishList);
13+
14+
export default wishListRouter;

0 commit comments

Comments
 (0)