From c2bba92e39d14dd5a3aa38b82cc52d6e4fb6e8d4 Mon Sep 17 00:00:00 2001 From: waylon Date: Tue, 20 Jun 2023 17:18:07 -0500 Subject: [PATCH] only db from api --- learn_sql_model/api/hero.py | 4 ++- learn_sql_model/game/menu.py | 2 ++ learn_sql_model/game/player.py | 1 + learn_sql_model/models/hero.py | 50 ++++++---------------------------- 4 files changed, 15 insertions(+), 42 deletions(-) diff --git a/learn_sql_model/api/hero.py b/learn_sql_model/api/hero.py index 79db3d6..bb86c23 100644 --- a/learn_sql_model/api/hero.py +++ b/learn_sql_model/api/hero.py @@ -82,4 +82,6 @@ async def get_heros( session: Session = Depends(get_session), ) -> Heros: "get all heros" - return Heros.list(session=session) + statement = select(Hero) + heros = session.exec(statement).all() + return Heros(heros=heros) diff --git a/learn_sql_model/game/menu.py b/learn_sql_model/game/menu.py index c7ddc9c..c7ece7d 100644 --- a/learn_sql_model/game/menu.py +++ b/learn_sql_model/game/menu.py @@ -1,6 +1,8 @@ from typing import Callable, Tuple from pydantic import BaseModel +from learn_sql_model.optional import _optional_import_ +pygame = _optional_import_('pygame', group='game') screen_sizes = [ diff --git a/learn_sql_model/game/player.py b/learn_sql_model/game/player.py index ce9a8b9..81a408d 100644 --- a/learn_sql_model/game/player.py +++ b/learn_sql_model/game/player.py @@ -37,6 +37,7 @@ class Player: self.moving_down = False self.moving_left = False self.moving_right = False + self.joysticks = {} def rename_hero(self): old_hero = self.hero diff --git a/learn_sql_model/models/hero.py b/learn_sql_model/models/hero.py index 4b0544b..d66900b 100644 --- a/learn_sql_model/models/hero.py +++ b/learn_sql_model/models/hero.py @@ -3,7 +3,7 @@ from typing import Optional from fastapi import HTTPException import httpx from pydantic import BaseModel -from sqlmodel import Field, Relationship, SQLModel, Session, select +from sqlmodel import Field, Relationship, SQLModel from learn_sql_model.config import config from learn_sql_model.models.pet import Pet @@ -48,10 +48,9 @@ class HeroRead(HeroBase): cls, id: int, ) -> Hero: - with config.database.session as session: - hero = session.get(Hero, id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") + r = httpx.get(f"{config.api_client.url}/hero/{id}") + if r.status_code != 200: + raise RuntimeError(f"{r.status_code}:\n {r.text}") return hero @@ -61,31 +60,11 @@ class Heros(BaseModel): @classmethod def list( self, - where=None, - offset=0, - limit=None, - session: Session = None, ) -> Hero: - # with config.database.session as session: - - def get_heros(session, where, offset, limit): - statement = select(Hero) - if where != "None" and where is not None: - from sqlmodel import text - - statement = statement.where(text(where)) - statement = statement.offset(offset).limit(limit) - heros = session.exec(statement).all() - return Heros(heros=heros) - - if session is None: - - r = httpx.get(f"{config.api_client.url}/heros/") - if r.status_code != 200: - raise RuntimeError(f"{r.status_code}:\n {r.text}") - return Heros.parse_obj(r.json()) - - return get_heros(session, where, offset, limit) + r = httpx.get(f"{config.api_client.url}/heros/") + if r.status_code != 200: + raise RuntimeError(f"{r.status_code}:\n {r.text}") + return Heros.parse_obj(r.json()) class HeroUpdate(SQLModel): @@ -103,18 +82,7 @@ class HeroUpdate(SQLModel): pet_id: Optional[int] = Field(default=None, foreign_key="pet.id") pet: Optional[Pet] = Relationship(back_populates="hero") - def update(self, session: Session = None) -> Hero: - if session is not None: - db_hero = session.get(Hero, self.id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - for key, value in self.dict(exclude_unset=True).items(): - setattr(db_hero, key, value) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - + def update(self) -> Hero: r = httpx.patch( f"{config.api_client.url}/hero/", json=self.dict(),