25 lines
820 B
Python
25 lines
820 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
|