Initial commit

This commit is contained in:
2026-09-16 17:36:46 +05:30
commit c9f429d123
117 changed files with 14145 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
const Wishlist = require('../models/Wishlist');
const { successResponse, errorResponse } = require('../utils/apiResponse');
const getWishlist = async (req, res, next) => {
try {
let wishlist = await Wishlist.findOne({ user: req.user._id }).populate('products');
if (!wishlist) {
wishlist = await Wishlist.create({ user: req.user._id, products: [] });
}
return successResponse(res, 'Wishlist retrieved', wishlist);
} catch (error) {
next(error);
}
};
const toggleWishlist = async (req, res, next) => {
try {
const { productId } = req.body;
let wishlist = await Wishlist.findOne({ user: req.user._id });
if (!wishlist) {
wishlist = new Wishlist({ user: req.user._id, products: [productId] });
} else {
const exists = wishlist.products.includes(productId);
if (exists) {
wishlist.products.pull(productId);
} else {
wishlist.products.push(productId);
}
}
await wishlist.save();
const updated = await Wishlist.findById(wishlist._id).populate('products');
return successResponse(res, 'Wishlist updated', updated);
} catch (error) {
next(error);
}
};
module.exports = { getWishlist, toggleWishlist };