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

31 lines
1.1 KiB
Python

from sqlalchemy import Column, String, Float, Integer, ForeignKey, UniqueConstraint
from sqlalchemy.orm import relationship
from app.models.base_model import BaseModel
class Cart(BaseModel):
"""Model for shopping cart."""
__tablename__ = "carts"
user_id = Column(String(255), nullable=False, index=True)
is_active = Column(Integer, default=1, nullable=False)
items = relationship("CartItem", back_populates="cart", cascade="all, delete-orphan")
class CartItem(BaseModel):
"""Model for items in shopping cart."""
__tablename__ = "cart_items"
__table_args__ = (
UniqueConstraint("cart_id", "product_id", name="uix_cart_product"),
)
cart_id = Column(Integer, ForeignKey("carts.id", ondelete="CASCADE"), nullable=False)
product_id = Column(Integer, ForeignKey("products.id", ondelete="CASCADE"), nullable=False)
quantity = Column(Integer, nullable=False, default=1)
unit_price = Column(Float, nullable=False) # Stores the price at the time of adding to cart
cart = relationship("Cart", back_populates="items")
product = relationship("Product")