From e943a3c4a85b26f51b88b78fc65bbd0e5d921101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80?= <78488229+viktor138irk@users.noreply.github.com> Date: Fri, 8 May 2026 03:33:47 +0900 Subject: [PATCH] Wire database-backed demo account into API --- app/main.py | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/app/main.py b/app/main.py index 6415fb5..209efa3 100644 --- a/app/main.py +++ b/app/main.py @@ -1,11 +1,13 @@ import asyncio from pathlib import Path -from fastapi import FastAPI, HTTPException, WebSocket, WebSocketDisconnect +from fastapi import Depends, FastAPI, HTTPException, WebSocket, WebSocketDisconnect from fastapi.responses import FileResponse from fastapi.staticfiles import StaticFiles +from sqlalchemy.orm import Session from app.coinex import CoinExClient from app.core import get_settings +from app.db import SessionLocal, get_db, init_db from app.demo_account import DemoAccountService settings = get_settings() @@ -17,6 +19,11 @@ static_dir = Path(__file__).parent / 'static' app.mount('/static', StaticFiles(directory=static_dir), name='static') +@app.on_event('startup') +def on_startup() -> None: + init_db() + + @app.get('/') async def dashboard() -> FileResponse: return FileResponse(static_dir / 'index.html') @@ -35,19 +42,26 @@ async def health() -> dict: @app.get('/api/v1/demo/account') -async def demo_account() -> dict: - return demo_service.snapshot().model_dump(mode='json') +async def demo_account(db: Session = Depends(get_db)) -> dict: + return demo_service.snapshot(db).model_dump(mode='json') @app.post('/api/v1/demo/reset') -async def reset_demo_account() -> dict: - return demo_service.reset().model_dump(mode='json') +async def reset_demo_account(db: Session = Depends(get_db)) -> dict: + return demo_service.reset(db).model_dump(mode='json') @app.post('/api/v1/demo/trades') -async def create_demo_trade(market: str, side: str, price: float, amount: float, reason: str = 'manual demo trade') -> dict: +async def create_demo_trade( + market: str, + side: str, + price: float, + amount: float, + reason: str = 'manual demo trade', + db: Session = Depends(get_db), +) -> dict: try: - trade = demo_service.add_demo_trade(market=market, side=side, price=price, amount=amount, reason=reason) + trade = demo_service.add_demo_trade(db=db, market=market, side=side, price=price, amount=amount, reason=reason) return trade.model_dump(mode='json') except ValueError as exc: raise HTTPException(status_code=400, detail=str(exc)) from exc @@ -69,11 +83,13 @@ async def market_stream(websocket: WebSocket, market: str) -> None: try: while True: ticker = await coinex.get_ticker(market) + with SessionLocal() as db: + account = demo_service.snapshot(db).model_dump(mode='json') await websocket.send_json({ 'type': 'ticker', 'market': market.upper(), 'data': ticker, - 'demo_account': demo_service.snapshot().model_dump(mode='json'), + 'demo_account': account, }) await asyncio.sleep(3) except WebSocketDisconnect: