-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcart.py
More file actions
60 lines (49 loc) · 1.83 KB
/
cart.py
File metadata and controls
60 lines (49 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from fastapi import APIRouter, Depends, HTTPException
from models import User, Cart, CartItem, Product
from authentication import get_current_user
router = APIRouter()
@router.post("/cart/add")
async def add_to_cart(
product_id: int, quantity: int, user: User = Depends(get_current_user)
):
product = await Product.get(id=product_id)
cart, _ = await Cart.get_or_create(user=user)
cart_item, created = await CartItem.get_or_create(cart=cart, product=product)
if not created:
cart_item.quantity += quantity
else:
cart_item.quantity = quantity
cart_item.price = product.new_price
await cart_item.save()
return {"message": "Item added to cart"}
@router.get("/cart")
async def view_cart(user: User = Depends(get_current_user)):
cart, _ = await Cart.get_or_create(user=user)
items = await CartItem.filter(cart=cart).prefetch_related("product")
total = sum(item.quantity * item.price for item in items)
return {
"items": [
{
"product": item.product.name,
"quantity": item.quantity,
"price": item.price,
"total": item.quantity * item.price,
}
for item in items
],
"total": total,
}
@router.put("/cart/update/{item_id}")
async def update_cart_item(
item_id: int, quantity: int, user: User = Depends(get_current_user)
):
cart = await Cart.get(user=user)
item = await CartItem.get(id=item_id, cart=cart)
item.quantity = quantity
await item.save()
return {"message": "Cart item updated"}
@router.delete("/cart/remove/{item_id}")
async def remove_from_cart(item_id: int, user: User = Depends(get_current_user)):
cart = await Cart.get(user=user)
await CartItem.filter(id=item_id, cart=cart).delete()
return {"message": "Item removed from cart"}