Initial work on cart management
This commit is contained in:
1 parent
eb57a10bbe
commit
e069dedcda
5 files changed
+50
-2
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user