
- Created FastAPI application with SQLite database - Implemented cart management (create, get, add items, remove items, clear) - Added checkout functionality with payment processing simulation - Set up database models using SQLAlchemy with Cart and CartItem tables - Configured Alembic for database migrations - Added comprehensive API documentation and examples - Included health endpoint and CORS support - Formatted code with ruff linting
34 lines
1.0 KiB
Python
34 lines
1.0 KiB
Python
from sqlalchemy import Column, Integer, String, Float, DateTime, ForeignKey
|
|
from sqlalchemy.orm import relationship
|
|
from sqlalchemy.sql import func
|
|
from app.db.base import Base
|
|
|
|
|
|
class Cart(Base):
|
|
__tablename__ = "carts"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
user_id = Column(String, index=True)
|
|
status = Column(String, default="active") # active, checked_out, abandoned
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
updated_at = Column(
|
|
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
|
|
)
|
|
|
|
items = relationship(
|
|
"CartItem", back_populates="cart", cascade="all, delete-orphan"
|
|
)
|
|
|
|
|
|
class CartItem(Base):
|
|
__tablename__ = "cart_items"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
cart_id = Column(Integer, ForeignKey("carts.id"))
|
|
product_id = Column(String, index=True)
|
|
product_name = Column(String)
|
|
price = Column(Float)
|
|
quantity = Column(Integer)
|
|
|
|
cart = relationship("Cart", back_populates="items")
|