From e069dedcdab44de79c8322ed19beb3e334b78939 Mon Sep 17 00:00:00 2001 From: Thastertyn Date: Fri, 8 Mar 2024 14:04:40 +0100 Subject: [PATCH] Initial work on cart management --- app/__init__.py | 3 ++- app/api/__init__.py | 1 + app/api/routes/__init__.py | 2 +- app/api/routes/cart_routes.py | 16 ++++++++++++++++ app/services/cart_service.py | 30 ++++++++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 app/api/routes/cart_routes.py create mode 100644 app/services/cart_service.py diff --git a/app/__init__.py b/app/__init__.py index 16aa9a6..6635132 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -5,11 +5,12 @@ app = Flask(__name__) jwt_manager = JWTManager(app) def create_app(): - from app.api import bp, bp_errors, bp_product, bp_user + from app.api import bp, bp_errors, bp_product, bp_user, bp_cart app.register_blueprint(bp) app.register_blueprint(bp_errors) app.register_blueprint(bp_product) app.register_blueprint(bp_user) + app.register_blueprint(bp_cart) from app.config import FlaskTesting, FlaskProduction app.config.from_object(FlaskTesting) diff --git a/app/api/__init__.py b/app/api/__init__.py index 152f96d..c0c21b1 100644 --- a/app/api/__init__.py +++ b/app/api/__init__.py @@ -4,5 +4,6 @@ bp_errors = Blueprint('errors', __name__) bp = Blueprint('api', __name__) bp_product = Blueprint('product', __name__, url_prefix="/product") bp_user = Blueprint('user', __name__, url_prefix="/user") +bp_cart = Blueprint('cart', __name__, url_prefix="/cart") from . import routes \ No newline at end of file diff --git a/app/api/routes/__init__.py b/app/api/routes/__init__.py index 9a49a8c..a342a91 100644 --- a/app/api/routes/__init__.py +++ b/app/api/routes/__init__.py @@ -1 +1 @@ -from . import main_routes,error_routes, product_routes, user_routes \ No newline at end of file +from app.api.routes import main_routes, error_routes, product_routes, user_routes, cart_routes \ No newline at end of file diff --git a/app/api/routes/cart_routes.py b/app/api/routes/cart_routes.py new file mode 100644 index 0000000..e762766 --- /dev/null +++ b/app/api/routes/cart_routes.py @@ -0,0 +1,16 @@ +from flask import jsonify, abort, request +from flask_jwt_extended import jwt_required, get_jwt_identity + +from app.api import bp_cart + +from app.services.cart_service import CartService + +@bp_cart.route('add/', methods=['PUT']) +@jwt_required() +def add_to_cart(product_id: int): + user_id = get_jwt_identity() + count = request.args.get('count', default=1, type=int) + + result, status_code = CartService.add_to_cart(user_id, product_id, count) + + return result, status_code diff --git a/app/services/cart_service.py b/app/services/cart_service.py new file mode 100644 index 0000000..6eeb8c2 --- /dev/null +++ b/app/services/cart_service.py @@ -0,0 +1,30 @@ +from mysql.connector import Error +from typing import Tuple, Union + +from app.extensions import db_cursor, db_connection + +class CartService: + + @staticmethod + def add_to_cart(user_id: str, product_id: int, count: int) -> Tuple[Union[dict, str], int]: + try: + with db_connection.cursor() as cursor: + db_connection.begin() + + db_cursor.execute("select * from cart_item where cart_id = %s and product_id = %s", (user_id, product_id)) + + if db_cursor.rowcount != 0: + db_cursor.execute("update cart_item set count = count + %s where cart_id = %s and product_id = %s", (count, user_id, product_id)) + else: + db_cursor.execute("insert into cart_item(cart_id, product_id, count) values (%s, %s, %s)", (user_id, product_id, count)) + + db_connection.commit() + + return {"Success": "Successfully added to cart"}, 200 + + except Error as e: + return {"Failed": f"Failed to add item to cart. {e}"} + + @staticmethod + def show_cart(user_id: str): + return None \ No newline at end of file