30 lines
967 B
Python
30 lines
967 B
Python
|
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
|