Spaces:
Sleeping
Sleeping
Upload app/models/cart.py
Browse files- app/models/cart.py +45 -0
app/models/cart.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime, timezone
|
| 2 |
+
from sqlalchemy import Column, Integer, String, Float, DateTime, ForeignKey, Boolean
|
| 3 |
+
from sqlalchemy.orm import relationship
|
| 4 |
+
|
| 5 |
+
from app.db.base import Base
|
| 6 |
+
|
| 7 |
+
class Coupon(Base):
|
| 8 |
+
__tablename__ = "coupons"
|
| 9 |
+
|
| 10 |
+
id = Column(Integer, primary_key=True, autoincrement=True)
|
| 11 |
+
code = Column(String(50), unique=True, index=True, nullable=False)
|
| 12 |
+
discount_percent = Column(Float, nullable=False)
|
| 13 |
+
is_active = Column(Boolean, default=True, nullable=False)
|
| 14 |
+
expires_at = Column(DateTime, nullable=True)
|
| 15 |
+
created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc), nullable=False)
|
| 16 |
+
|
| 17 |
+
carts = relationship("Cart", back_populates="coupon")
|
| 18 |
+
|
| 19 |
+
class Cart(Base):
|
| 20 |
+
__tablename__ = "carts"
|
| 21 |
+
|
| 22 |
+
id = Column(Integer, primary_key=True, autoincrement=True)
|
| 23 |
+
user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=True)
|
| 24 |
+
session_id = Column(String(255), unique=True, index=True, nullable=True)
|
| 25 |
+
coupon_id = Column(Integer, ForeignKey("coupons.id", ondelete="SET NULL"), nullable=True)
|
| 26 |
+
created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc), nullable=False)
|
| 27 |
+
updated_at = Column(DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc), nullable=False)
|
| 28 |
+
|
| 29 |
+
user = relationship("User", backref="cart")
|
| 30 |
+
coupon = relationship("Coupon", back_populates="carts")
|
| 31 |
+
items = relationship("CartItem", back_populates="cart", cascade="all, delete-orphan")
|
| 32 |
+
|
| 33 |
+
class CartItem(Base):
|
| 34 |
+
__tablename__ = "cart_items"
|
| 35 |
+
|
| 36 |
+
id = Column(Integer, primary_key=True, autoincrement=True)
|
| 37 |
+
cart_id = Column(Integer, ForeignKey("carts.id", ondelete="CASCADE"), nullable=False)
|
| 38 |
+
product_id = Column(Integer, ForeignKey("products.id", ondelete="CASCADE"), nullable=False)
|
| 39 |
+
quantity = Column(Integer, default=1, nullable=False)
|
| 40 |
+
unit_price = Column(Float, nullable=False) # snapshot of price when added/updated
|
| 41 |
+
created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc), nullable=False)
|
| 42 |
+
updated_at = Column(DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc), nullable=False)
|
| 43 |
+
|
| 44 |
+
cart = relationship("Cart", back_populates="items")
|
| 45 |
+
product = relationship("Product")
|