2024-07-24 22:53:56 +02:00
|
|
|
from typing import Union
|
2024-08-12 12:08:08 +02:00
|
|
|
from datetime import date, timedelta, datetime
|
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
from sqlmodel import Session
|
2024-08-08 18:13:01 +02:00
|
|
|
|
2024-08-12 12:08:08 +02:00
|
|
|
from fastapi import FastAPI, Depends
|
2024-08-08 16:35:06 +02:00
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
from contextlib import asynccontextmanager
|
2024-07-24 22:53:56 +02:00
|
|
|
|
2024-08-11 21:42:07 +02:00
|
|
|
from src.classes import boardgame_classes, play_classes, statistic_classes
|
2024-08-08 16:35:06 +02:00
|
|
|
from src.modules import data_connection
|
2024-08-12 12:08:08 +02:00
|
|
|
from src.filters import boardgame_filters, play_filters
|
2024-08-08 16:35:06 +02:00
|
|
|
|
2024-08-12 20:11:34 +02:00
|
|
|
def get_db_session():
|
|
|
|
|
yield data_connection.get_db_session()
|
|
|
|
|
|
2024-08-08 16:35:06 +02:00
|
|
|
@asynccontextmanager
|
|
|
|
|
async def lifespan(app: FastAPI):
|
|
|
|
|
# Startup
|
2024-08-12 20:39:25 +02:00
|
|
|
data_connection.delete_database()
|
2024-08-08 16:35:06 +02:00
|
|
|
data_connection.create_db_and_tables()
|
|
|
|
|
yield
|
|
|
|
|
# Shutdown
|
|
|
|
|
|
|
|
|
|
app = FastAPI(lifespan=lifespan)
|
|
|
|
|
|
|
|
|
|
origins = [
|
|
|
|
|
"http://127.0.0.1:8080",
|
|
|
|
|
"http://0.0.0.0:8080" #Will become something like 'bordspellen2.yarnecoppens.com'
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
app.add_middleware(
|
|
|
|
|
CORSMiddleware,
|
|
|
|
|
allow_origins=origins,
|
|
|
|
|
allow_credentials=True,
|
|
|
|
|
allow_methods=["*"],
|
|
|
|
|
allow_headers=["*"],
|
|
|
|
|
)
|
2024-07-24 22:53:56 +02:00
|
|
|
|
2024-08-12 12:08:08 +02:00
|
|
|
#expansion filtering parameters
|
|
|
|
|
class ExpansionFilteringParams(BaseModel):
|
|
|
|
|
filter_expansions_out: bool = False
|
|
|
|
|
only_expansions: bool = False
|
|
|
|
|
|
|
|
|
|
def do_filtering(self,boardgame_list):
|
|
|
|
|
if self.filter_expansions_out:
|
|
|
|
|
to_return_boardgames = boardgame_filters.filter_expansions_out(boardgame_list)
|
|
|
|
|
|
|
|
|
|
if self.only_expansions:
|
|
|
|
|
to_return_boardgames = boardgame_filters.filter_non_expansions_out(boardgame_list)
|
|
|
|
|
|
|
|
|
|
return to_return_boardgames
|
|
|
|
|
|
2024-07-24 22:53:56 +02:00
|
|
|
@app.get("/")
|
|
|
|
|
def read_root():
|
|
|
|
|
return {"Hello": "World"}
|
|
|
|
|
|
2024-08-13 10:35:25 +02:00
|
|
|
@app.get("/boardgame", response_model=Union[boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion])
|
2024-08-12 20:11:34 +02:00
|
|
|
def get_boardgame_by_id(id: int, session: Session = Depends(get_db_session)):
|
2024-08-13 10:35:25 +02:00
|
|
|
requested_boardgame: Union[boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion] = data_connection.get_boardgame(session, boardgame_classes.BoardGame, id)
|
2024-07-31 16:03:24 +02:00
|
|
|
return requested_boardgame
|
|
|
|
|
|
2024-08-08 18:41:18 +02:00
|
|
|
@app.get("/owned", response_model=list[Union[boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion]])
|
2024-08-12 20:11:34 +02:00
|
|
|
def get_owned_collection(filter_expansions_out: bool = False, only_expansions: bool = False, session: Session = Depends(get_db_session)):
|
2024-08-12 12:08:08 +02:00
|
|
|
to_return_boardgames = data_connection.get_user_owned_collection(session)
|
2024-08-12 09:10:03 +02:00
|
|
|
|
|
|
|
|
if filter_expansions_out:
|
|
|
|
|
to_return_boardgames = boardgame_filters.filter_expansions_out(to_return_boardgames)
|
|
|
|
|
|
|
|
|
|
if only_expansions:
|
|
|
|
|
to_return_boardgames = boardgame_filters.filter_non_expansions_out(to_return_boardgames)
|
|
|
|
|
|
2024-08-11 19:58:22 +02:00
|
|
|
return to_return_boardgames
|
2024-08-01 10:35:51 +02:00
|
|
|
|
|
|
|
|
|
2024-08-08 18:41:18 +02:00
|
|
|
@app.get("/wishlist", response_model=list[Union[boardgame_classes.WishlistBoardGame, boardgame_classes.WishlistBoardGameExpansion]])
|
2024-08-12 20:11:34 +02:00
|
|
|
def get_wishlist_collection(priority: int = 0, session: Session = Depends(get_db_session)):
|
2024-08-12 12:08:08 +02:00
|
|
|
return data_connection.get_user_wishlist_collection(session, priority)
|
|
|
|
|
|
2024-08-02 12:48:35 +02:00
|
|
|
|
2024-08-12 12:08:08 +02:00
|
|
|
@app.get("/plays", response_model=list[play_classes.PlayPublicWithPlayers])
|
2024-08-12 20:23:05 +02:00
|
|
|
def get_plays(boardgame_id: int = -1, session: Session = Depends(get_db_session)):
|
2024-08-12 20:11:34 +02:00
|
|
|
|
2024-08-12 20:23:05 +02:00
|
|
|
requested_plays = data_connection.get_plays(session)
|
2024-08-12 20:11:34 +02:00
|
|
|
|
2024-08-13 10:35:25 +02:00
|
|
|
#requested_plays = play_filters.filter_out_expansion_plays(requested_plays)
|
2024-08-12 20:23:05 +02:00
|
|
|
|
|
|
|
|
if boardgame_id > -1:
|
|
|
|
|
requested_plays = play_filters.filter_only_specific_boardgame(boardgame_id, requested_plays)
|
2024-08-11 21:27:59 +02:00
|
|
|
|
|
|
|
|
return requested_plays
|
|
|
|
|
|
|
|
|
|
|
2024-08-12 12:16:28 +02:00
|
|
|
@app.get('/players', response_model=list[play_classes.PlayPlayerPublic])
|
2024-08-12 20:11:34 +02:00
|
|
|
def get_players_from_play(play_id: int, session: Session = Depends(get_db_session)):
|
2024-08-12 12:08:08 +02:00
|
|
|
requested_players = data_connection.get_players_from_play(session, play_id)
|
2024-08-11 21:27:59 +02:00
|
|
|
|
2024-08-11 21:42:07 +02:00
|
|
|
return requested_players
|
|
|
|
|
|
|
|
|
|
|
2024-08-11 22:18:12 +02:00
|
|
|
@app.get('/statistics/amount_of_games', response_model=statistic_classes.NumberStatistic)
|
2024-08-12 20:11:34 +02:00
|
|
|
def get_amount_of_games(session: Session = Depends(get_db_session)):
|
2024-08-11 21:42:07 +02:00
|
|
|
|
|
|
|
|
statistic_dict = {
|
|
|
|
|
"name":"Amount of games in owned collection",
|
2024-08-12 12:08:08 +02:00
|
|
|
"result":len(data_connection.get_user_owned_collection(session))
|
2024-08-11 21:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-11 22:18:12 +02:00
|
|
|
statistic_to_return = statistic_classes.NumberStatistic(**statistic_dict)
|
2024-08-11 21:52:36 +02:00
|
|
|
|
2024-08-11 22:18:12 +02:00
|
|
|
return statistic_to_return
|
|
|
|
|
|
2024-08-11 22:52:15 +02:00
|
|
|
@app.get('/statistics/amount_of_games_over_time', response_model=statistic_classes.TimeLineStatistic)
|
2024-08-12 20:11:34 +02:00
|
|
|
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)):
|
2024-08-11 22:52:15 +02:00
|
|
|
|
2024-08-12 09:10:03 +02:00
|
|
|
def daterange(start_date: date, end_date: date, day_step):
|
2024-08-11 22:52:15 +02:00
|
|
|
days = int((end_date - start_date).days)
|
2024-08-12 09:10:03 +02:00
|
|
|
for n in range(0, days, day_step):
|
2024-08-11 22:52:15 +02:00
|
|
|
yield start_date + timedelta(n)
|
|
|
|
|
|
2024-08-12 12:08:08 +02:00
|
|
|
games_in_owned_collection = data_connection.get_user_owned_collection(session)
|
2024-08-11 22:52:15 +02:00
|
|
|
games_in_owned_collection.sort(key=lambda x: x.acquisition_date)
|
|
|
|
|
|
2024-08-12 09:18:43 +02:00
|
|
|
start_date = games_in_owned_collection[0].acquisition_date
|
|
|
|
|
|
|
|
|
|
if filter_expansions_out:
|
|
|
|
|
games_in_owned_collection = boardgame_filters.filter_expansions_out(games_in_owned_collection)
|
|
|
|
|
|
|
|
|
|
if only_expansions:
|
|
|
|
|
games_in_owned_collection = boardgame_filters.filter_non_expansions_out(games_in_owned_collection)
|
|
|
|
|
|
2024-08-11 22:52:15 +02:00
|
|
|
timeline_dict = {}
|
|
|
|
|
|
2024-08-12 09:18:43 +02:00
|
|
|
for current_date in daterange(start_date, date.today(), day_step):
|
2024-08-11 22:52:15 +02:00
|
|
|
games_in_collection_at_date = list(filter(lambda game: game.acquisition_date <= current_date, games_in_owned_collection))
|
|
|
|
|
timeline_dict[current_date] = len(games_in_collection_at_date)
|
|
|
|
|
|
|
|
|
|
statistic_dict = {
|
|
|
|
|
"name":"Amount of games in owned collection over time",
|
|
|
|
|
"result":timeline_dict
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
statistic_to_return = statistic_classes.TimeLineStatistic(**statistic_dict)
|
|
|
|
|
|
|
|
|
|
return statistic_to_return
|
|
|
|
|
|
2024-08-12 12:08:08 +02:00
|
|
|
@app.get('/statistics/games_played_per_year', response_model=statistic_classes.TimeLineStatistic)
|
2024-08-12 20:11:34 +02:00
|
|
|
def get_amount_of_games_played_per_year(query: ExpansionFilteringParams = Depends(), session: Session = Depends(get_db_session)):
|
2024-08-12 12:08:08 +02:00
|
|
|
all_plays = data_connection.get_plays(session)
|
|
|
|
|
|
|
|
|
|
all_plays.sort(key= lambda x: x.play_date)
|
|
|
|
|
|
2024-08-13 10:35:25 +02:00
|
|
|
if query.filter_expansions_out:
|
|
|
|
|
all_plays = play_filters.filter_out_expansion_plays(all_plays)
|
|
|
|
|
|
|
|
|
|
|
2024-08-12 12:08:08 +02:00
|
|
|
all_played_boardgame_ids = []
|
|
|
|
|
|
|
|
|
|
for play in all_plays:
|
|
|
|
|
if play.boardgame_id not in all_played_boardgame_ids:
|
|
|
|
|
all_played_boardgame_ids.append(play.boardgame_id)
|
|
|
|
|
|
|
|
|
|
first_year_played = all_plays[0].play_date.year
|
|
|
|
|
current_year = datetime.now().year
|
|
|
|
|
|
|
|
|
|
years_plays_dict = {}
|
|
|
|
|
|
|
|
|
|
for year in range(first_year_played, current_year + 1):
|
|
|
|
|
plays_in_year = list(filter(lambda x: x.play_date.year == year, all_plays))
|
|
|
|
|
years_plays_dict[datetime(year, 1,1)] = len(plays_in_year)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
statistic_dict = {
|
|
|
|
|
"name":"Amount of games played per year",
|
|
|
|
|
"result":years_plays_dict
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
statistic_to_return = statistic_classes.TimeLineStatistic(**statistic_dict)
|
|
|
|
|
|
|
|
|
|
return statistic_to_return
|
|
|
|
|
|
2024-08-11 22:18:12 +02:00
|
|
|
|
|
|
|
|
@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
|