2024-07-24 22:53:56 +02:00
|
|
|
from typing import Union
|
2024-08-08 18:13:01 +02:00
|
|
|
|
2024-07-24 22:53:56 +02:00
|
|
|
from fastapi import FastAPI
|
2024-08-08 16:35:06 +02:00
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
from contextlib import asynccontextmanager
|
2024-07-24 22:53:56 +02:00
|
|
|
|
2024-08-02 12:48:35 +02:00
|
|
|
from src.classes import boardgame_classes, play_classes
|
2024-08-08 16:35:06 +02:00
|
|
|
from src.modules import data_connection
|
|
|
|
|
|
|
|
|
|
@asynccontextmanager
|
|
|
|
|
async def lifespan(app: FastAPI):
|
|
|
|
|
# Startup
|
|
|
|
|
data_connection.create_db_and_tables()
|
|
|
|
|
yield
|
|
|
|
|
# Shutdown
|
|
|
|
|
|
|
|
|
|
app = FastAPI(lifespan=lifespan)
|
|
|
|
|
|
|
|
|
|
origins = [
|
|
|
|
|
"http://127.0.0.1:8080",
|
|
|
|
|
"http://0.0.0.0:8080" #Will become something like 'bordspellen2.yarnecoppens.com'
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
app.add_middleware(
|
|
|
|
|
CORSMiddleware,
|
|
|
|
|
allow_origins=origins,
|
|
|
|
|
allow_credentials=True,
|
|
|
|
|
allow_methods=["*"],
|
|
|
|
|
allow_headers=["*"],
|
|
|
|
|
)
|
2024-07-24 22:53:56 +02:00
|
|
|
|
|
|
|
|
@app.get("/")
|
|
|
|
|
def read_root():
|
|
|
|
|
return {"Hello": "World"}
|
|
|
|
|
|
2024-08-02 09:52:06 +02:00
|
|
|
@app.get("/boardgame/{boardgame_id}", response_model=boardgame_classes.BoardGame)
|
2024-07-25 19:38:20 +02:00
|
|
|
def get_boardgame_by_id(boardgame_id: int):
|
2024-08-03 10:44:52 +02:00
|
|
|
requested_boardgame: boardgame_classes.BoardGame = data_connection.get_boardgame(boardgame_id)
|
2024-07-31 16:03:24 +02:00
|
|
|
return requested_boardgame
|
|
|
|
|
|
2024-08-08 18:41:18 +02:00
|
|
|
@app.get("/owned", response_model=list[Union[boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion]])
|
2024-07-31 16:03:24 +02:00
|
|
|
def get_owned_collection():
|
2024-08-08 18:41:18 +02:00
|
|
|
return data_connection.get_user_owned_collection()
|
2024-08-01 10:35:51 +02:00
|
|
|
|
|
|
|
|
|
2024-08-08 18:41:18 +02:00
|
|
|
@app.get("/wishlist", response_model=list[Union[boardgame_classes.WishlistBoardGame, boardgame_classes.WishlistBoardGameExpansion]])
|
2024-08-01 10:35:51 +02:00
|
|
|
def get_wishlist_collection():
|
2024-08-08 18:41:18 +02:00
|
|
|
return data_connection.get_user_wishlist_collection()
|
2024-08-02 12:48:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/plays", response_model=list[play_classes.Play])
|
|
|
|
|
def get_plays():
|
2024-08-03 10:44:52 +02:00
|
|
|
requested_plays: list[play_classes.Play] = data_connection.get_plays()
|
2024-08-02 12:48:35 +02:00
|
|
|
return requested_plays
|