ShopBot/utils/database/cart.py

25 lines
820 B
Python
Raw Normal View History

2022-06-04 22:02:51 +03:00
from .model import Cart
2022-06-16 00:32:17 +03:00
def add_to_cart(user_id: int, item_id: int):
2022-06-04 22:02:51 +03:00
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):
2022-06-16 00:32:17 +03:00
Cart.delete().where(Cart.user_id == user_id, Cart.product_id == item_id).execute()
2022-06-04 22:02:51 +03:00
def clean_cart(user_id: int):
2022-06-16 00:32:17 +03:00
Cart.delete().where(Cart.user_id == user_id).execute()
2022-06-04 22:02:51 +03:00
2022-06-05 15:02:41 +03:00
2022-06-16 00:32:17 +03:00
def get_user_cart(user_id: int):
2022-06-04 22:02:51 +03:00
cart = []
for i in Cart.select().where(Cart.user_id == user_id):
cart.append((i.product_id, i.count))
2022-06-16 00:32:17 +03:00
return cart