ShopBot/utils/database/cart.py
2022-06-05 15:02:41 +03:00

24 lines
811 B
Python

from .model import Cart
def add_to_cart(user_id:int, item_id: int):
if Cart.select().where(Cart.user_id==user_id, Cart.product_id==item_id).exists():
count = Cart.get(Cart.user_id==user_id, Cart.product_id==item_id).count
Cart.update(count=count+1).where(Cart.user_id==user_id, Cart.product_id==item_id).execute()
return
Cart.insert(user_id=user_id, product_id=item_id, count=1).execute()
def del_from_cart(user_id: int, item_id: int):
Cart.delete().where(Cart.user_id==user_id, Cart.product_id==item_id).execute()
def clean_cart(user_id: int):
Cart.delete().where(Cart.user_id==user_id).execute()
def get_user_cart(user_id:int):
cart = []
for i in Cart.select().where(Cart.user_id == user_id):
cart.append((i.product_id, i.count))
return cart