This commit is contained in:
Waylon Walker 2023-05-23 08:55:35 -05:00
parent daf81343bf
commit a2b33b25f8
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
11 changed files with 479 additions and 55 deletions

View file

@ -4,15 +4,15 @@ from fastapi import APIRouter, Depends
from sqlmodel import SQLModel
from learn_sql_model.api.user import oauth2_scheme
from learn_sql_model.config import get_config
from learn_sql_model.config import Config, get_config
from learn_sql_model.models.hero import Hero
hero_router = APIRouter()
@hero_router.on_event("startup")
def on_startup() -> None:
SQLModel.metadata.create_all(get_config().database.engine)
def on_startup(config: Config = Depends(get_config)) -> None:
SQLModel.metadata.create_all(config.database.engine)
@hero_router.get("/items/")
@ -21,21 +21,22 @@ async def read_items(token: Annotated[str, Depends(oauth2_scheme)]):
@hero_router.get("/hero/{id}")
def get_hero(id: int) -> Hero:
def get_hero(id: int, config: Config = Depends(get_config)) -> Hero:
"get one hero"
return Hero().get(id=id)
return Hero().get(id=id, config=config)
@hero_router.post("/hero/")
def post_hero(hero: Hero) -> Hero:
def post_hero(hero: Hero, config: Config = Depends(get_config)) -> Hero:
"read all the heros"
return hero.post()
hero.post(config=config)
return hero
@hero_router.get("/heros/")
def get_heros() -> list[Hero]:
def get_heros(config: Config = Depends(get_config)) -> list[Hero]:
"get all heros"
return Hero().get()
return Hero().get(config=config)
# Alternatively
# with get_config().database.session as session:
# statement = select(Hero)