Automated Action a28e115e4f Implement simple cart system with FastAPI and SQLite
- Set up project structure with FastAPI and SQLite
- Create models for products and cart items
- Implement schemas for API request/response validation
- Add database migrations with Alembic
- Create service layer for business logic
- Implement API endpoints for cart operations
- Add health endpoint for monitoring
- Update documentation
- Fix linting issues
2025-05-18 23:36:40 +00:00

109 lines
3.2 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, Header, status
from sqlalchemy.orm import Session
from app.db.session import get_db
from app.schemas.cart import CartItemCreate, CartItemUpdate, CartDetail, CartItemResponse
from app.schemas.common import DataResponse, ResponseBase
from app.services import cart_service
router = APIRouter(prefix="/cart", tags=["cart"])
@router.get("", response_model=DataResponse[CartDetail])
def get_cart(
user_id: str = Header(..., description="User ID for cart identification"),
db: Session = Depends(get_db)
):
"""
Get the current user's cart with all items.
"""
cart = cart_service.get_active_cart(db, user_id)
cart_detail = cart_service.get_cart_detail(db, cart.id)
return {
"success": True,
"message": "Cart retrieved successfully",
"data": cart_detail
}
@router.post("/items", response_model=DataResponse[CartItemResponse], status_code=status.HTTP_201_CREATED)
def add_to_cart(
item_data: CartItemCreate,
user_id: str = Header(..., description="User ID for cart identification"),
db: Session = Depends(get_db)
):
"""
Add a product to the user's cart.
"""
cart_item, is_new = cart_service.add_item_to_cart(db, user_id, item_data)
status_message = "Item added to cart successfully" if is_new else "Item quantity updated successfully"
return {
"success": True,
"message": status_message,
"data": cart_item
}
@router.put("/items/{item_id}", response_model=DataResponse[CartItemResponse])
def update_cart_item(
item_id: int,
item_data: CartItemUpdate,
user_id: str = Header(..., description="User ID for cart identification"),
db: Session = Depends(get_db)
):
"""
Update the quantity of an item in the user's cart.
"""
cart_item = cart_service.update_cart_item(db, user_id, item_id, item_data)
if not cart_item:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Cart item with ID {item_id} not found in user's cart"
)
return {
"success": True,
"message": "Cart item updated successfully",
"data": cart_item
}
@router.delete("/items/{item_id}", response_model=ResponseBase)
def remove_cart_item(
item_id: int,
user_id: str = Header(..., description="User ID for cart identification"),
db: Session = Depends(get_db)
):
"""
Remove an item from the user's cart.
"""
success = cart_service.remove_cart_item(db, user_id, item_id)
if not success:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Cart item with ID {item_id} not found in user's cart"
)
return {
"success": True,
"message": "Item removed from cart successfully"
}
@router.delete("", response_model=ResponseBase)
def clear_cart(
user_id: str = Header(..., description="User ID for cart identification"),
db: Session = Depends(get_db)
):
"""
Remove all items from the user's cart.
"""
cart_service.clear_cart(db, user_id)
return {
"success": True,
"message": "Cart cleared successfully"
}