# Imports from contextlib import asynccontextmanager from fastapi import FastAPI, HTTPException from sql import * from functions import * # Create DB on startup # noinspection PyUnusedLocal @asynccontextmanager async def lifespan(app: FastAPI): create_db_and_tables() yield # Code before the yield will run on startup, code after yield won't run until the program is over # Define the app app = FastAPI() # Routes @app.post("/heroes/create", response_model=HeroPublic) def create_hero(hero: HeroCreate, session: SessionDep): db_hero = Hero.model_validate(hero) session.add(db_hero) session.commit() session.refresh(db_hero) return db_hero @app.get("/heros/{type}", response_model=HeroPublic) def get_hero(type: str, session: SessionDep): user = get_hero_by_id(type, session) if user is None: user = get_hero_by_email(type, session) if not user: raise HTTPException(status_code=404, detail="User not found") return user