Skip to content

Commit

Permalink
added clearing cart controller
Browse files Browse the repository at this point in the history
  • Loading branch information
bhanu committed Oct 21, 2024
1 parent fbc3f40 commit 455a84b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
19 changes: 19 additions & 0 deletions server/controllers/userControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,25 @@ exports.getUserCart = async (req, res) => {
}
};

exports.clearCart = async (req, res) => {
const { userId } = req.params;

try {
const user = await User.findById(userId);

if (!user) {
return res.status(404).json({ message: "User not found" });
}

user.cart = [];
await user.save();

return res.status(200).json({ message: "Cart cleared successfully" });
} catch (error) {
return res.status(500).json({ error: error.message });
}
};

exports.addToBookmarks = async (req, res) => {
const { userId, restaurantId } = req.body;

Expand Down
2 changes: 2 additions & 0 deletions server/routes/userRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ const {
removeFromCart,
removeFromBookmarks,
getBookmarks,
clearCart,
} = userControllers;

router.route("/details").get(getUserDetails);
router.route("/update-details").put(updateUserDetails);
router.route("/add-to-cart").post(addToCart);
router.route("/get-cart/:userId").get(getUserCart);
router.route("/clear-cart/:userId").post(clearCart);
router.route("/remove-from-cart").delete(removeFromCart);
router.route("/add-to-bookmarks").post(addToBookmarks);
router.route("/get-bookmarks").get(getBookmarks);
Expand Down

0 comments on commit 455a84b

Please sign in to comment.