Added ability to add multiple plays at once to db
This commit is contained in:
parent
9ef700aef8
commit
36330749c7
3 changed files with 13 additions and 8 deletions
|
|
@ -17,7 +17,7 @@ def get_db_session():
|
|||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
# Startup
|
||||
#data_connection.delete_database()
|
||||
data_connection.delete_database()
|
||||
data_connection.create_db_and_tables()
|
||||
yield
|
||||
# Shutdown
|
||||
|
|
|
|||
|
|
@ -87,8 +87,7 @@ def get_plays(session: Session) -> list[play_classes.Play]:
|
|||
if len(plays_from_db) == 0:
|
||||
all_plays = bgg_connection.get_plays()
|
||||
|
||||
for play in all_plays:
|
||||
db_connection.add_play(session, play)
|
||||
db_connection.add_multiple_plays(session, all_plays)
|
||||
|
||||
plays_from_db = db_connection.get_plays(session)
|
||||
|
||||
|
|
@ -107,8 +106,7 @@ def get_players_from_play(session: Session, play_id: int) -> list[play_classes.P
|
|||
if len(players_from_db) == 0:
|
||||
all_plays = bgg_connection.get_plays()
|
||||
|
||||
for play in all_plays:
|
||||
db_connection.add_play(session, play)
|
||||
db_connection.add_multiple_plays(session, all_plays)
|
||||
|
||||
players_from_db = db_connection.get_players_from_play(session, play_id)
|
||||
|
||||
|
|
|
|||
|
|
@ -60,8 +60,7 @@ def get_boardgame(session: Session, boardgame_type: SQLModel, boardgame_id: int)
|
|||
|
||||
print(boardgame)
|
||||
return boardgame
|
||||
|
||||
|
||||
|
||||
def get_multiple_boardgames(session: Session, boardgame_type: SQLModel, boardgame_ids: list[int]) -> Union[
|
||||
list[boardgame_classes.BoardGame], list[boardgame_classes.BoardGameExpansion],
|
||||
list[boardgame_classes.OwnedBoardGame], list[boardgame_classes.OwnedBoardGameExpansion],
|
||||
|
|
@ -74,7 +73,6 @@ def get_multiple_boardgames(session: Session, boardgame_type: SQLModel, boardgam
|
|||
|
||||
return boardgames
|
||||
|
||||
|
||||
def get_all_boardgames(session: Session, boardgame_type: SQLModel) -> Union[
|
||||
list[boardgame_classes.BoardGame], list[boardgame_classes.BoardGameExpansion],
|
||||
list[boardgame_classes.OwnedBoardGame], list[boardgame_classes.OwnedBoardGameExpansion],
|
||||
|
|
@ -95,6 +93,15 @@ def add_play(session: Session, play: play_classes.Play):
|
|||
session.commit()
|
||||
session.refresh(play)
|
||||
|
||||
def add_multiple_plays(session: Session, play_list: list[play_classes.Play]):
|
||||
|
||||
for play in play_list:
|
||||
is_play_present = len(session.exec(select(play_classes.Play).where(play_classes.Play.id == play.id)).all()) != 0
|
||||
|
||||
if not is_play_present:
|
||||
session.add(play)
|
||||
|
||||
session.commit()
|
||||
|
||||
def get_plays(session: Session) -> list[play_classes.Play]:
|
||||
statement = select(play_classes.Play)
|
||||
|
|
|
|||
Loading…
Reference in a new issue