Initial work on cart management
This commit is contained in:
parent
eb57a10bbe
commit
e069dedcda
@ -5,11 +5,12 @@ app = Flask(__name__)
|
|||||||
jwt_manager = JWTManager(app)
|
jwt_manager = JWTManager(app)
|
||||||
|
|
||||||
def create_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)
|
||||||
app.register_blueprint(bp_errors)
|
app.register_blueprint(bp_errors)
|
||||||
app.register_blueprint(bp_product)
|
app.register_blueprint(bp_product)
|
||||||
app.register_blueprint(bp_user)
|
app.register_blueprint(bp_user)
|
||||||
|
app.register_blueprint(bp_cart)
|
||||||
|
|
||||||
from app.config import FlaskTesting, FlaskProduction
|
from app.config import FlaskTesting, FlaskProduction
|
||||||
app.config.from_object(FlaskTesting)
|
app.config.from_object(FlaskTesting)
|
||||||
|
@ -4,5 +4,6 @@ bp_errors = Blueprint('errors', __name__)
|
|||||||
bp = Blueprint('api', __name__)
|
bp = Blueprint('api', __name__)
|
||||||
bp_product = Blueprint('product', __name__, url_prefix="/product")
|
bp_product = Blueprint('product', __name__, url_prefix="/product")
|
||||||
bp_user = Blueprint('user', __name__, url_prefix="/user")
|
bp_user = Blueprint('user', __name__, url_prefix="/user")
|
||||||
|
bp_cart = Blueprint('cart', __name__, url_prefix="/cart")
|
||||||
|
|
||||||
from . import routes
|
from . import routes
|
@ -1 +1 @@
|
|||||||
from . import main_routes,error_routes, product_routes, user_routes
|
from app.api.routes import main_routes, error_routes, product_routes, user_routes, cart_routes
|
16
app/api/routes/cart_routes.py
Normal file
16
app/api/routes/cart_routes.py
Normal file
@ -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/<int:product_id>', 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
|
30
app/services/cart_service.py
Normal file
30
app/services/cart_service.py
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user