From fde850d13af34cf07d6f6fc539a86990b892bdf7 Mon Sep 17 00:00:00 2001 From: Yarne Coppens Date: Thu, 22 Aug 2024 13:56:22 +0200 Subject: [PATCH] Fixed bug where, if owned was retrieved after generic collection, owned info would not update --- src/classes/statistic_classes.py | 2 +- src/modules/data_connection.py | 8 ++-- src/modules/db_connection.py | 81 ++++++++++++++++++++++++++++---- 3 files changed, 76 insertions(+), 15 deletions(-) diff --git a/src/classes/statistic_classes.py b/src/classes/statistic_classes.py index 4e71a3c..359b4c8 100644 --- a/src/classes/statistic_classes.py +++ b/src/classes/statistic_classes.py @@ -11,7 +11,7 @@ class NumberStatistic(StatisticBase): result: float class GamesStatistic(StatisticBase): - result: list[boardgame_classes.BoardGame] + result: list[boardgame_classes.BoardGamePublic] model_config = { 'validate_assignment':True diff --git a/src/modules/data_connection.py b/src/modules/data_connection.py index 438eef1..5f93651 100644 --- a/src/modules/data_connection.py +++ b/src/modules/data_connection.py @@ -33,7 +33,7 @@ def get_multiple_boardgames(session: Session, boardgame_ids: list[int]) -> list[ if len(boardgame_ids_missing) != 0: missing_boardgames = bgg_connection.get_multiple_boardgames(boardgame_ids_missing) - db_connection.add_multiple_boardgames(session, missing_boardgames) + db_connection.upsert_multiple_boardgames(session, missing_boardgames) boardgames_in_db, boardgame_ids_missing = db_connection.get_multiple_boardgames(session, boardgame_ids=boardgame_ids) @@ -45,7 +45,7 @@ def get_user_collection(session: Session) -> list[boardgame_classes.BoardGame]: if len(boardgames_from_db) == 0: boardgames = bgg_connection.get_user_collection() - db_connection.add_multiple_boardgames(session, boardgames) + db_connection.upsert_multiple_boardgames(session, boardgames) boardgames_from_db: list[boardgame_classes.BoardGame] = db_connection.get_all_boardgames(session) @@ -57,7 +57,7 @@ def get_user_owned_collection(session: Session) -> list[boardgame_classes.BoardG if len(owned_boardgames_from_db) == 0: owned_boardgames = bgg_connection.get_user_owned_collection() - db_connection.add_multiple_boardgames(session, owned_boardgames) + db_connection.upsert_multiple_boardgames(session, owned_boardgames) owned_boardgames_from_db: list[boardgame_classes.BoardGame] = db_connection.get_owned_boardgames(session) @@ -70,7 +70,7 @@ def get_user_wishlist_collection(session: Session, wishlist_priority: int = 0) - if len(wishlisted_boardgames_from_db) == 0: wishlisted_boardgames = bgg_connection.get_user_wishlist_collection() - db_connection.add_multiple_boardgames(session, wishlisted_boardgames) + db_connection.upsert_multiple_boardgames(session, wishlisted_boardgames) wishlisted_boardgames_from_db = db_connection.get_wishlist_boardgames(session) diff --git a/src/modules/db_connection.py b/src/modules/db_connection.py index 87bc227..ddaf89f 100644 --- a/src/modules/db_connection.py +++ b/src/modules/db_connection.py @@ -2,6 +2,8 @@ from sqlmodel import create_engine, SQLModel, Session, select from src.config import definitions from typing import Union from threading import Lock +from datetime import datetime +import copy critical_function_lock = Lock() @@ -16,6 +18,9 @@ engine = create_engine(sqlite_url, echo=False, connect_args=connect_args) def get_engine(): return engine +def copy_attributes_from_to(from_instance, to_instance): + to_instance.__dict__.update(from_instance.__dict__) + def add_boardgame(session: Session, boardgame: boardgame_classes.BoardGame): with critical_function_lock: @@ -34,26 +39,82 @@ def add_boardgame(session: Session, boardgame: boardgame_classes.BoardGame): session.commit() session.refresh(boardgame) -def add_multiple_boardgames(session: Session, boardgame_list: list[boardgame_classes.BoardGame]): +def upsert_multiple_boardgames(session: Session, boardgame_list: list[boardgame_classes.BoardGame]): + #Returns existing row + def update_boardgame(db_boardgame: boardgame_classes.BoardGame, new_db_boardgame: boardgame_classes.BoardGame) -> boardgame_classes.BoardGame: + + if new_db_boardgame.owned_info != None: + owned_info = new_db_boardgame.owned_info.model_dump() + db_boardgame.owned_info = boardgame_classes.OwnedInfo(**owned_info) + + if new_db_boardgame.wishlist_info != None: + wishlist_info = new_db_boardgame.wishlist_info.model_dump() + db_boardgame.wishlist_info = boardgame_classes.WishlistInfo(**wishlist_info) + + return db_boardgame + with critical_function_lock: + + missing_boardgames: list[boardgame_classes.BoardGame] = [] + existing_boardgames: list[boardgame_classes.BoardGame] = [] + for boardgame in boardgame_list: + statement = select(boardgame_classes.BoardGame).where(boardgame_classes.BoardGame.id == boardgame.id) + present_boardgame = session.exec(statement).one_or_none() + is_boardgame_present = present_boardgame != None + if is_boardgame_present: + existing_boardgames.append(present_boardgame) + else: + missing_boardgames.append(boardgame) + altered_boardgames = [] - for boardgame_index in range(len(boardgame_list)): - boardgame_designers = boardgame_list[boardgame_index].designers + + for boardgame in missing_boardgames: + boardgame_designers = boardgame.designers for designer_index in range(len(boardgame_designers)): designer_in_db = get_designer(session, boardgame_designers[designer_index].id) if designer_in_db != None: - boardgame_list[boardgame_index].designers[designer_index] = designer_in_db + boardgame.designers[designer_index] = designer_in_db - statement = select(boardgame_classes.BoardGame).where(boardgame_classes.BoardGame.id == boardgame_list[boardgame_index].id) - is_boardgame_present = len(session.exec(statement).all()) != 0 + session.add(boardgame) + altered_boardgames.append(boardgame) - if not is_boardgame_present: - session.add(boardgame_list[boardgame_index]) - altered_boardgames.append(boardgame_list[boardgame_index]) - + for boardgame in existing_boardgames: + new_db_boardgame = list(filter(lambda x: x.id == boardgame.id, boardgame_list))[0] + updated_boardgame = update_boardgame(boardgame, new_db_boardgame) + session.add(updated_boardgame) + altered_boardgames.append(updated_boardgame) session.commit() + + # for boardgame_index in range(len(boardgame_list)): + # #Check if boardgame is already present + + # if boardgame_list[boardgame_index] in existing_boardgames: + # to_alter_boardgame = update_boardgame(list(filter(lambda x: x.id == boardgame_list[boardgame_index].id, existing_boardgames))[0], boardgame_list[boardgame_index]) + # else: + # to_alter_boardgame = insert_boardgame(boardgame_list[boardgame_index]) + + + + # # if not is_boardgame_present: + # # to_alter_boardgame = boardgame_list[boardgame_index] + # # else: + # # to_alter_boardgame = present_boardgames[0] + # # copy_attributes_from_to(boardgame_list[boardgame_index],to_alter_boardgame) + + + + + # # statement = select(boardgame_classes.BoardGame).where(boardgame_classes.BoardGame.id == to_alter_boardgame.id) + # # present_boardgames = session.exec(statement).all() + # # is_boardgame_present = len(present_boardgames) != 0 + + + + # session.add(to_alter_boardgame) + # altered_boardgames.append(to_alter_boardgame) + [session.refresh(boardgame) for boardgame in altered_boardgames] def get_designer(session: Session, designer_id: int) -> people_classes.Designer: