from typing import Any from fastapi import APIRouter, Depends, HTTPException, status from sqlalchemy.orm import Session from app import crud, models, schemas from app.api import deps router = APIRouter() @router.get("/", response_model=schemas.Cart) def read_user_cart( db: Session = Depends(deps.get_db), current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Get current user's cart with all items. """ cart = crud.cart.get_cart_with_items(db, user_id=current_user.id) if not cart: # Create a new cart for the user if it doesn't exist cart = crud.cart.create(db, obj_in=schemas.CartCreate(user_id=current_user.id)) return cart @router.post("/items", response_model=schemas.CartItem) def add_cart_item( *, db: Session = Depends(deps.get_db), item_in: schemas.CartItemCreate, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Add item to cart. If item already exists, update quantity. """ # Verify the product exists and is active product = crud.product.get(db, id=item_in.product_id) if not product or not product.is_active: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Product not found or inactive", ) # Check if product is in stock if product.stock < item_in.quantity: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=f"Not enough stock. Available: {product.stock}", ) # Get or create user's cart cart = crud.cart.get_or_create_cart(db, user_id=current_user.id) # Add or update cart item cart_item = crud.cart_item.create_or_update_cart_item( db, cart_id=cart.id, product_id=item_in.product_id, quantity=item_in.quantity ) return cart_item @router.put("/items/{product_id}", response_model=schemas.CartItem) def update_cart_item( *, db: Session = Depends(deps.get_db), product_id: int, item_in: schemas.CartItemUpdate, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Update cart item quantity. """ # Verify the product exists and is active product = crud.product.get(db, id=product_id) if not product or not product.is_active: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Product not found or inactive", ) # Check if product is in stock if product.stock < item_in.quantity: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=f"Not enough stock. Available: {product.stock}", ) # Get user's cart cart = crud.cart.get_by_user_id(db, user_id=current_user.id) if not cart: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Cart not found", ) # Update cart item cart_item = crud.cart_item.create_or_update_cart_item( db, cart_id=cart.id, product_id=product_id, quantity=item_in.quantity ) return cart_item @router.delete("/items/{product_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None) def remove_cart_item( *, db: Session = Depends(deps.get_db), product_id: int, current_user: models.User = Depends(deps.get_current_active_user), ) -> None: """ Remove item from cart. """ # Get user's cart cart = crud.cart.get_by_user_id(db, user_id=current_user.id) if not cart: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Cart not found", ) # Remove cart item crud.cart_item.remove_cart_item(db, cart_id=cart.id, product_id=product_id) @router.delete("/", status_code=status.HTTP_204_NO_CONTENT, response_model=None) def clear_cart( *, db: Session = Depends(deps.get_db), current_user: models.User = Depends(deps.get_current_active_user), ) -> None: """ Clear all items from cart. """ # Get user's cart cart = crud.cart.get_by_user_id(db, user_id=current_user.id) if not cart: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Cart not found", ) # Clear cart crud.cart_item.clear_cart(db, cart_id=cart.id)