Added expansion filtering on most endpoints
This commit is contained in:
parent
259f82fa8d
commit
b0826b875c
3 changed files with 48 additions and 14 deletions
|
|
@ -1,12 +1,18 @@
|
||||||
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]):
|
def filter_expansions_out(play_list: list[play_classes.Play]):
|
||||||
to_return_plays = []
|
to_return_plays = []
|
||||||
|
|
||||||
to_return_plays = list(filter(lambda x: x.expansion_id == None, play_list))
|
to_return_plays = list(filter(lambda x: x.expansion_id == None, play_list))
|
||||||
|
|
||||||
return to_return_plays
|
return to_return_plays
|
||||||
|
|
||||||
|
def filter_non_expansions_out(play_list: list[play_classes.Play]):
|
||||||
|
to_return_plays = []
|
||||||
|
|
||||||
|
to_return_plays = list(filter(lambda x: x.expansion_id != None, play_list))
|
||||||
|
|
||||||
|
return to_return_plays
|
||||||
|
|
||||||
def filter_only_specific_boardgame(boardgame_id: int, play_list: list[play_classes.Play]):
|
def filter_only_specific_boardgame(boardgame_id: int, play_list: list[play_classes.Play]):
|
||||||
|
|
||||||
|
|
|
||||||
53
src/main.py
53
src/main.py
|
|
@ -61,29 +61,42 @@ def get_boardgame_by_id(id: int, session: Session = Depends(get_db_session)):
|
||||||
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_db_session)):
|
def get_owned_collection(query: ExpansionFilteringParams = Depends(), 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 query.filter_expansions_out:
|
||||||
to_return_boardgames = boardgame_filters.filter_expansions_out(to_return_boardgames)
|
to_return_boardgames = boardgame_filters.filter_expansions_out(to_return_boardgames)
|
||||||
|
|
||||||
if only_expansions:
|
if query.only_expansions:
|
||||||
to_return_boardgames = boardgame_filters.filter_non_expansions_out(to_return_boardgames)
|
to_return_boardgames = boardgame_filters.filter_non_expansions_out(to_return_boardgames)
|
||||||
|
|
||||||
return to_return_boardgames
|
return to_return_boardgames
|
||||||
|
|
||||||
|
|
||||||
@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_db_session)):
|
def get_wishlist_collection(priority: int = 0, query: ExpansionFilteringParams = Depends(), session: Session = Depends(get_db_session)):
|
||||||
return data_connection.get_user_wishlist_collection(session, priority)
|
|
||||||
|
to_return_boardgames = data_connection.get_user_wishlist_collection(session, priority)
|
||||||
|
|
||||||
|
if query.filter_expansions_out:
|
||||||
|
to_return_boardgames = boardgame_filters.filter_expansions_out(to_return_boardgames)
|
||||||
|
|
||||||
|
if query.only_expansions:
|
||||||
|
to_return_boardgames = boardgame_filters.filter_non_expansions_out(to_return_boardgames)
|
||||||
|
|
||||||
|
return to_return_boardgames
|
||||||
|
|
||||||
|
|
||||||
@app.get("/plays", response_model=list[play_classes.PlayPublicWithPlayers])
|
@app.get("/plays", response_model=list[play_classes.PlayPublicWithPlayers])
|
||||||
def get_plays(boardgame_id: int = -1, session: Session = Depends(get_db_session)):
|
def get_plays(query: ExpansionFilteringParams = Depends(), boardgame_id: int = -1, session: Session = Depends(get_db_session)):
|
||||||
|
|
||||||
requested_plays = data_connection.get_plays(session)
|
requested_plays = data_connection.get_plays(session)
|
||||||
|
|
||||||
#requested_plays = play_filters.filter_out_expansion_plays(requested_plays)
|
if query.filter_expansions_out:
|
||||||
|
requested_plays = play_filters.filter_expansions_out(requested_plays)
|
||||||
|
|
||||||
|
if query.only_expansions:
|
||||||
|
requested_plays = play_filters.filter_non_expansions_out(requested_plays)
|
||||||
|
|
||||||
if boardgame_id > -1:
|
if boardgame_id > -1:
|
||||||
requested_plays = play_filters.filter_only_specific_boardgame(boardgame_id, requested_plays)
|
requested_plays = play_filters.filter_only_specific_boardgame(boardgame_id, requested_plays)
|
||||||
|
|
@ -99,11 +112,18 @@ def get_players_from_play(play_id: int, session: Session = Depends(get_db_sessio
|
||||||
|
|
||||||
|
|
||||||
@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_db_session)):
|
def get_amount_of_games(query: ExpansionFilteringParams = Depends(), session: Session = Depends(get_db_session)):
|
||||||
|
|
||||||
|
owned_collection = data_connection.get_user_owned_collection(session)
|
||||||
|
|
||||||
|
if query.filter_expansions_out:
|
||||||
|
owned_collection = boardgame_filters.filter_expansions_out(owned_collection)
|
||||||
|
if query.only_expansions:
|
||||||
|
owned_collection = boardgame_filters.filter_non_expansions_out(owned_collection)
|
||||||
|
|
||||||
statistic_dict = {
|
statistic_dict = {
|
||||||
"name":"Amount of games in owned collection",
|
"name":"Amount of games in owned collection",
|
||||||
"result":len(data_connection.get_user_owned_collection(session))
|
"result":len(owned_collection)
|
||||||
}
|
}
|
||||||
|
|
||||||
statistic_to_return = statistic_classes.NumberStatistic(**statistic_dict)
|
statistic_to_return = statistic_classes.NumberStatistic(**statistic_dict)
|
||||||
|
|
@ -151,7 +171,10 @@ def get_amount_of_games_played_per_year(query: ExpansionFilteringParams = Depend
|
||||||
all_plays.sort(key= lambda x: x.play_date)
|
all_plays.sort(key= lambda x: x.play_date)
|
||||||
|
|
||||||
if query.filter_expansions_out:
|
if query.filter_expansions_out:
|
||||||
all_plays = play_filters.filter_out_expansion_plays(all_plays)
|
all_plays = play_filters.filter_expansions_out(all_plays)
|
||||||
|
|
||||||
|
if query.only_expansions:
|
||||||
|
all_plays = play_filters.filter_non_expansions_out(all_plays)
|
||||||
|
|
||||||
|
|
||||||
all_played_boardgame_ids = []
|
all_played_boardgame_ids = []
|
||||||
|
|
@ -182,9 +205,15 @@ def get_amount_of_games_played_per_year(query: ExpansionFilteringParams = Depend
|
||||||
|
|
||||||
|
|
||||||
@app.get('/statistics/most_expensive_games', response_model=statistic_classes.GameOrderStatistic)
|
@app.get('/statistics/most_expensive_games', response_model=statistic_classes.GameOrderStatistic)
|
||||||
def get_most_expensive_game(top_amount: int = 10):
|
def get_most_expensive_game(query: ExpansionFilteringParams = Depends(), top_amount: int = 10, session: Session = Depends(get_db_session)):
|
||||||
|
|
||||||
most_expensive_games: list[Union[boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion]] = data_connection.get_user_owned_collection()
|
most_expensive_games: list[Union[boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion]] = data_connection.get_user_owned_collection(session)
|
||||||
|
|
||||||
|
if query.filter_expansions_out:
|
||||||
|
most_expensive_games = boardgame_filters.filter_expansions_out(most_expensive_games)
|
||||||
|
|
||||||
|
if query.only_expansions:
|
||||||
|
most_expensive_games = boardgame_filters.filter_non_expansions_out(most_expensive_games)
|
||||||
|
|
||||||
most_expensive_games.sort(key=lambda x: x.price_paid, reverse=True)
|
most_expensive_games.sort(key=lambda x: x.price_paid, reverse=True)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,6 @@ def get_boardgame(session: Session, boardgame_type: SQLModel, boardgame_id: int)
|
||||||
to_return_boardgame = boardgame_in_db
|
to_return_boardgame = boardgame_in_db
|
||||||
else:
|
else:
|
||||||
to_return_boardgame = bgg_connection.get_boardgame(boardgame_id)
|
to_return_boardgame = bgg_connection.get_boardgame(boardgame_id)
|
||||||
print(type(to_return_boardgame))
|
|
||||||
db_connection.add_boardgame(session, to_return_boardgame)
|
db_connection.add_boardgame(session, to_return_boardgame)
|
||||||
to_return_boardgame = db_connection.get_boardgame(session, boardgame_id)
|
to_return_boardgame = db_connection.get_boardgame(session, boardgame_id)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue