feat: include stock names in snapshot, transaction, and backtest transaction API responses

This commit is contained in:
ayuriel 2026-02-16 12:50:21 +09:00
parent b6c22f70ae
commit 628b431171
3 changed files with 38 additions and 1 deletions

View File

@ -17,6 +17,7 @@ from app.schemas.backtest import (
EquityCurvePoint, RebalanceHoldings, HoldingItem, TransactionItem,
)
from app.services.backtest import submit_backtest
from app.services.rebalance import RebalanceService
router = APIRouter(prefix="/api/backtest", tags=["backtest"])
@ -228,11 +229,17 @@ async def get_transactions(
.all()
)
# Resolve stock names
tickers = list({t.ticker for t in transactions})
name_service = RebalanceService(db)
names = name_service.get_stock_names(tickers)
return [
TransactionItem(
id=t.id,
date=t.date,
ticker=t.ticker,
name=names.get(t.ticker),
action=t.action,
shares=t.shares,
price=t.price,

View File

@ -231,7 +231,25 @@ async def get_transactions(
.limit(limit)
.all()
)
return transactions
# Resolve stock names
tickers = list({tx.ticker for tx in transactions})
service = RebalanceService(db)
names = service.get_stock_names(tickers)
return [
TransactionResponse(
id=tx.id,
ticker=tx.ticker,
name=names.get(tx.ticker),
tx_type=tx.tx_type.value,
quantity=tx.quantity,
price=tx.price,
executed_at=tx.executed_at,
memo=tx.memo,
)
for tx in transactions
]
@router.post("/{portfolio_id}/transactions", response_model=TransactionResponse, status_code=status.HTTP_201_CREATED)

View File

@ -16,6 +16,7 @@ from app.schemas.portfolio import (
ReturnsResponse, ReturnDataPoint,
)
from app.services.price_service import PriceService
from app.services.rebalance import RebalanceService
router = APIRouter(prefix="/api/portfolios", tags=["snapshots"])
@ -110,6 +111,10 @@ async def create_snapshot(
db.commit()
db.refresh(snapshot)
# Get stock names
name_service = RebalanceService(db)
names = name_service.get_stock_names(tickers)
return SnapshotResponse(
id=snapshot.id,
portfolio_id=snapshot.portfolio_id,
@ -118,6 +123,7 @@ async def create_snapshot(
holdings=[
SnapshotHoldingResponse(
ticker=h.ticker,
name=names.get(h.ticker),
quantity=h.quantity,
price=h.price,
value=h.value,
@ -150,6 +156,11 @@ async def get_snapshot(
if not snapshot:
raise HTTPException(status_code=404, detail="Snapshot not found")
# Get stock names
tickers = [h.ticker for h in snapshot.holdings]
name_service = RebalanceService(db)
names = name_service.get_stock_names(tickers)
return SnapshotResponse(
id=snapshot.id,
portfolio_id=snapshot.portfolio_id,
@ -158,6 +169,7 @@ async def get_snapshot(
holdings=[
SnapshotHoldingResponse(
ticker=h.ticker,
name=names.get(h.ticker),
quantity=h.quantity,
price=h.price,
value=h.value,