Added most expensive games statistic
This commit is contained in:
parent
7778813206
commit
e7b9c4d0c1
3 changed files with 35 additions and 6 deletions
|
|
@ -1,5 +1,15 @@
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
class BaseNumberStatistic(BaseModel):
|
from src.classes import boardgame_classes
|
||||||
|
|
||||||
|
class NumberStatistic(BaseModel):
|
||||||
name: str
|
name: str
|
||||||
result: float
|
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]]
|
||||||
25
src/main.py
25
src/main.py
|
|
@ -64,7 +64,7 @@ def get_players_from_play(play_id):
|
||||||
return requested_players
|
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():
|
def get_amount_of_games():
|
||||||
|
|
||||||
statistic_dict = {
|
statistic_dict = {
|
||||||
|
|
@ -72,6 +72,25 @@ def get_amount_of_games():
|
||||||
"result":len(data_connection.get_user_owned_collection())
|
"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
|
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
|
||||||
|
|
|
||||||
|
|
@ -91,7 +91,7 @@ def test_retrieve_basic_statistic():
|
||||||
response = client.get("/statistics/amount_of_games")
|
response = client.get("/statistics/amount_of_games")
|
||||||
assert response.status_code == 200
|
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.name) == str
|
||||||
assert type(returned_statistic.result) == float
|
assert type(returned_statistic.result) == float
|
||||||
Loading…
Reference in a new issue