# models/database/cart.py """Cart item database model.""" from datetime import datetime from sqlalchemy import Column, Float, ForeignKey, Index, Integer, String, UniqueConstraint from sqlalchemy.orm import relationship from app.core.database import Base from models.database.base import TimestampMixin class CartItem(Base, TimestampMixin): """ Shopping cart items. Stores cart items per session, vendor, and product. Sessions are identified by a session_id string (from browser cookies). """ __tablename__ = "cart_items" id = Column(Integer, primary_key=True, index=True) vendor_id = Column(Integer, ForeignKey("vendors.id"), nullable=False) product_id = Column(Integer, ForeignKey("products.id"), nullable=False) session_id = Column(String(255), nullable=False, index=True) # Cart details quantity = Column(Integer, nullable=False, default=1) price_at_add = Column(Float, nullable=False) # Store price when added to cart # Relationships vendor = relationship("Vendor") product = relationship("Product") # Constraints __table_args__ = ( UniqueConstraint("vendor_id", "session_id", "product_id", name="uq_cart_item"), Index("idx_cart_session", "vendor_id", "session_id"), Index("idx_cart_created", "created_at"), # For cleanup of old carts ) def __repr__(self): return f"" @property def line_total(self) -> float: """Calculate line total.""" return self.price_at_add * self.quantity