2024-08-08 18:41:18 +02:00
|
|
|
from typing import Union
|
|
|
|
|
|
2024-08-03 15:42:19 +02:00
|
|
|
from src.modules import bgg_connection, db_connection
|
2024-08-03 10:44:52 +02:00
|
|
|
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
|
2024-08-08 17:56:22 +02:00
|
|
|
|
2024-08-11 15:58:07 +02:00
|
|
|
boardgame_in_db: list[boardgame_classes.BoardGame] = db_connection.get_boardgames(boardgame_classes.BoardGame, boardgame_id=boardgame_id)
|
2024-08-03 10:44:52 +02:00
|
|
|
|
2024-08-08 17:56:22 +02:00
|
|
|
to_return_boardgame = None
|
2024-08-03 10:44:52 +02:00
|
|
|
|
2024-08-08 18:13:18 +02:00
|
|
|
if len(boardgame_in_db) != 0:
|
|
|
|
|
to_return_boardgame = boardgame_in_db[0]
|
|
|
|
|
else:
|
2024-08-08 17:56:22 +02:00
|
|
|
to_return_boardgame = bgg_connection.get_boardgame(boardgame_id)
|
|
|
|
|
db_connection.add_boardgame(to_return_boardgame)
|
|
|
|
|
|
|
|
|
|
return to_return_boardgame
|
2024-08-03 10:44:52 +02:00
|
|
|
|
|
|
|
|
|
2024-08-08 18:41:18 +02:00
|
|
|
def get_user_owned_collection() -> list[Union[boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion]]:
|
2024-08-11 19:58:22 +02:00
|
|
|
owned_boardgames_from_db: list[boardgame_classes.OwnedBoardGame] = db_connection.get_boardgames(boardgame_classes.OwnedBoardGame)
|
|
|
|
|
owned_boardgame_expanions_from_db: list[boardgame_classes.OwnedBoardGameExpansion] = db_connection.get_boardgames(boardgame_classes.OwnedBoardGameExpansion)
|
|
|
|
|
|
2024-08-08 18:41:18 +02:00
|
|
|
if len(owned_boardgames_from_db) == 0 and len(owned_boardgame_expanions_from_db) == 0:
|
2024-08-08 18:13:18 +02:00
|
|
|
owned_boardgames = bgg_connection.get_user_owned_collection()
|
|
|
|
|
for boardgame in owned_boardgames:
|
|
|
|
|
db_connection.add_boardgame(boardgame)
|
2024-08-11 19:58:22 +02:00
|
|
|
|
2024-08-11 21:27:59 +02:00
|
|
|
owned_boardgames_from_db: list[boardgame_classes.OwnedBoardGame] = db_connection.get_boardgames(boardgame_classes.OwnedBoardGame)
|
|
|
|
|
owned_boardgame_expanions_from_db: list[boardgame_classes.OwnedBoardGameExpansion] = db_connection.get_boardgames(boardgame_classes.OwnedBoardGameExpansion)
|
2024-08-11 19:58:22 +02:00
|
|
|
|
2024-08-11 21:27:59 +02:00
|
|
|
|
|
|
|
|
return owned_boardgames_from_db + owned_boardgame_expanions_from_db
|
2024-08-08 18:41:18 +02:00
|
|
|
|
2024-08-03 10:44:52 +02:00
|
|
|
|
|
|
|
|
|
2024-08-11 12:05:29 +02:00
|
|
|
def get_user_wishlist_collection(wishlist_priority: int = 0) -> Union[list[boardgame_classes.WishlistBoardGame], list[boardgame_classes.WishlistBoardGameExpansion]]:
|
2024-08-03 10:44:52 +02:00
|
|
|
|
2024-08-11 15:58:07 +02:00
|
|
|
wishlisted_boardgames_from_db = db_connection.get_boardgames(boardgame_classes.WishlistBoardGame)
|
|
|
|
|
wishlisted_boardgame_expansions_from_db = db_connection.get_boardgames(boardgame_classes.WishlistBoardGameExpansion)
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
2024-08-11 21:27:59 +02:00
|
|
|
wishlisted_boardgames_from_db = db_connection.get_boardgames(boardgame_classes.WishlistBoardGame)
|
|
|
|
|
wishlisted_boardgame_expansions_from_db = db_connection.get_boardgames(boardgame_classes.WishlistBoardGameExpansion)
|
2024-08-11 15:58:07 +02:00
|
|
|
|
2024-08-11 21:27:59 +02:00
|
|
|
to_return_boardgames = wishlisted_boardgames_from_db + wishlisted_boardgame_expansions_from_db
|
2024-08-11 15:58:07 +02:00
|
|
|
|
|
|
|
|
if wishlist_priority != 0:
|
|
|
|
|
to_return_boardgames = filter(lambda game: game.wishlist_priority == wishlist_priority, to_return_boardgames)
|
|
|
|
|
|
|
|
|
|
return to_return_boardgames
|
2024-08-08 18:13:18 +02:00
|
|
|
|
2024-08-03 10:44:52 +02:00
|
|
|
|
|
|
|
|
def get_plays() -> list[play_classes.Play]:
|
2024-08-11 21:27:59 +02:00
|
|
|
|
|
|
|
|
plays_from_db = db_connection.get_plays()
|
|
|
|
|
|
|
|
|
|
if len(plays_from_db) == 0:
|
|
|
|
|
all_plays = bgg_connection.get_plays()
|
|
|
|
|
|
|
|
|
|
for play in all_plays:
|
|
|
|
|
db_connection.add_play(play)
|
|
|
|
|
|
|
|
|
|
plays_from_db = db_connection.get_plays()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return plays_from_db
|
|
|
|
|
|
|
|
|
|
def get_players_from_play(play_id: int) -> list[play_classes.PlayPlayer]:
|
|
|
|
|
players_from_db = db_connection.get_players_from_play(play_id)
|
|
|
|
|
|
|
|
|
|
if len(players_from_db) == 0:
|
|
|
|
|
all_plays = bgg_connection.get_plays()
|
|
|
|
|
|
|
|
|
|
for play in all_plays:
|
|
|
|
|
db_connection.add_play(play)
|
|
|
|
|
|
|
|
|
|
players_from_db = db_connection.get_players_from_play(play_id)
|
|
|
|
|
|
|
|
|
|
print(players_from_db)
|
|
|
|
|
return players_from_db
|
2024-08-03 15:42:19 +02:00
|
|
|
|
2024-08-08 16:50:52 +02:00
|
|
|
def delete_database():
|
|
|
|
|
db_connection.delete_database()
|
2024-08-03 15:42:19 +02:00
|
|
|
|
|
|
|
|
def create_db_and_tables():
|
|
|
|
|
db_connection.create_db_and_tables()
|