From 9e82b162549664d54ec7a316835522bffddbdf81 Mon Sep 17 00:00:00 2001 From: Yarne Coppens Date: Sun, 11 Aug 2024 21:27:59 +0200 Subject: [PATCH] Changed play-playplayer to one-to-many relationship --- src/classes/play_classes.py | 14 +++++----- src/main.py | 13 +++++++--- src/modules/data_connection.py | 47 ++++++++++++++++++++++++---------- src/modules/db_connection.py | 40 ++++++++++++++++++++--------- tests/test_main.py | 2 +- 5 files changed, 79 insertions(+), 37 deletions(-) diff --git a/src/classes/play_classes.py b/src/classes/play_classes.py index 489f0ff..1ecf3a5 100644 --- a/src/classes/play_classes.py +++ b/src/classes/play_classes.py @@ -3,25 +3,23 @@ from sqlmodel import Field, SQLModel, Relationship from typing import Union from datetime import date -class PlayerPlayLink(SQLModel, table=True): - player_name: str | None = Field(default=None, foreign_key="playplayer.name", primary_key=True) - play_id: int | None = Field(default=None, foreign_key="play.id", primary_key=True) - class PlayPlayer(SQLModel, table=True): - name: str | None = Field(default=None, primary_key=True) + id: int | None = Field(default=None, primary_key=True) + name: str username: str score: Union[float, None] first_play : bool has_won : bool - plays: list["Play"] = Relationship(back_populates="players", link_model=PlayerPlayLink) + play_id : int = Field(default=None, foreign_key="play.id") + play: "Play" = Relationship(back_populates="players") class Play(SQLModel, table=True): - id: int| None = Field(default=None, primary_key=True) + id: int | None = Field(default=None, primary_key=True) boardgame_id: int play_date: date duration: int #In minutes ignore_for_stats : bool location: str - players: list[PlayPlayer] = Relationship(back_populates="plays", link_model=PlayerPlayLink) \ No newline at end of file + players: list[PlayPlayer] = Relationship(back_populates="play") \ No newline at end of file diff --git a/src/main.py b/src/main.py index 0b0d98d..3421b03 100644 --- a/src/main.py +++ b/src/main.py @@ -50,8 +50,15 @@ def get_wishlist_collection(priority: int = 0): return data_connection.get_user_wishlist_collection(priority) - @app.get("/plays", response_model=list[play_classes.Play]) def get_plays(): - requested_plays: list[play_classes.Play] = data_connection.get_plays() - return requested_plays \ No newline at end of file + requested_plays: list[play_classes.Play] = data_connection.get_plays()[0:10] + + return requested_plays + + +@app.get('/players', response_model=list[play_classes.PlayPlayer]) +def get_players_from_play(play_id): + requested_players: list[play_classes.PlayPlayer] = data_connection.get_players_from_play(play_id) + + return requested_players \ No newline at end of file diff --git a/src/modules/data_connection.py b/src/modules/data_connection.py index bfe46df..dc82737 100644 --- a/src/modules/data_connection.py +++ b/src/modules/data_connection.py @@ -24,18 +24,16 @@ def get_user_owned_collection() -> list[Union[boardgame_classes.OwnedBoardGame, 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) - to_return_boardgames = [] - if len(owned_boardgames_from_db) == 0 and len(owned_boardgame_expanions_from_db) == 0: owned_boardgames = bgg_connection.get_user_owned_collection() for boardgame in owned_boardgames: db_connection.add_boardgame(boardgame) - to_return_boardgames = owned_boardgames - else: - to_return_boardgames = owned_boardgames_from_db + owned_boardgame_expanions_from_db + 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) - return to_return_boardgames + + return owned_boardgames_from_db + owned_boardgame_expanions_from_db @@ -44,18 +42,15 @@ def get_user_wishlist_collection(wishlist_priority: int = 0) -> Union[list[board 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 + 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 = 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) @@ -64,7 +59,33 @@ def get_user_wishlist_collection(wishlist_priority: int = 0) -> Union[list[board def get_plays() -> list[play_classes.Play]: - return bgg_connection.get_plays() + + 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 def delete_database(): db_connection.delete_database() diff --git a/src/modules/db_connection.py b/src/modules/db_connection.py index 8f08063..a0032d5 100644 --- a/src/modules/db_connection.py +++ b/src/modules/db_connection.py @@ -16,7 +16,6 @@ def add_boardgame(boardgame: Union[ boardgame_classes.WishlistBoardGame, boardgame_classes.WishlistBoardGameExpansion]): with Session(engine) as session: - #First check if board game is not already present is_boardgame_present = len(session.exec( select(boardgame.__class__).where(boardgame.__class__.id == boardgame.id) @@ -34,7 +33,6 @@ def get_boardgames(boardgame_type: SQLModel, boardgame_id = None) -> Union[ list[boardgame_classes.WishlistBoardGame], list[boardgame_classes.WishlistBoardGameExpansion]]: with Session(engine) as session: - session.expire_on_commit = False if boardgame_id == None: statement = select(boardgame_type) else: @@ -45,18 +43,36 @@ def get_boardgames(boardgame_type: SQLModel, boardgame_id = None) -> Union[ return boardgame_list +def add_play(play: play_classes.Play): + + with Session(engine) as session: + session.add(play) + + session.commit() + session.refresh(play) + + +def get_plays() -> list[play_classes.Play]: + with Session(engine) as session: + statement = select(play_classes.Play) + results = session.exec(statement) + + play_list = results.all() + + return play_list + +def get_players_from_play(play_id: int) -> list[play_classes.PlayPlayer]: + + with Session(engine) as session: + statement = select(play_classes.PlayPlayer).where(play_classes.PlayPlayer.play_id == play_id) + results = session.exec(statement) + + player_list = results.all() + + return player_list def delete_database(): - 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) - - play_classes.Play.__table__.drop(engine) - play_classes.PlayPlayer.__table__.drop(engine) - play_classes.PlayerPlayLink.__table__.drop(engine) - + SQLModel.metadata.drop_all(engine) def create_db_and_tables(): diff --git a/tests/test_main.py b/tests/test_main.py index 95126e9..5cd8d41 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -31,7 +31,7 @@ def test_read_main(): assert response.json() == {"Hello": "World"} def test_retrieve_boardgame(): - response = client.get("/boardgame/373167") + response = client.get("/boardgame?id=373167") assert response.status_code == 200 returned_boardgame = boardgame_classes.BoardGame(**response.json())