only db from api
This commit is contained in:
parent
da41253583
commit
c2bba92e39
4 changed files with 15 additions and 42 deletions
|
|
@ -82,4 +82,6 @@ async def get_heros(
|
||||||
session: Session = Depends(get_session),
|
session: Session = Depends(get_session),
|
||||||
) -> Heros:
|
) -> Heros:
|
||||||
"get all heros"
|
"get all heros"
|
||||||
return Heros.list(session=session)
|
statement = select(Hero)
|
||||||
|
heros = session.exec(statement).all()
|
||||||
|
return Heros(heros=heros)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
from typing import Callable, Tuple
|
from typing import Callable, Tuple
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
from learn_sql_model.optional import _optional_import_
|
||||||
|
pygame = _optional_import_('pygame', group='game')
|
||||||
|
|
||||||
|
|
||||||
screen_sizes = [
|
screen_sizes = [
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ class Player:
|
||||||
self.moving_down = False
|
self.moving_down = False
|
||||||
self.moving_left = False
|
self.moving_left = False
|
||||||
self.moving_right = False
|
self.moving_right = False
|
||||||
|
self.joysticks = {}
|
||||||
|
|
||||||
def rename_hero(self):
|
def rename_hero(self):
|
||||||
old_hero = self.hero
|
old_hero = self.hero
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ from typing import Optional
|
||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
import httpx
|
import httpx
|
||||||
from pydantic import BaseModel
|
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.config import config
|
||||||
from learn_sql_model.models.pet import Pet
|
from learn_sql_model.models.pet import Pet
|
||||||
|
|
@ -48,10 +48,9 @@ class HeroRead(HeroBase):
|
||||||
cls,
|
cls,
|
||||||
id: int,
|
id: int,
|
||||||
) -> Hero:
|
) -> Hero:
|
||||||
with config.database.session as session:
|
r = httpx.get(f"{config.api_client.url}/hero/{id}")
|
||||||
hero = session.get(Hero, id)
|
if r.status_code != 200:
|
||||||
if not hero:
|
raise RuntimeError(f"{r.status_code}:\n {r.text}")
|
||||||
raise HTTPException(status_code=404, detail="Hero not found")
|
|
||||||
return hero
|
return hero
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -61,32 +60,12 @@ class Heros(BaseModel):
|
||||||
@classmethod
|
@classmethod
|
||||||
def list(
|
def list(
|
||||||
self,
|
self,
|
||||||
where=None,
|
|
||||||
offset=0,
|
|
||||||
limit=None,
|
|
||||||
session: Session = None,
|
|
||||||
) -> Hero:
|
) -> 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/")
|
r = httpx.get(f"{config.api_client.url}/heros/")
|
||||||
if r.status_code != 200:
|
if r.status_code != 200:
|
||||||
raise RuntimeError(f"{r.status_code}:\n {r.text}")
|
raise RuntimeError(f"{r.status_code}:\n {r.text}")
|
||||||
return Heros.parse_obj(r.json())
|
return Heros.parse_obj(r.json())
|
||||||
|
|
||||||
return get_heros(session, where, offset, limit)
|
|
||||||
|
|
||||||
|
|
||||||
class HeroUpdate(SQLModel):
|
class HeroUpdate(SQLModel):
|
||||||
# id is required to update the hero
|
# id is required to update the hero
|
||||||
|
|
@ -103,18 +82,7 @@ class HeroUpdate(SQLModel):
|
||||||
pet_id: Optional[int] = Field(default=None, foreign_key="pet.id")
|
pet_id: Optional[int] = Field(default=None, foreign_key="pet.id")
|
||||||
pet: Optional[Pet] = Relationship(back_populates="hero")
|
pet: Optional[Pet] = Relationship(back_populates="hero")
|
||||||
|
|
||||||
def update(self, session: Session = None) -> Hero:
|
def update(self) -> 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
|
|
||||||
|
|
||||||
r = httpx.patch(
|
r = httpx.patch(
|
||||||
f"{config.api_client.url}/hero/",
|
f"{config.api_client.url}/hero/",
|
||||||
json=self.dict(),
|
json=self.dict(),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue