Created a play <-> boardgame relationship
This commit is contained in:
parent
fa6c6807b0
commit
c2e6e755c3
6 changed files with 73 additions and 41 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
from datetime import date
|
from datetime import date
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from sqlmodel import Field, SQLModel
|
from sqlmodel import Field, SQLModel, Relationship
|
||||||
|
|
||||||
|
|
||||||
class BoardgameType(Enum):
|
class BoardgameType(Enum):
|
||||||
|
|
@ -41,11 +41,22 @@ class WishlistBoardGameBase(BoardGameBase):
|
||||||
class BoardGame(BoardGameBase, table=True):
|
class BoardGame(BoardGameBase, table=True):
|
||||||
id: int = Field(primary_key=True)
|
id: int = Field(primary_key=True)
|
||||||
type: BoardgameType = BoardgameType.BOARDGAME
|
type: BoardgameType = BoardgameType.BOARDGAME
|
||||||
|
plays: list["Play"] = Relationship(back_populates='boardgame')
|
||||||
|
|
||||||
|
class BoardGameNoPlays(BoardGameBase):
|
||||||
|
id: int
|
||||||
|
type: BoardgameType
|
||||||
|
|
||||||
class BoardGameExpansion(BoardGameBase, table=True):
|
class BoardGameExpansion(BoardGameBase, table=True):
|
||||||
id: int = Field(primary_key=True)
|
id: int = Field(primary_key=True)
|
||||||
expansion_for: int = Field(default=None, foreign_key="boardgame.id")
|
expansion_for: int = Field(default=None, foreign_key="boardgame.id")
|
||||||
type: BoardgameType = BoardgameType.BOARDGAMEEXPANSION
|
type: BoardgameType = BoardgameType.BOARDGAMEEXPANSION
|
||||||
|
plays: list["Play"] = Relationship(back_populates='expansion')
|
||||||
|
|
||||||
|
class BoardGameExpansionNoPlays(BoardGameBase):
|
||||||
|
id: int
|
||||||
|
expansion_for: int
|
||||||
|
type: BoardgameType
|
||||||
|
|
||||||
class OwnedBoardGame(OwnedBoardGameBase, table=True):
|
class OwnedBoardGame(OwnedBoardGameBase, table=True):
|
||||||
id: int = Field(primary_key=True)
|
id: int = Field(primary_key=True)
|
||||||
|
|
@ -64,3 +75,10 @@ class WishlistBoardGameExpansion(WishlistBoardGameBase, table=True):
|
||||||
id: int = Field(primary_key=True)
|
id: int = Field(primary_key=True)
|
||||||
expansion_for: int = Field(default=None, foreign_key="boardgame.id")
|
expansion_for: int = Field(default=None, foreign_key="boardgame.id")
|
||||||
type: BoardgameType = BoardgameType.WISHLISTBOARDGAMEEXPANSION
|
type: BoardgameType = BoardgameType.WISHLISTBOARDGAMEEXPANSION
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
from src.classes.play_classes import Play, PlayPublic
|
||||||
|
BoardGame.model_rebuild()
|
||||||
|
BoardGameNoPlays.model_rebuild()
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
from sqlmodel import Field, SQLModel, Relationship
|
from sqlmodel import Field, SQLModel, Relationship
|
||||||
|
|
||||||
from typing import Union
|
from typing import Union
|
||||||
from datetime import date
|
from datetime import date
|
||||||
|
|
||||||
|
|
@ -24,8 +23,10 @@ class PlayPlayerPublic(PlayPlayerBase):
|
||||||
class PlayPlayerPublicWithPlay(PlayPlayerPublic):
|
class PlayPlayerPublicWithPlay(PlayPlayerPublic):
|
||||||
play: "PlayPublic"
|
play: "PlayPublic"
|
||||||
|
|
||||||
|
|
||||||
class PlayBase(SQLModel):
|
class PlayBase(SQLModel):
|
||||||
boardgame_id: int
|
boardgame_id: int | None = Field(default=None, foreign_key="boardgame.id")
|
||||||
|
expansion_id: int | None = Field(default=None, foreign_key="boardgameexpansion.id")
|
||||||
play_date: date
|
play_date: date
|
||||||
duration: int #In minutes
|
duration: int #In minutes
|
||||||
ignore_for_stats : bool
|
ignore_for_stats : bool
|
||||||
|
|
@ -36,15 +37,23 @@ class Play(PlayBase, table=True):
|
||||||
id: int | None = Field(default=None, primary_key=True)
|
id: int | None = Field(default=None, primary_key=True)
|
||||||
|
|
||||||
players: list[PlayPlayer] = Relationship(back_populates="play")
|
players: list[PlayPlayer] = Relationship(back_populates="play")
|
||||||
|
boardgame: Union["BoardGame", None] = Relationship(back_populates="plays")
|
||||||
|
expansion: Union["BoardGameExpansion", None] = Relationship(back_populates="plays")
|
||||||
|
|
||||||
model_config = {
|
model_config = {
|
||||||
'validate_assignment':True
|
'validate_assignment':True
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class PlayPublic(PlayBase):
|
class PlayPublic(PlayBase):
|
||||||
id: int
|
id: int
|
||||||
|
|
||||||
|
|
||||||
class PlayPublicWithPlayers(PlayPublic):
|
class PlayPublicWithPlayers(PlayPublic):
|
||||||
players: list[PlayPlayerPublic] = []
|
players: list[PlayPlayerPublic] = []
|
||||||
|
boardgame: Union["BoardGameNoPlays", None]
|
||||||
|
expansion: Union["BoardGameExpansionNoPlays", None]
|
||||||
|
|
||||||
|
|
||||||
|
from src.classes.boardgame_classes import BoardGame, BoardGameExpansion, BoardGameNoPlays, BoardGameExpansionNoPlays
|
||||||
|
Play.model_rebuild()
|
||||||
|
PlayPublicWithPlayers.model_rebuild()
|
||||||
|
|
@ -1,20 +1,8 @@
|
||||||
from src.classes import play_classes, boardgame_classes
|
from src.classes import play_classes, boardgame_classes
|
||||||
|
|
||||||
def filter_out_expansion_plays(play_list: list[play_classes.Play], all_needed_boardgames: list[boardgame_classes.BoardGame]):
|
def filter_out_expansion_plays(play_list: list[play_classes.Play]):
|
||||||
to_return_plays = []
|
to_return_plays = []
|
||||||
|
|
||||||
|
to_return_plays = list(filter(lambda x: x.expansion == None, play_list))
|
||||||
|
|
||||||
for play in play_list:
|
|
||||||
needed_boardgame = list(filter(lambda x: x.id == play.boardgame_id, all_needed_boardgames))
|
|
||||||
if len(needed_boardgame) == 0:
|
|
||||||
print('oh ow')
|
|
||||||
print(play.boardgame_id)
|
|
||||||
print(all_needed_boardgames)
|
|
||||||
print(type(needed_boardgame))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
to_return_plays = play_list
|
|
||||||
|
|
||||||
return to_return_plays
|
return to_return_plays
|
||||||
37
src/main.py
37
src/main.py
|
|
@ -11,6 +11,9 @@ from src.classes import boardgame_classes, play_classes, statistic_classes
|
||||||
from src.modules import data_connection
|
from src.modules import data_connection
|
||||||
from src.filters import boardgame_filters, play_filters
|
from src.filters import boardgame_filters, play_filters
|
||||||
|
|
||||||
|
def get_db_session():
|
||||||
|
yield data_connection.get_db_session()
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
# Startup
|
# Startup
|
||||||
|
|
@ -34,11 +37,6 @@ app.add_middleware(
|
||||||
allow_headers=["*"],
|
allow_headers=["*"],
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_session():
|
|
||||||
engine = data_connection.get_db_engine()
|
|
||||||
with Session(engine) as session:
|
|
||||||
yield session
|
|
||||||
|
|
||||||
#expansion filtering parameters
|
#expansion filtering parameters
|
||||||
class ExpansionFilteringParams(BaseModel):
|
class ExpansionFilteringParams(BaseModel):
|
||||||
filter_expansions_out: bool = False
|
filter_expansions_out: bool = False
|
||||||
|
|
@ -58,12 +56,12 @@ def read_root():
|
||||||
return {"Hello": "World"}
|
return {"Hello": "World"}
|
||||||
|
|
||||||
@app.get("/boardgame", response_model=boardgame_classes.BoardGame)
|
@app.get("/boardgame", response_model=boardgame_classes.BoardGame)
|
||||||
def get_boardgame_by_id(id: int, session: Session = Depends(get_session)):
|
def get_boardgame_by_id(id: int, session: Session = Depends(get_db_session)):
|
||||||
requested_boardgame: boardgame_classes.BoardGame = data_connection.get_boardgame(session, boardgame_classes.BoardGame, id)
|
requested_boardgame: boardgame_classes.BoardGame = data_connection.get_boardgame(session, boardgame_classes.BoardGame, id)
|
||||||
return requested_boardgame
|
return requested_boardgame
|
||||||
|
|
||||||
@app.get("/owned", response_model=list[Union[boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion]])
|
@app.get("/owned", response_model=list[Union[boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion]])
|
||||||
def get_owned_collection(filter_expansions_out: bool = False, only_expansions: bool = False, session: Session = Depends(get_session)):
|
def get_owned_collection(filter_expansions_out: bool = False, only_expansions: bool = False, session: Session = Depends(get_db_session)):
|
||||||
to_return_boardgames = data_connection.get_user_owned_collection(session)
|
to_return_boardgames = data_connection.get_user_owned_collection(session)
|
||||||
|
|
||||||
if filter_expansions_out:
|
if filter_expansions_out:
|
||||||
|
|
@ -76,27 +74,38 @@ def get_owned_collection(filter_expansions_out: bool = False, only_expansions: b
|
||||||
|
|
||||||
|
|
||||||
@app.get("/wishlist", response_model=list[Union[boardgame_classes.WishlistBoardGame, boardgame_classes.WishlistBoardGameExpansion]])
|
@app.get("/wishlist", response_model=list[Union[boardgame_classes.WishlistBoardGame, boardgame_classes.WishlistBoardGameExpansion]])
|
||||||
def get_wishlist_collection(priority: int = 0, session: Session = Depends(get_session)):
|
def get_wishlist_collection(priority: int = 0, session: Session = Depends(get_db_session)):
|
||||||
return data_connection.get_user_wishlist_collection(session, priority)
|
return data_connection.get_user_wishlist_collection(session, priority)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/plays", response_model=list[play_classes.PlayPublicWithPlayers])
|
@app.get("/plays", response_model=list[play_classes.PlayPublicWithPlayers])
|
||||||
def get_plays(session: Session = Depends(get_session)):
|
def get_plays(session: Session = Depends(get_db_session)):
|
||||||
|
|
||||||
|
requested_plays = data_connection.get_plays(session)[0:10]
|
||||||
|
|
||||||
|
# all_played_boardgame_ids = []
|
||||||
|
|
||||||
|
# for play in requested_plays:
|
||||||
|
# if play.boardgame_id not in all_played_boardgame_ids:
|
||||||
|
# all_played_boardgame_ids.append(play.boardgame_id)
|
||||||
|
|
||||||
|
#all_played_boardgames = data_connection.get_multiple_boardgames(session, all_played_boardgame_ids)
|
||||||
|
|
||||||
|
requested_plays = play_filters.filter_out_expansion_plays(requested_plays)
|
||||||
|
|
||||||
requested_plays = data_connection.get_plays(session)
|
|
||||||
|
|
||||||
return requested_plays
|
return requested_plays
|
||||||
|
|
||||||
|
|
||||||
@app.get('/players', response_model=list[play_classes.PlayPlayerPublic])
|
@app.get('/players', response_model=list[play_classes.PlayPlayerPublic])
|
||||||
def get_players_from_play(play_id: int, session: Session = Depends(get_session)):
|
def get_players_from_play(play_id: int, session: Session = Depends(get_db_session)):
|
||||||
requested_players = data_connection.get_players_from_play(session, play_id)
|
requested_players = data_connection.get_players_from_play(session, play_id)
|
||||||
|
|
||||||
return requested_players
|
return requested_players
|
||||||
|
|
||||||
|
|
||||||
@app.get('/statistics/amount_of_games', response_model=statistic_classes.NumberStatistic)
|
@app.get('/statistics/amount_of_games', response_model=statistic_classes.NumberStatistic)
|
||||||
def get_amount_of_games(session: Session = Depends(get_session)):
|
def get_amount_of_games(session: Session = Depends(get_db_session)):
|
||||||
|
|
||||||
statistic_dict = {
|
statistic_dict = {
|
||||||
"name":"Amount of games in owned collection",
|
"name":"Amount of games in owned collection",
|
||||||
|
|
@ -108,7 +117,7 @@ def get_amount_of_games(session: Session = Depends(get_session)):
|
||||||
return statistic_to_return
|
return statistic_to_return
|
||||||
|
|
||||||
@app.get('/statistics/amount_of_games_over_time', response_model=statistic_classes.TimeLineStatistic)
|
@app.get('/statistics/amount_of_games_over_time', response_model=statistic_classes.TimeLineStatistic)
|
||||||
def get_amount_of_games_over_time(day_step: int = 1, filter_expansions_out: bool = False, only_expansions: bool = False, session: Session = Depends(get_session)):
|
def get_amount_of_games_over_time(day_step: int = 1, filter_expansions_out: bool = False, only_expansions: bool = False, session: Session = Depends(get_db_session)):
|
||||||
|
|
||||||
def daterange(start_date: date, end_date: date, day_step):
|
def daterange(start_date: date, end_date: date, day_step):
|
||||||
days = int((end_date - start_date).days)
|
days = int((end_date - start_date).days)
|
||||||
|
|
@ -142,7 +151,7 @@ def get_amount_of_games_over_time(day_step: int = 1, filter_expansions_out: bool
|
||||||
return statistic_to_return
|
return statistic_to_return
|
||||||
|
|
||||||
@app.get('/statistics/games_played_per_year', response_model=statistic_classes.TimeLineStatistic)
|
@app.get('/statistics/games_played_per_year', response_model=statistic_classes.TimeLineStatistic)
|
||||||
def get_amount_of_games_played_per_year(query: ExpansionFilteringParams = Depends(), session: Session = Depends(get_session)):
|
def get_amount_of_games_played_per_year(query: ExpansionFilteringParams = Depends(), session: Session = Depends(get_db_session)):
|
||||||
all_plays = data_connection.get_plays(session)
|
all_plays = data_connection.get_plays(session)
|
||||||
|
|
||||||
all_plays.sort(key= lambda x: x.play_date)
|
all_plays.sort(key= lambda x: x.play_date)
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,8 @@ from sqlmodel import SQLModel, Session
|
||||||
from src.modules import bgg_connection, db_connection
|
from src.modules import bgg_connection, db_connection
|
||||||
from src.classes import boardgame_classes, play_classes
|
from src.classes import boardgame_classes, play_classes
|
||||||
|
|
||||||
def get_db_engine():
|
def get_db_session():
|
||||||
return db_connection.get_engine()
|
return db_connection.get_session()
|
||||||
|
|
||||||
def get_boardgame(session: Session, boardgame_type: SQLModel, boardgame_id: int) -> boardgame_classes.BoardGame:
|
def get_boardgame(session: Session, boardgame_type: SQLModel, boardgame_id: int) -> boardgame_classes.BoardGame:
|
||||||
#Will check if it already exists in db, then it will get it from there
|
#Will check if it already exists in db, then it will get it from there
|
||||||
|
|
@ -26,6 +26,9 @@ def get_boardgame(session: Session, boardgame_type: SQLModel, boardgame_id: int)
|
||||||
def get_multiple_boardgames(session: Session, boardgame_ids: list[int]) -> list[boardgame_classes.BoardGame]:
|
def get_multiple_boardgames(session: Session, boardgame_ids: list[int]) -> list[boardgame_classes.BoardGame]:
|
||||||
|
|
||||||
boardgames_in_db = db_connection.get_multiple_boardgames(session, boardgame_classes.BoardGame, boardgame_ids=boardgame_ids)
|
boardgames_in_db = db_connection.get_multiple_boardgames(session, boardgame_classes.BoardGame, boardgame_ids=boardgame_ids)
|
||||||
|
boardgame_expansions_in_db = db_connection.get_multiple_boardgames(session, boardgame_classes.BoardGameExpansion, boardgame_ids=boardgame_ids)
|
||||||
|
|
||||||
|
boardgames_in_db += boardgame_expansions_in_db
|
||||||
|
|
||||||
to_return_boardgames = []
|
to_return_boardgames = []
|
||||||
|
|
||||||
|
|
@ -88,6 +91,12 @@ def get_plays(session: Session) -> list[play_classes.Play]:
|
||||||
|
|
||||||
plays_from_db = db_connection.get_plays(session)
|
plays_from_db = db_connection.get_plays(session)
|
||||||
|
|
||||||
|
#Making sure all played board games are in table 'boardgames'
|
||||||
|
#list + set to remove duplicates
|
||||||
|
played_boardgame_ids = list(set([play.boardgame_id for play in plays_from_db]))
|
||||||
|
played_expansion_ids = list(set([play.expansion_id for play in plays_from_db]))
|
||||||
|
|
||||||
|
get_multiple_boardgames(get_db_session(), played_boardgame_ids + played_expansion_ids)
|
||||||
|
|
||||||
return plays_from_db
|
return plays_from_db
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,10 +8,11 @@ sqlite_url = definitions.SQLITE_URL
|
||||||
|
|
||||||
|
|
||||||
connect_args = {"check_same_thread": False}
|
connect_args = {"check_same_thread": False}
|
||||||
engine = create_engine(sqlite_url, echo=False, connect_args=connect_args)
|
engine = create_engine(sqlite_url, echo=True, connect_args=connect_args)
|
||||||
|
|
||||||
def get_engine():
|
def get_session():
|
||||||
return engine
|
with Session(engine) as session:
|
||||||
|
return session
|
||||||
|
|
||||||
def add_boardgame(session: Session, boardgame: Union[
|
def add_boardgame(session: Session, boardgame: Union[
|
||||||
boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion,
|
boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion,
|
||||||
|
|
@ -26,7 +27,6 @@ def add_boardgame(session: Session, boardgame: Union[
|
||||||
session.add(boardgame)
|
session.add(boardgame)
|
||||||
|
|
||||||
session.commit()
|
session.commit()
|
||||||
session.refresh(boardgame)
|
|
||||||
|
|
||||||
def add_multiple_boardgames(session: Session, boardgame_list: list[Union[
|
def add_multiple_boardgames(session: Session, boardgame_list: list[Union[
|
||||||
boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion,
|
boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion,
|
||||||
|
|
@ -42,7 +42,6 @@ def add_multiple_boardgames(session: Session, boardgame_list: list[Union[
|
||||||
session.add(boardgame)
|
session.add(boardgame)
|
||||||
|
|
||||||
session.commit()
|
session.commit()
|
||||||
session.refresh(boardgame)
|
|
||||||
|
|
||||||
def get_boardgame(session: Session, boardgame_type: SQLModel, boardgame_id: int) -> Union[
|
def get_boardgame(session: Session, boardgame_type: SQLModel, boardgame_id: int) -> Union[
|
||||||
boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion,
|
boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue