
* Created FastAPI application structure * Added database models for assets, exchanges, markets, and rates * Integrated with CoinCap API * Implemented REST API endpoints * Setup SQLite persistence with Alembic migrations * Added comprehensive documentation Generated with BackendIM... (backend.im)
46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
from sqlalchemy import Column, String, Float, Integer, DateTime, ForeignKey
|
|
from sqlalchemy.orm import relationship
|
|
from datetime import datetime
|
|
|
|
from app.core.database import Base
|
|
|
|
|
|
class Asset(Base):
|
|
__tablename__ = "assets"
|
|
|
|
id = Column(String, primary_key=True, index=True) # Slug (e.g., "bitcoin")
|
|
rank = Column(Integer)
|
|
symbol = Column(String, index=True)
|
|
name = Column(String, index=True)
|
|
supply = Column(Float)
|
|
max_supply = Column(Float, nullable=True)
|
|
market_cap_usd = Column(Float)
|
|
volume_usd_24hr = Column(Float)
|
|
price_usd = Column(Float)
|
|
change_percent_24hr = Column(Float, nullable=True)
|
|
vwap_24hr = Column(Float, nullable=True)
|
|
explorer = Column(String, nullable=True)
|
|
|
|
# Last updated timestamp
|
|
last_updated = Column(DateTime, default=datetime.utcnow)
|
|
|
|
# Relationships
|
|
price_history = relationship("AssetPriceHistory", back_populates="asset", cascade="all, delete-orphan")
|
|
|
|
def __repr__(self):
|
|
return f"<Asset {self.symbol}: {self.name}>"
|
|
|
|
|
|
class AssetPriceHistory(Base):
|
|
__tablename__ = "asset_price_history"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
asset_id = Column(String, ForeignKey("assets.id", ondelete="CASCADE"), index=True)
|
|
price_usd = Column(Float)
|
|
timestamp = Column(DateTime, index=True)
|
|
|
|
# Relationships
|
|
asset = relationship("Asset", back_populates="price_history")
|
|
|
|
def __repr__(self):
|
|
return f"<AssetPriceHistory {self.asset_id}: {self.price_usd} at {self.timestamp}>" |