33 lines
1.3 KiB
Python
33 lines
1.3 KiB
Python
from uuid import uuid4
|
|
|
|
from sqlalchemy import Column, DateTime, Float, ForeignKey, Integer, String, Text
|
|
from sqlalchemy.orm import relationship
|
|
from sqlalchemy.sql import func
|
|
|
|
from app.core.database import Base
|
|
|
|
|
|
class CartItem(Base):
|
|
__tablename__ = "cart_items"
|
|
|
|
id = Column(String(36), primary_key=True, default=lambda: str(uuid4()))
|
|
user_id = Column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
|
|
product_id = Column(String(36), ForeignKey("products.id", ondelete="CASCADE"), nullable=False)
|
|
quantity = Column(Integer, default=1, nullable=False)
|
|
price_at_addition = Column(Float, nullable=False) # Price when added to cart
|
|
custom_properties = Column(Text, nullable=True) # JSON string for custom product properties
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
|
|
|
# Relationships
|
|
user = relationship("User", back_populates="cart_items")
|
|
product = relationship("Product", back_populates="cart_items")
|
|
|
|
def __repr__(self):
|
|
return f"<CartItem {self.id} for Product {self.product_id}>"
|
|
|
|
@property
|
|
def subtotal(self):
|
|
"""Calculate the subtotal for this cart item"""
|
|
return self.price_at_addition * self.quantity
|