Skip to content

Commit

Permalink
updating the cart_api
Browse files Browse the repository at this point in the history
  • Loading branch information
bharathi committed Apr 17, 2024
1 parent e611ef2 commit 30cfdc6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
52 changes: 33 additions & 19 deletions PCP/server/src/api/cart_api.py
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
4 changes: 3 additions & 1 deletion PCP/server/tests/1_api/test_3_logout_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from tests.test_utils import *

from api.cart_api import get


class LogOutApiTestCase(unittest.TestCase):
SignUp_URL = 'http://localhost:5000/signUp'
Expand Down Expand Up @@ -40,7 +42,7 @@ def test_1_successful_logout(self, mock_post):
self.assertEqual(response.status_code, 200)

# Simulating extracting session key from login response
session_key = response.json().get('sessionKey')
session_key = response.get('sessionKey')
self.assertIsNotNone(session_key, "Session key should not be None")

# Simulating logout with the session key
Expand Down

0 comments on commit 30cfdc6

Please sign in to comment.