Changed play-playplayer to one-to-many relationship
This commit is contained in:
parent
2b7fcdbae3
commit
9e82b16254
5 changed files with 79 additions and 37 deletions
|
|
@ -3,25 +3,23 @@ from sqlmodel import Field, SQLModel, Relationship
|
||||||
from typing import Union
|
from typing import Union
|
||||||
from datetime import date
|
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):
|
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
|
username: str
|
||||||
score: Union[float, None]
|
score: Union[float, None]
|
||||||
first_play : bool
|
first_play : bool
|
||||||
has_won : 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):
|
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
|
boardgame_id: int
|
||||||
play_date: date
|
play_date: date
|
||||||
duration: int #In minutes
|
duration: int #In minutes
|
||||||
ignore_for_stats : bool
|
ignore_for_stats : bool
|
||||||
location: str
|
location: str
|
||||||
|
|
||||||
players: list[PlayPlayer] = Relationship(back_populates="plays", link_model=PlayerPlayLink)
|
players: list[PlayPlayer] = Relationship(back_populates="play")
|
||||||
11
src/main.py
11
src/main.py
|
|
@ -50,8 +50,15 @@ def get_wishlist_collection(priority: int = 0):
|
||||||
return data_connection.get_user_wishlist_collection(priority)
|
return data_connection.get_user_wishlist_collection(priority)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/plays", response_model=list[play_classes.Play])
|
@app.get("/plays", response_model=list[play_classes.Play])
|
||||||
def get_plays():
|
def get_plays():
|
||||||
requested_plays: list[play_classes.Play] = data_connection.get_plays()
|
requested_plays: list[play_classes.Play] = data_connection.get_plays()[0:10]
|
||||||
|
|
||||||
return requested_plays
|
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
|
||||||
|
|
@ -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_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)
|
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:
|
if len(owned_boardgames_from_db) == 0 and len(owned_boardgame_expanions_from_db) == 0:
|
||||||
owned_boardgames = bgg_connection.get_user_owned_collection()
|
owned_boardgames = bgg_connection.get_user_owned_collection()
|
||||||
for boardgame in owned_boardgames:
|
for boardgame in owned_boardgames:
|
||||||
db_connection.add_boardgame(boardgame)
|
db_connection.add_boardgame(boardgame)
|
||||||
|
|
||||||
to_return_boardgames = owned_boardgames
|
owned_boardgames_from_db: list[boardgame_classes.OwnedBoardGame] = db_connection.get_boardgames(boardgame_classes.OwnedBoardGame)
|
||||||
else:
|
owned_boardgame_expanions_from_db: list[boardgame_classes.OwnedBoardGameExpansion] = db_connection.get_boardgames(boardgame_classes.OwnedBoardGameExpansion)
|
||||||
to_return_boardgames = owned_boardgames_from_db + owned_boardgame_expanions_from_db
|
|
||||||
|
|
||||||
return to_return_boardgames
|
|
||||||
|
return owned_boardgames_from_db + owned_boardgame_expanions_from_db
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -44,19 +42,16 @@ def get_user_wishlist_collection(wishlist_priority: int = 0) -> Union[list[board
|
||||||
wishlisted_boardgames_from_db = db_connection.get_boardgames(boardgame_classes.WishlistBoardGame)
|
wishlisted_boardgames_from_db = db_connection.get_boardgames(boardgame_classes.WishlistBoardGame)
|
||||||
wishlisted_boardgame_expansions_from_db = db_connection.get_boardgames(boardgame_classes.WishlistBoardGameExpansion)
|
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:
|
if len(wishlisted_boardgames_from_db) == 0 and len(wishlisted_boardgame_expansions_from_db) == 0:
|
||||||
wishlisted_boardgames = bgg_connection.get_user_wishlist_collection()
|
wishlisted_boardgames = bgg_connection.get_user_wishlist_collection()
|
||||||
for boardgame in wishlisted_boardgames:
|
for boardgame in wishlisted_boardgames:
|
||||||
db_connection.add_boardgame(boardgame)
|
db_connection.add_boardgame(boardgame)
|
||||||
|
|
||||||
to_return_boardgames = 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)
|
||||||
|
|
||||||
else:
|
|
||||||
to_return_boardgames = wishlisted_boardgames_from_db + wishlisted_boardgame_expansions_from_db
|
to_return_boardgames = wishlisted_boardgames_from_db + wishlisted_boardgame_expansions_from_db
|
||||||
|
|
||||||
|
|
||||||
if wishlist_priority != 0:
|
if wishlist_priority != 0:
|
||||||
to_return_boardgames = filter(lambda game: game.wishlist_priority == wishlist_priority, to_return_boardgames)
|
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]:
|
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():
|
def delete_database():
|
||||||
db_connection.delete_database()
|
db_connection.delete_database()
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,6 @@ def add_boardgame(boardgame: Union[
|
||||||
boardgame_classes.WishlistBoardGame, boardgame_classes.WishlistBoardGameExpansion]):
|
boardgame_classes.WishlistBoardGame, boardgame_classes.WishlistBoardGameExpansion]):
|
||||||
|
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
|
|
||||||
#First check if board game is not already present
|
#First check if board game is not already present
|
||||||
is_boardgame_present = len(session.exec(
|
is_boardgame_present = len(session.exec(
|
||||||
select(boardgame.__class__).where(boardgame.__class__.id == boardgame.id)
|
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]]:
|
list[boardgame_classes.WishlistBoardGame], list[boardgame_classes.WishlistBoardGameExpansion]]:
|
||||||
|
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
session.expire_on_commit = False
|
|
||||||
if boardgame_id == None:
|
if boardgame_id == None:
|
||||||
statement = select(boardgame_type)
|
statement = select(boardgame_type)
|
||||||
else:
|
else:
|
||||||
|
|
@ -45,18 +43,36 @@ def get_boardgames(boardgame_type: SQLModel, boardgame_id = None) -> Union[
|
||||||
|
|
||||||
return boardgame_list
|
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():
|
def delete_database():
|
||||||
boardgame_classes.BoardGame.__table__.drop(engine)
|
SQLModel.metadata.drop_all(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)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def create_db_and_tables():
|
def create_db_and_tables():
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ def test_read_main():
|
||||||
assert response.json() == {"Hello": "World"}
|
assert response.json() == {"Hello": "World"}
|
||||||
|
|
||||||
def test_retrieve_boardgame():
|
def test_retrieve_boardgame():
|
||||||
response = client.get("/boardgame/373167")
|
response = client.get("/boardgame?id=373167")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
returned_boardgame = boardgame_classes.BoardGame(**response.json())
|
returned_boardgame = boardgame_classes.BoardGame(**response.json())
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue