|
|
@ -1,5 +1,6 @@ |
|
|
from typing import Union |
|
|
|
|
|
from contextlib import asynccontextmanager |
|
|
from contextlib import asynccontextmanager |
|
|
|
|
|
from typing import Union |
|
|
|
|
|
|
|
|
from fastapi import Depends, FastAPI, HTTPException, Query |
|
|
from fastapi import Depends, FastAPI, HTTPException, Query |
|
|
from sqlmodel import Field, Session, SQLModel, create_engine, select |
|
|
from sqlmodel import Field, Session, SQLModel, create_engine, select |
|
|
|
|
|
|
|
|
@ -26,6 +27,7 @@ def get_session(): |
|
|
with Session(engine) as session: |
|
|
with Session(engine) as session: |
|
|
yield session |
|
|
yield session |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@asynccontextmanager |
|
|
@asynccontextmanager |
|
|
async def lifespan(app: FastAPI): |
|
|
async def lifespan(app: FastAPI): |
|
|
create_db_and_tables() |
|
|
create_db_and_tables() |
|
|
@ -36,7 +38,6 @@ async def lifespan(app: FastAPI): |
|
|
app = FastAPI(lifespan=lifespan) |
|
|
app = FastAPI(lifespan=lifespan) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/heroes/") |
|
|
@app.post("/heroes/") |
|
|
def create_hero(hero: Hero, session: Session = Depends(get_session)) -> Hero: |
|
|
def create_hero(hero: Hero, session: Session = Depends(get_session)) -> Hero: |
|
|
session.add(hero) |
|
|
session.add(hero) |
|
|
|