This commit is contained in:
Waylon Walker 2023-05-19 20:37:42 -05:00
parent c238b9d757
commit fcc0698aeb
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
12 changed files with 95 additions and 97 deletions

View file

@ -1,9 +1,7 @@
from typing import Union
from fastapi import FastAPI
import httpx
from learn_sql_model.console import console
from learn_sql_model.models.hero import Hero
from learn_sql_model.models.pet import Pet
@ -13,53 +11,3 @@ models = Union[Hero, Pet]
# from learn_sql_model.models import Hero
app = FastAPI()
app.post("/heroes/")
def post(self: models) -> None:
try:
httpx.post("http://localhost:5000/heroes/", json=self.dict())
except httpx.ConnectError:
console.log("local failover")
post_local(self)
def post_local(self: models) -> None:
from learn_sql_model.config import config
with config.session as session:
session.add(self)
session.commit()
def get(self: models, instance: models = None) -> list[models]:
"read all the heros"
from learn_sql_model.config import config
with config.session as session:
if instance is None:
heroes = session.exec(select(self)).all()
return heroes
else:
hero = session.exec(select(self).where(self.id == instance.id)).all().one()
return hero
@app.post("/heroes/")
def create_hero(hero: Hero):
post(hero)
@app.get("/heroes/")
def read_heroes() -> list[Hero]:
"read all the heros"
return get(Hero)
@app.get("/hero/")
def read_heroes(hero: Hero) -> list[Hero]:
"read all the heros"
return get(Hero, hero)