from typing import Union from fastapi import FastAPI from contextlib import asynccontextmanager from src.classes import boardgame_classes, play_classes from src.modules import data_connection @asynccontextmanager async def lifespan(app: FastAPI): # Startup data_connection.delete_database() data_connection.create_db_and_tables() yield # Shutdown app = FastAPI(lifespan=lifespan) @app.get("/") def read_root(): return {"Hello": "World"} @app.get("/boardgame/{boardgame_id}", response_model=boardgame_classes.BoardGame) def get_boardgame_by_id(boardgame_id: int): requested_boardgame: boardgame_classes.BoardGame = data_connection.get_boardgame(boardgame_id) return requested_boardgame @app.get("/owned", response_model=Union[list[boardgame_classes.OwnedBoardGame], list[boardgame_classes.OwnedBoardGameExpansion]]) def get_owned_collection(): requested_collection: list[boardgame_classes.OwnedBoardGame] = data_connection.get_user_owned_collection() return requested_collection @app.get("/wishlist", response_model=Union[list[boardgame_classes.WishlistBoardGame], list[boardgame_classes.WishlistBoardGameExpansion]]) def get_wishlist_collection(): requested_collection: list[boardgame_classes.WishlistBoardGame] = data_connection.get_user_wishlist_collection() return requested_collection @app.get("/plays", response_model=list[play_classes.Play]) def get_plays(): requested_plays: list[play_classes.Play] = data_connection.get_plays() return requested_plays