Created boardgame DB classes for table creation

This commit is contained in:
Yarne Coppens 2024-08-03 15:42:19 +02:00
parent b7d3f2d0d2
commit aaf0319151
7 changed files with 62 additions and 40 deletions

BIN
db/database.db Normal file

Binary file not shown.

View file

@ -1,7 +1,7 @@
from pydantic import BaseModel, HttpUrl
from datetime import date from datetime import date
from enum import Enum from enum import Enum
from sqlmodel import Field, SQLModel, Column, JSON from sqlmodel import Field, SQLModel
class BoardgameType(Enum): class BoardgameType(Enum):
BOARDGAME = 'boardgame' BOARDGAME = 'boardgame'
@ -12,24 +12,7 @@ class BoardgameType(Enum):
WISHLISTBOARDGAMEEXPANSION = 'wishlistboardgameexpansion' WISHLISTBOARDGAMEEXPANSION = 'wishlistboardgameexpansion'
class BoardGame(BaseModel): class BoardGameBase(SQLModel):
id: int
name: str
description: str
image_url : HttpUrl
thumbnail_url : HttpUrl
year_published: int
min_players: int
max_players: int
min_playing_time: int
max_playing_time: int
min_age: int
all_expansion_ids: list[int]
type: BoardgameType = BoardgameType.BOARDGAME
class BoardGameTable(SQLModel, table=True):
id: int = Field(primary_key=True)
name: str name: str
description: str description: str
image_url : str image_url : str
@ -40,25 +23,43 @@ class BoardGameTable(SQLModel, table=True):
min_playing_time: int min_playing_time: int
max_playing_time: int max_playing_time: int
min_age: int min_age: int
all_expansion_ids: list[int] = Field(sa_column=Column(JSON))
type: BoardgameType = BoardgameType.BOARDGAME
model_config = {
'validate_assignment':True
}
class BoardGameExpansion(BoardGame): class OwnedBoardGameBase(BoardGameBase):
type: BoardgameType = BoardgameType.BOARDGAMEEXPANSION
class OwnedBoardGame(BoardGame):
price_paid: float price_paid: float
acquisition_date: date acquisition_date: date
acquired_from: str acquired_from: str
class WishlistBoardGameBase(BoardGameBase):
wishlist_priority: int
class BoardGame(BoardGameBase, table=True):
id: int = Field(primary_key=True)
type: BoardgameType = BoardgameType.BOARDGAME
class BoardGameExpansion(BoardGameBase, table=True):
id: int = Field(primary_key=True)
expansion_for: int = Field(default=None, foreign_key="boardgame.id")
type: BoardgameType = BoardgameType.BOARDGAMEEXPANSION
class OwnedBoardGame(OwnedBoardGameBase, table=True):
id: int = Field(primary_key=True)
type: BoardgameType = BoardgameType.OWNEDBOARDGAME type: BoardgameType = BoardgameType.OWNEDBOARDGAME
class OwnedBoardGameExpansion(OwnedBoardGame): class OwnedBoardGameExpansion(OwnedBoardGameBase, table=True):
id: int = Field(primary_key=True)
expansion_for: int = Field(default=None, foreign_key="boardgame.id")
type: BoardgameType = BoardgameType.OWNEDBOARDGAMEEXPANSION type: BoardgameType = BoardgameType.OWNEDBOARDGAMEEXPANSION
class WishlistBoardGame(BoardGame): class WishlistBoardGame(WishlistBoardGameBase, table=True):
wishlist_priority: int id: int = Field(primary_key=True)
type: BoardgameType = BoardgameType.WISHLISTBOARDGAME type: BoardgameType = BoardgameType.WISHLISTBOARDGAME
class WishlistBoardGameExpansion(WishlistBoardGame): class WishlistBoardGameExpansion(WishlistBoardGameBase, table=True):
id: int = Field(primary_key=True)
expansion_for: int = Field(default=None, foreign_key="boardgame.id")
type: BoardgameType = BoardgameType.WISHLISTBOARDGAMEEXPANSION type: BoardgameType = BoardgameType.WISHLISTBOARDGAMEEXPANSION

View file

@ -1,11 +1,17 @@
from typing import Union
from fastapi import FastAPI from fastapi import FastAPI
from contextlib import asynccontextmanager
from src.classes import boardgame_classes, play_classes from src.classes import boardgame_classes, play_classes
from src.modules import data_connection from src.modules import data_connection
app = FastAPI() @asynccontextmanager
async def lifespan(app: FastAPI):
# Startup
data_connection.create_db_and_tables()
yield
# Shutdown
app = FastAPI(lifespan=lifespan)
@app.get("/") @app.get("/")
def read_root(): def read_root():

View file

