When getting base board game, connection will check if board game is already in db and return it, if not it will add it.
This commit is contained in:
parent
dfec34acc7
commit
a7f9b63556
3 changed files with 29 additions and 9 deletions
BIN
db/database.db
BIN
db/database.db
Binary file not shown.
|
|
@ -4,9 +4,18 @@ from src.classes import boardgame_classes, play_classes
|
||||||
|
|
||||||
def get_boardgame(boardgame_id: int) -> boardgame_classes.BoardGame:
|
def get_boardgame(boardgame_id: int) -> boardgame_classes.BoardGame:
|
||||||
#Will check if it already exists in db, then it will get it from there
|
#Will check if it already exists in db, then it will get it from there
|
||||||
|
|
||||||
|
boardgame_in_db: list[boardgame_classes.BoardGame] = db_connection.get_base_boardgames(boardgame_id=boardgame_id)
|
||||||
|
|
||||||
return bgg_connection.get_boardgame(boardgame_id)
|
to_return_boardgame = None
|
||||||
|
|
||||||
|
if len(boardgame_in_db) == 0:
|
||||||
|
to_return_boardgame = bgg_connection.get_boardgame(boardgame_id)
|
||||||
|
db_connection.add_boardgame(to_return_boardgame)
|
||||||
|
else:
|
||||||
|
to_return_boardgame = boardgame_in_db[0]
|
||||||
|
|
||||||
|
return to_return_boardgame
|
||||||
|
|
||||||
|
|
||||||
def get_user_owned_collection() -> list[boardgame_classes.BoardGame]:
|
def get_user_owned_collection() -> list[boardgame_classes.BoardGame]:
|
||||||
|
|
|
||||||
|
|
@ -16,21 +16,32 @@ def add_boardgame(boardgame: Union[
|
||||||
boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion,
|
boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion,
|
||||||
boardgame_classes.WishlistBoardGame, boardgame_classes.WishlistBoardGameExpansion]):
|
boardgame_classes.WishlistBoardGame, boardgame_classes.WishlistBoardGameExpansion]):
|
||||||
|
|
||||||
session = Session(engine)
|
with Session(engine) as session:
|
||||||
|
session.add(boardgame)
|
||||||
|
|
||||||
session.add(boardgame)
|
session.commit()
|
||||||
|
session.refresh(boardgame)
|
||||||
|
|
||||||
session.commit()
|
def get_base_boardgames(boardgame_id = None) -> list[boardgame_classes.BoardGame]:
|
||||||
|
with Session(engine) as session:
|
||||||
|
session.expire_on_commit = False
|
||||||
|
if boardgame_id == None:
|
||||||
|
statement = select(boardgame_classes.BoardGame)
|
||||||
|
else:
|
||||||
|
statement = select(boardgame_classes.BoardGame).where(boardgame_classes.BoardGame.id == boardgame_id)
|
||||||
|
results = session.exec(statement)
|
||||||
|
|
||||||
|
boardgame_list = results.all()
|
||||||
|
|
||||||
def get_all_owned_boardgames():
|
return boardgame_list
|
||||||
|
|
||||||
|
|
||||||
|
def get_all_owned_boardgames() -> list[boardgame_classes.OwnedBoardGame]:
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
statement = select(boardgame_classes.OwnedBoardGame)
|
statement = select(boardgame_classes.OwnedBoardGame)
|
||||||
results = session.exec(statement)
|
results = session.exec(statement)
|
||||||
|
|
||||||
boardgame_list = []
|
boardgame_list = results.all()
|
||||||
|
|
||||||
for boardgame in results:
|
|
||||||
boardgame_list.append(boardgame)
|
|
||||||
|
|
||||||
return boardgame_list
|
return boardgame_list
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue