From e7b9c4d0c1b846a48843b8ea64ba32661a228fcf Mon Sep 17 00:00:00 2001 From: Yarne Coppens Date: Sun, 11 Aug 2024 22:18:12 +0200 Subject: [PATCH] Added most expensive games statistic --- src/classes/statistic_classes.py | 14 ++++++++++++-- src/main.py | 25 ++++++++++++++++++++++--- tests/test_main.py | 2 +- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/classes/statistic_classes.py b/src/classes/statistic_classes.py index 89f974a..7399c0b 100644 --- a/src/classes/statistic_classes.py +++ b/src/classes/statistic_classes.py @@ -1,5 +1,15 @@ from pydantic import BaseModel +from typing import Union -class BaseNumberStatistic(BaseModel): +from src.classes import boardgame_classes + +class NumberStatistic(BaseModel): name: str - result: float \ No newline at end of file + result: float + +class GameOrderStatistic(BaseModel): + name: str + result: list[Union[ + boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion, + boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion, + boardgame_classes.WishlistBoardGame, boardgame_classes.WishlistBoardGameExpansion]] \ No newline at end of file diff --git a/src/main.py b/src/main.py index 86d980b..faa14a5 100644 --- a/src/main.py +++ b/src/main.py @@ -64,7 +64,7 @@ def get_players_from_play(play_id): return requested_players -@app.get('/statistics/amount_of_games', response_model=statistic_classes.BaseNumberStatistic) +@app.get('/statistics/amount_of_games', response_model=statistic_classes.NumberStatistic) def get_amount_of_games(): statistic_dict = { @@ -72,6 +72,25 @@ def get_amount_of_games(): "result":len(data_connection.get_user_owned_collection()) } - statistic_to_return = statistic_classes.BaseNumberStatistic(**statistic_dict) + statistic_to_return = statistic_classes.NumberStatistic(**statistic_dict) - return statistic_to_return \ No newline at end of file + return statistic_to_return + + +@app.get('/statistics/most_expensive_games', response_model=statistic_classes.GameOrderStatistic) +def get_most_expensive_game(top_amount: int = 10): + + most_expensive_games: list[Union[boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion]] = data_connection.get_user_owned_collection() + + most_expensive_games.sort(key=lambda x: x.price_paid, reverse=True) + + most_expensive_games = most_expensive_games[0:top_amount] + + statistic_dict = { + "name":"Most expensive games", + "result":most_expensive_games + } + + statistic_to_return = statistic_classes.GameOrderStatistic(**statistic_dict) + + return statistic_to_return diff --git a/tests/test_main.py b/tests/test_main.py index 15af72c..5e364be 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -91,7 +91,7 @@ def test_retrieve_basic_statistic(): response = client.get("/statistics/amount_of_games") assert response.status_code == 200 - returned_statistic = statistic_classes.BaseNumberStatistic(**response.json()) + returned_statistic = statistic_classes.NumberStatistic(**response.json()) assert type(returned_statistic.name) == str assert type(returned_statistic.result) == float \ No newline at end of file