@ -80,14 +80,15 @@ def convert_xml_to_boardgame(boardgame_xml: ET.Element) -> boardgame_classes.Boa
"max_players" : int(boardgame_xml.find('maxplayers').get('value')), "max_players" : int(boardgame_xml.find('maxplayers').get('value')),
"min_playing_time" : int(boardgame_xml.find('minplaytime').get('value')), "min_playing_time" : int(boardgame_xml.find('minplaytime').get('value')),
"max_playing_time" : int(boardgame_xml.find('maxplaytime').get('value')), "max_playing_time" : int(boardgame_xml.find('maxplaytime').get('value')),
"min_age" : int(boardgame_xml.find('minage').get('value')), "min_age" : int(boardgame_xml.find('minage').get('value'))
"all_expansion_ids" : expansion_ids
} }
match boardgame_type: match boardgame_type:
case "boardgame": case "boardgame":
boardgame = boardgame_classes.BoardGame(**boardgame_dict) boardgame = boardgame_classes.BoardGame(**boardgame_dict)
case "boardgameexpansion": case "boardgameexpansion":
expansion_for = expansion_ids[0]
boardgame_dict['expansion_for'] = expansion_for
boardgame = boardgame_classes.BoardGameExpansion(**boardgame_dict) boardgame = boardgame_classes.BoardGameExpansion(**boardgame_dict)
@ -132,7 +133,6 @@ def convert_collection_xml_to_owned_boardgame(boardgame_extra_info: boardgame_cl
"min_playing_time" : boardgame_extra_info.min_playing_time, "min_playing_time" : boardgame_extra_info.min_playing_time,
"max_playing_time" : boardgame_extra_info.max_playing_time, "max_playing_time" : boardgame_extra_info.max_playing_time,
"min_age" : boardgame_extra_info.min_age, "min_age" : boardgame_extra_info.min_age,
"all_expansion_ids" : boardgame_extra_info.all_expansion_ids,
**owned_boardgame_dict **owned_boardgame_dict
} }
@ -140,6 +140,7 @@ def convert_collection_xml_to_owned_boardgame(boardgame_extra_info: boardgame_cl
case "boardgame": case "boardgame":
boardgame = boardgame_classes.OwnedBoardGame(**boardgame_dict) boardgame = boardgame_classes.OwnedBoardGame(**boardgame_dict)
case "boardgameexpansion": case "boardgameexpansion":
boardgame_dict['expansion_for'] = boardgame_extra_info.expansion_for
boardgame = boardgame_classes.OwnedBoardGameExpansion(**boardgame_dict) boardgame = boardgame_classes.OwnedBoardGameExpansion(**boardgame_dict)
@ -168,7 +169,6 @@ def convert_collection_xml_to_wishlist_boardgame(boardgame_extra_info: boardgame
"min_playing_time" : boardgame_extra_info.min_playing_time, "min_playing_time" : boardgame_extra_info.min_playing_time,
"max_playing_time" : boardgame_extra_info.max_playing_time, "max_playing_time" : boardgame_extra_info.max_playing_time,
"min_age" : boardgame_extra_info.min_age, "min_age" : boardgame_extra_info.min_age,
"all_expansion_ids" : boardgame_extra_info.all_expansion_ids,
**wishlist_boardgame_dict **wishlist_boardgame_dict
} }
@ -176,6 +176,7 @@ def convert_collection_xml_to_wishlist_boardgame(boardgame_extra_info: boardgame
case "boardgame": case "boardgame":
boardgame = boardgame_classes.WishlistBoardGame(**boardgame_dict) boardgame = boardgame_classes.WishlistBoardGame(**boardgame_dict)
case "boardgameexpansion": case "boardgameexpansion":
boardgame_dict['expansion_for'] = boardgame_extra_info.expansion_for
boardgame = boardgame_classes.WishlistBoardGameExpansion(**boardgame_dict) boardgame = boardgame_classes.WishlistBoardGameExpansion(**boardgame_dict)

View file

@ -1,4 +1,4 @@
from src.modules import bgg_connection from src.modules import bgg_connection, db_connection
from src.classes import boardgame_classes, play_classes from src.classes import boardgame_classes, play_classes
@ -20,3 +20,7 @@ def get_user_wishlist_collection() -> list[boardgame_classes.BoardGame]:
def get_plays() -> list[play_classes.Play]: def get_plays() -> list[play_classes.Play]:
return bgg_connection.get_plays() return bgg_connection.get_plays()
def create_db_and_tables():
db_connection.create_db_and_tables()

View file

@ -0,0 +1,12 @@
from sqlmodel import create_engine, SQLModel
from src.config import definitions
sqlite_url = definitions.SQLITE_URL
connect_args = {"check_same_thread": False}
engine = create_engine(sqlite_url, echo=True, connect_args=connect_args)
def create_db_and_tables():
SQLModel.metadata.create_all(engine)

View file

@ -20,8 +20,6 @@ def default_boardgame_test(to_test_boardgame: boardgame_classes.BoardGame):
assert type(to_test_boardgame.min_playing_time) == int assert type(to_test_boardgame.min_playing_time) == int
assert type(to_test_boardgame.max_playing_time) == int assert type(to_test_boardgame.max_playing_time) == int
assert type(to_test_boardgame.min_age) == int assert type(to_test_boardgame.min_age) == int
assert type(to_test_boardgame.all_expansion_ids) == list
assert type(to_test_boardgame.all_expansion_ids[0]) == int
assert type(to_test_boardgame.type) == boardgame_classes.BoardgameType assert type(to_test_boardgame.type) == boardgame_classes.BoardgameType