This commit is contained in:
Waylon Walker 2023-06-13 13:41:56 -05:00
parent 7cce3940fc
commit 6676011ec6
2 changed files with 29 additions and 16 deletions

View file

@ -2,6 +2,7 @@ from contextlib import contextmanager
from fastapi import APIRouter, Depends, WebSocket, WebSocketDisconnect
from fastapi.responses import HTMLResponse
from rich.console import Console
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlmodel import Session
@ -9,7 +10,7 @@ from websockets.exceptions import ConnectionClosed
from learn_sql_model.api.websocket_connection_manager import manager
from learn_sql_model.config import get_config, get_session
from learn_sql_model.models.hero import Hero, HeroUpdate, Heros
from learn_sql_model.models.hero import HeroUpdate, Heros
web_socket_router = APIRouter()
@ -100,12 +101,18 @@ async def websocket_endpoint_hero_echo(
):
config = get_config()
await websocket.accept()
try:
while True:
with db_session(config.database_url) as db:
heros = Heros(heros=db.query(Hero).all())
# heros = Heros.list(session=session)
with config.database.engine.connect() as con:
while True:
data = await websocket.receive_text()
hero = HeroUpdate.parse_raw(data)
heros = con.execute("SELECT * FROM hero").fetchall()
heros = Heros.parse_obj({"heros": heros})
hero.update(session=session)
Console().print(heros)
await websocket.send_text(heros.json())
except WebSocketDisconnect:
print("disconnected")
except ConnectionClosed: