Fixed bug where, if owned was retrieved after generic collection, owned info would not update
This commit is contained in:
parent
507db6ac41
commit
fde850d13a
3 changed files with 76 additions and 15 deletions
|
|
@ -11,7 +11,7 @@ class NumberStatistic(StatisticBase):
|
||||||
result: float
|
result: float
|
||||||
|
|
||||||
class GamesStatistic(StatisticBase):
|
class GamesStatistic(StatisticBase):
|
||||||
result: list[boardgame_classes.BoardGame]
|
result: list[boardgame_classes.BoardGamePublic]
|
||||||
|
|
||||||
model_config = {
|
model_config = {
|
||||||
'validate_assignment':True
|
'validate_assignment':True
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ def get_multiple_boardgames(session: Session, boardgame_ids: list[int]) -> list[
|
||||||
|
|
||||||
if len(boardgame_ids_missing) != 0:
|
if len(boardgame_ids_missing) != 0:
|
||||||
missing_boardgames = bgg_connection.get_multiple_boardgames(boardgame_ids_missing)
|
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)
|
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:
|
if len(boardgames_from_db) == 0:
|
||||||
boardgames = bgg_connection.get_user_collection()
|
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)
|
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:
|
if len(owned_boardgames_from_db) == 0:
|
||||||
owned_boardgames = bgg_connection.get_user_owned_collection()
|
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)
|
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:
|
if len(wishlisted_boardgames_from_db) == 0:
|
||||||
wishlisted_boardgames = bgg_connection.get_user_wishlist_collection()
|
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)
|
wishlisted_boardgames_from_db = db_connection.get_wishlist_boardgames(session)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ from sqlmodel import create_engine, SQLModel, Session, select
|
||||||
from src.config import definitions
|
from src.config import definitions
|
||||||
from typing import Union
|
from typing import Union
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
|
from datetime import datetime
|
||||||
|
import copy
|
||||||
|
|
||||||
critical_function_lock = Lock()
|
critical_function_lock = Lock()
|
||||||
|
|
||||||
|
|
@ -16,6 +18,9 @@ engine = create_engine(sqlite_url, echo=False, connect_args=connect_args)
|
||||||
def get_engine():
|
def get_engine():
|
||||||
return 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):
|
def add_boardgame(session: Session, boardgame: boardgame_classes.BoardGame):
|
||||||
|
|
||||||
with critical_function_lock:
|
with critical_function_lock:
|
||||||
|
|
@ -34,26 +39,82 @@ def add_boardgame(session: Session, boardgame: boardgame_classes.BoardGame):
|
||||||
session.commit()
|
session.commit()
|
||||||
session.refresh(boardgame)
|
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:
|
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 = []
|
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)):
|
for designer_index in range(len(boardgame_designers)):
|
||||||
designer_in_db = get_designer(session, boardgame_designers[designer_index].id)
|
designer_in_db = get_designer(session, boardgame_designers[designer_index].id)
|
||||||
if designer_in_db != None:
|
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)
|
session.add(boardgame)
|
||||||
is_boardgame_present = len(session.exec(statement).all()) != 0
|
altered_boardgames.append(boardgame)
|
||||||
|
|
||||||
if not is_boardgame_present:
|
for boardgame in existing_boardgames:
|
||||||
session.add(boardgame_list[boardgame_index])
|
new_db_boardgame = list(filter(lambda x: x.id == boardgame.id, boardgame_list))[0]
|
||||||
altered_boardgames.append(boardgame_list[boardgame_index])
|
updated_boardgame = update_boardgame(boardgame, new_db_boardgame)
|
||||||
|
session.add(updated_boardgame)
|
||||||
|
altered_boardgames.append(updated_boardgame)
|
||||||
|
|
||||||
session.commit()
|
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]
|
[session.refresh(boardgame) for boardgame in altered_boardgames]
|
||||||
|
|
||||||
def get_designer(session: Session, designer_id: int) -> people_classes.Designer:
|
def get_designer(session: Session, designer_id: int) -> people_classes.Designer:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue