This commit is contained in:
Waylon Walker 2023-06-11 10:46:37 -05:00
parent 83313f6cd4
commit 5101d4c478
5 changed files with 35 additions and 16 deletions

View file

@ -12,6 +12,7 @@ from learn_sql_model.models.hero import Hero
from learn_sql_model.models.hero import HeroCreate from learn_sql_model.models.hero import HeroCreate
from learn_sql_model.models.hero import HeroDelete from learn_sql_model.models.hero import HeroDelete
from learn_sql_model.models.hero import HeroRead from learn_sql_model.models.hero import HeroRead
from learn_sql_model.models.hero import Heros
from learn_sql_model.models.hero import HeroUpdate from learn_sql_model.models.hero import HeroUpdate
from learn_sql_model.models.new import new from learn_sql_model.models.new import new
from learn_sql_model.models.new import newCreate from learn_sql_model.models.new import newCreate

View file

@ -82,4 +82,4 @@ async def get_heros(
session: Session = Depends(get_session), session: Session = Depends(get_session),
) -> list[Hero]: ) -> list[Hero]:
"get all heros" "get all heros"
return HeroRead.list(session=session) return Heros.list(session=session)

View file

@ -1,7 +1,10 @@
from fastapi import APIRouter, WebSocket, WebSocketDisconnect from fastapi import APIRouter, Depends, WebSocket, WebSocketDisconnect
from fastapi.responses import HTMLResponse from fastapi.responses import HTMLResponse
from sqlmodel import Session
from learn_sql_model.api.websocket_connection_manager import manager from learn_sql_model.api.websocket_connection_manager import manager
from learn_sql_model.config import get_session
from learn_sql_model.models.hero import Heros
web_socket_router = APIRouter() web_socket_router = APIRouter()
@ -70,3 +73,14 @@ async def websocket_endpoint(websocket: WebSocket):
except WebSocketDisconnect: except WebSocketDisconnect:
manager.disconnect(websocket, id) manager.disconnect(websocket, id)
await manager.broadcast(f"Client #{id} left the chat", id) await manager.broadcast(f"Client #{id} left the chat", id)
@web_socket_router.websocket("/wsecho")
async def websocket_endpoint(
websocket: WebSocket,
session: Session = Depends(get_session),
):
await websocket.accept()
while True:
heros = Heros.list(session=session)
await websocket.send_text(heros.json())

View file

@ -12,13 +12,7 @@ import pygame
import typer import typer
from typer import Typer from typer import Typer
from learn_sql_model.models.hero import ( from learn_sql_model.models.hero import Hero, HeroCreate, HeroDelete, HeroUpdate
Hero,
HeroCreate,
HeroDelete,
HeroRead,
HeroUpdate,
)
speed = 10 speed = 10
@ -42,7 +36,7 @@ class Client:
self.moving_left = False self.moving_left = False
self.moving_right = False self.moving_right = False
self.others = [hero for hero in HeroRead.list() if hero.id != self.hero.id] self.others = [hero for hero in Heros.list() if hero.id != self.hero.id]
self.ticks = 0 self.ticks = 0
atexit.register(self.quit) atexit.register(self.quit)
@ -73,7 +67,7 @@ class Client:
HeroUpdate( HeroUpdate(
**{k: v for k, v in self.hero.dict().items() if v is not None} **{k: v for k, v in self.hero.dict().items() if v is not None}
).update() ).update()
self.others = [hero for hero in HeroRead.list() if hero.id != self.hero.id] self.others = [hero for hero in Heros.list() if hero.id != self.hero.id]
def render(self): def render(self):
# Console().print(self.hero) # Console().print(self.hero)

View file

@ -54,6 +54,10 @@ class HeroRead(HeroBase):
raise HTTPException(status_code=404, detail="Hero not found") raise HTTPException(status_code=404, detail="Hero not found")
return hero return hero
class Heros(BaseModel):
heros: list[Hero]
@classmethod @classmethod
def list( def list(
self, self,
@ -64,17 +68,23 @@ class HeroRead(HeroBase):
) -> Hero: ) -> Hero:
# with config.database.session as session: # with config.database.session as session:
if session is None: def get_heros(session, where, offset, limit):
engine = get_config().database.engine
with Session(engine) as session:
statement = select(Hero) statement = select(Hero)
if where != "None" and where is not None: if where != "None" and where is not None:
from sqlmodel import text from sqlmodel import text
statement = statement.where(text(where)) statement = statement.where(text(where))
statement = statement.offset(offset).limit(limit) statement = statement.offset(offset).limit(limit)
heroes = session.exec(statement).all() heros = session.exec(statement).all()
return heroes return Heros(heros=heros)
if session is None:
engine = get_config().database.engine
with Session(engine) as session:
heros = get_heros(session, where, offset, limit)
return heros
return get_heros(session, where, offset, limit)
class HeroUpdate(SQLModel): class HeroUpdate(SQLModel):