-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
bharathi
committed
Apr 17, 2024
1 parent
e611ef2
commit 30cfdc6
Showing
2 changed files
with
36 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,41 @@ | ||
from flask import make_response | ||
from flask_restful import Resource | ||
from flask import jsonify | ||
from flask_restful import request | ||
|
||
from db.cart import get_cart_contents | ||
from flask_restful import Resource | ||
|
||
try: | ||
from src.db.categories import get_all_categories, get_category_by_id, search_categories | ||
from src.db.cart import get_cart_contents | ||
except ImportError: | ||
from db.categories import get_all_categories, get_category_by_id, search_categories | ||
|
||
from db.cart import get_cart_contents, add_item_to_cart, remove_item_from_cart | ||
|
||
class Cart(Resource): | ||
|
||
def get(self, user_id): | ||
"""Fetches all items in a user's shopping cart.""" | ||
if not user_id: | ||
return jsonify({"error": "User ID is required"}), 400 | ||
def get(user_id): | ||
"""Fetches all items in a user's shopping cart by their user ID.""" | ||
try: | ||
cart_items = get_cart_contents(user_id) | ||
if not cart_items: | ||
return jsonify( | ||
{"message": "No items found in the cart"} | ||
), 404 | ||
return jsonify( | ||
{"cart_items": cart_items} | ||
) | ||
return jsonify({"error": "No items found in the cart"}), 404 | ||
return jsonify({"cart_items": cart_items}) | ||
except Exception as e: | ||
return jsonify({"error": str(e)}), 500 | ||
|
||
|
||
class AddToCart(Resource): | ||
def post(self, user_id, product_id, quantity): | ||
"""Adds a new item to the shopping cart or updates the quantity if it already exists.""" | ||
try: | ||
result = add_item_to_cart(user_id, product_id, quantity) | ||
if result: | ||
return jsonify({'message': 'Item added to cart successfully'}), 200 | ||
else: | ||
return jsonify({'error': 'Failed to add item to cart'}), 500 | ||
except Exception as e: | ||
return jsonify({"error": str(e)}), 500 | ||
|
||
|
||
class RemoveFromCart(Resource): | ||
def delete(self, user_id, product_id): | ||
"""Removes an item from the shopping cart.""" | ||
try: | ||
remove_item_from_cart(user_id, product_id) | ||
return jsonify({'message': 'Item removed from cart successfully'}), 200 | ||
except Exception as e: | ||
return jsonify({"error": str(e)}), 500 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters