Update code in endpoints/cart.get.py
This commit is contained in:
parent
23d8c46b36
commit
4312c60436
@ -0,0 +1,42 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from core.database import fake_users_db
|
||||
from core.auth import get_current_user_dummy
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
# Demo cart data
|
||||
fake_cart_db = {
|
||||
"demo": [
|
||||
{"id": "1", "name": "Product 1", "price": 29.99, "quantity": 2},
|
||||
{"id": "2", "name": "Product 2", "price": 19.99, "quantity": 1}
|
||||
]
|
||||
}
|
||||
|
||||
@router.get("/cart")
|
||||
async def get_cart_items(
|
||||
current_user: dict = Depends(get_current_user_dummy)
|
||||
):
|
||||
"""Get list of items in user's cart"""
|
||||
username = current_user.get("username", "demo")
|
||||
|
||||
if username not in fake_cart_db:
|
||||
return {
|
||||
"message": "Cart is empty",
|
||||
"items": [],
|
||||
"metadata": {
|
||||
"total_items": 0,
|
||||
"total_value": 0.00
|
||||
}
|
||||
}
|
||||
|
||||
cart_items = fake_cart_db[username]
|
||||
total_value = sum(item["price"] * item["quantity"] for item in cart_items)
|
||||
|
||||
return {
|
||||
"message": "Cart items retrieved successfully",
|
||||
"items": cart_items,
|
||||
"metadata": {
|
||||
"total_items": len(cart_items),
|
||||
"total_value": total_value
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user