2024-08-08 17:13:29 +02:00
|
|
|
from sqlmodel import create_engine, SQLModel, Session, select
|
2024-08-03 15:42:19 +02:00
|
|
|
from src.config import definitions
|
2024-08-08 16:50:52 +02:00
|
|
|
import os
|
2024-08-08 17:13:29 +02:00
|
|
|
from typing import Union
|
|
|
|
|
|
|
|
|
|
from src.classes import boardgame_classes
|
2024-08-03 15:42:19 +02:00
|
|
|
|
|
|
|
|
sqlite_url = definitions.SQLITE_URL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
connect_args = {"check_same_thread": False}
|
|
|
|
|
engine = create_engine(sqlite_url, echo=True, connect_args=connect_args)
|
|
|
|
|
|
2024-08-08 17:13:29 +02:00
|
|
|
def add_boardgame(boardgame: Union[
|
|
|
|
|
boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion,
|
|
|
|
|
boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion,
|
|
|
|
|
boardgame_classes.WishlistBoardGame, boardgame_classes.WishlistBoardGameExpansion]):
|
|
|
|
|
|
|
|
|
|
session = Session(engine)
|
|
|
|
|
|
|
|
|
|
session.add(boardgame)
|
|
|
|
|
|
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
|
|
def get_all_owned_boardgames():
|
|
|
|
|
with Session(engine) as session:
|
|
|
|
|
statement = select(boardgame_classes.OwnedBoardGame)
|
|
|
|
|
results = session.exec(statement)
|
|
|
|
|
|
|
|
|
|
boardgame_list = []
|
|
|
|
|
|
|
|
|
|
for boardgame in results:
|
|
|
|
|
boardgame_list.append(boardgame)
|
|
|
|
|
|
|
|
|
|
return boardgame_list
|
|
|
|
|
|
2024-08-08 16:50:52 +02:00
|
|
|
def delete_database():
|
|
|
|
|
os.remove(definitions.DATABASE_FILE_PATH)
|
2024-08-03 15:42:19 +02:00
|
|
|
|
|
|
|
|
def create_db_and_tables():
|
|
|
|
|
SQLModel.metadata.create_all(engine)
|