Created base method on db_connection for retrieving board games instead of a method for each board game type
This commit is contained in:
parent
52dfd0395d
commit
75e68dbd89
3 changed files with 51 additions and 41 deletions
|
|
@ -257,7 +257,7 @@ def get_user_owned_collection() -> list[boardgame_classes.BoardGame]:
|
|||
|
||||
return owned_boardgames
|
||||
|
||||
def get_user_wishlist_collection(wishlist_priority: int = 0) -> list[boardgame_classes.BoardGame]:
|
||||
def get_user_wishlist_collection() -> list[boardgame_classes.BoardGame]:
|
||||
url_no_expanions = 'https://boardgamegeek.com/xmlapi2/collection?username={}&wishlist=1&stats=1&excludesubtype=boardgameexpansion&showprivate=1&version=1'.format(auth_manager.username)
|
||||
url_only_expansions = 'https://boardgamegeek.com/xmlapi2/collection?username={}&wishlist=1&stats=1&subtype=boardgameexpansion&showprivate=1&version=1'.format(auth_manager.username)
|
||||
|
||||
|
|
@ -266,8 +266,6 @@ def get_user_wishlist_collection(wishlist_priority: int = 0) -> list[boardgame_c
|
|||
|
||||
wishlisted_boardgames += wishlisted_boardgame_expansions
|
||||
|
||||
if wishlist_priority != 0:
|
||||
wishlisted_boardgames = filter(lambda game: game.wishlist_priority == wishlist_priority, wishlisted_boardgames)
|
||||
|
||||
return wishlisted_boardgames
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ from src.classes import boardgame_classes, play_classes
|
|||
def get_boardgame(boardgame_id: int) -> boardgame_classes.BoardGame:
|
||||
#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)
|
||||
boardgame_in_db: list[boardgame_classes.BoardGame] = db_connection.get_boardgames(boardgame_classes.BoardGame, boardgame_id=boardgame_id)
|
||||
|
||||
to_return_boardgame = None
|
||||
|
||||
|
|
@ -21,10 +21,8 @@ def get_boardgame(boardgame_id: int) -> boardgame_classes.BoardGame:
|
|||
|
||||
|
||||
def get_user_owned_collection() -> list[Union[boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion]]:
|
||||
owned_boardgames_from_db = db_connection.get_all_owned_boardgames()
|
||||
owned_boardgame_expanions_from_db = db_connection.get_all_owned_boardgames_expansions()
|
||||
|
||||
boardgames_to_return = []
|
||||
owned_boardgames_from_db = db_connection.get_boardgames(boardgame_classes.OwnedBoardGame)
|
||||
owned_boardgame_expanions_from_db = db_connection.get_boardgames(boardgame_classes.OwnedBoardGameExpansion)
|
||||
|
||||
if len(owned_boardgames_from_db) == 0 and len(owned_boardgame_expanions_from_db) == 0:
|
||||
owned_boardgames = bgg_connection.get_user_owned_collection()
|
||||
|
|
@ -33,15 +31,35 @@ def get_user_owned_collection() -> list[Union[boardgame_classes.OwnedBoardGame,
|
|||
|
||||
return owned_boardgames
|
||||
else:
|
||||
return owned_boardgames_from_db
|
||||
return owned_boardgames_from_db + owned_boardgame_expanions_from_db
|
||||
|
||||
|
||||
|
||||
def get_user_wishlist_collection(wishlist_priority: int = 0) -> Union[list[boardgame_classes.WishlistBoardGame], list[boardgame_classes.WishlistBoardGameExpansion]]:
|
||||
|
||||
owned_boardgames_from_db = db_connection.get_all_wishlisted_boardgames
|
||||
wishlisted_boardgames_from_db = db_connection.get_boardgames(boardgame_classes.WishlistBoardGame)
|
||||
wishlisted_boardgame_expansions_from_db = db_connection.get_boardgames(boardgame_classes.WishlistBoardGameExpansion)
|
||||
|
||||
to_return_boardgames = []
|
||||
|
||||
if len(wishlisted_boardgames_from_db) == 0 and len(wishlisted_boardgame_expansions_from_db) == 0:
|
||||
wishlisted_boardgames = bgg_connection.get_user_wishlist_collection()
|
||||
for boardgame in wishlisted_boardgames:
|
||||
db_connection.add_boardgame(boardgame)
|
||||
|
||||
to_return_boardgames = wishlisted_boardgames
|
||||
|
||||
else:
|
||||
to_return_boardgames = wishlisted_boardgames_from_db + wishlisted_boardgame_expansions_from_db
|
||||
|
||||
|
||||
if wishlist_priority != 0:
|
||||
to_return_boardgames = filter(lambda game: game.wishlist_priority == wishlist_priority, to_return_boardgames)
|
||||
|
||||
print(to_return_boardgames)
|
||||
|
||||
return to_return_boardgames
|
||||
|
||||
return bgg_connection.get_user_wishlist_collection(wishlist_priority)
|
||||
|
||||
def get_plays() -> list[play_classes.Play]:
|
||||
return bgg_connection.get_plays()
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
from sqlmodel import create_engine, SQLModel, Session, select
|
||||
from src.config import definitions
|
||||
import os
|
||||
from typing import Union
|
||||
|
||||
from src.classes import boardgame_classes
|
||||
|
|
@ -17,18 +16,29 @@ def add_boardgame(boardgame: Union[
|
|||
boardgame_classes.WishlistBoardGame, boardgame_classes.WishlistBoardGameExpansion]):
|
||||
|
||||
with Session(engine) as session:
|
||||
session.add(boardgame)
|
||||
|
||||
session.commit()
|
||||
session.refresh(boardgame)
|
||||
#First check if board game is not already present
|
||||
is_boardgame_present = len(session.exec(
|
||||
select(boardgame.__class__).where(boardgame.__class__.id == boardgame.id)
|
||||
).all()) != 0
|
||||
|
||||
if not is_boardgame_present:
|
||||
session.add(boardgame)
|
||||
|
||||
session.commit()
|
||||
session.refresh(boardgame)
|
||||
|
||||
def get_boardgames(boardgame_type: SQLModel, boardgame_id = None) -> Union[
|
||||
list[boardgame_classes.BoardGame], list[boardgame_classes.BoardGameExpansion],
|
||||
list[boardgame_classes.OwnedBoardGame], list[boardgame_classes.OwnedBoardGameExpansion],
|
||||
list[boardgame_classes.WishlistBoardGame], list[boardgame_classes.WishlistBoardGameExpansion]]:
|
||||
|
||||
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)
|
||||
statement = select(boardgame_type)
|
||||
else:
|
||||
statement = select(boardgame_classes.BoardGame).where(boardgame_classes.BoardGame.id == boardgame_id)
|
||||
statement = select(boardgame_type).where(boardgame_type.id == boardgame_id)
|
||||
results = session.exec(statement)
|
||||
|
||||
boardgame_list = results.all()
|
||||
|
|
@ -36,29 +46,13 @@ def get_base_boardgames(boardgame_id = None) -> list[boardgame_classes.BoardGame
|
|||
return boardgame_list
|
||||
|
||||
|
||||
def get_all_owned_boardgames() -> list[boardgame_classes.OwnedBoardGame]:
|
||||
with Session(engine) as session:
|
||||
statement = select(boardgame_classes.OwnedBoardGame)
|
||||
results = session.exec(statement)
|
||||
|
||||
boardgame_list = results.all()
|
||||
|
||||
return boardgame_list
|
||||
|
||||
def get_all_owned_boardgames_expansions() -> list[boardgame_classes.OwnedBoardGameExpansion]:
|
||||
with Session(engine) as session:
|
||||
statement = select(boardgame_classes.OwnedBoardGameExpansion)
|
||||
results = session.exec(statement)
|
||||
|
||||
boardgame_list = results.all()
|
||||
|
||||
return boardgame_list
|
||||
|
||||
def get_all_wishlisted_boardgames() -> list[boardgame_classes.WishlistBoardGame]:
|
||||
pass
|
||||
|
||||
def delete_database():
|
||||
os.remove(definitions.DATABASE_FILE_PATH)
|
||||
boardgame_classes.BoardGame.__table__.drop(engine)
|
||||
boardgame_classes.OwnedBoardGame.__table__.drop(engine)
|
||||
boardgame_classes.OwnedBoardGameExpansion.__table__.drop(engine)
|
||||
boardgame_classes.WishlistBoardGame.__table__.drop(engine)
|
||||
boardgame_classes.WishlistBoardGameExpansion.__table__.drop(engine)
|
||||
|
||||
|
||||
def create_db_and_tables():
|
||||
SQLModel.metadata.create_all(engine)
|
||||
Loading…
Reference in a new issue