2024-07-24 22:53:56 +02:00
|
|
|
from typing import Union
|
2024-08-12 12:08:08 +02:00
|
|
|
from pydantic import BaseModel
|
|
|
|
|
from sqlmodel import Session
|
2024-08-16 10:13:47 +02:00
|
|
|
from threading import Thread
|
2024-08-08 18:13:01 +02:00
|
|
|
|
2024-08-15 11:07:11 +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-22 09:18:18 +02:00
|
|
|
from src.classes import boardgame_classes, play_classes, statistic_classes, people_classes
|
2024-08-17 09:01:00 +02:00
|
|
|
from src.modules import data_connection, statistic_creator
|
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-16 10:13:47 +02:00
|
|
|
is_refreshing = False
|
|
|
|
|
|
2024-08-19 13:50:27 +02:00
|
|
|
async def get_session():
|
2024-08-15 11:07:11 +02:00
|
|
|
with Session(data_connection.get_db_engine()) as session:
|
|
|
|
|
yield session
|
|
|
|
|
|
|
|
|
|
def refresh_data():
|
2024-08-16 10:13:47 +02:00
|
|
|
global is_refreshing
|
|
|
|
|
|
|
|
|
|
is_refreshing = True
|
2024-08-15 11:07:11 +02:00
|
|
|
data_connection.delete_database()
|
2024-08-16 10:13:47 +02:00
|
|
|
data_connection.create_db_and_tables()
|
2024-08-15 11:07:11 +02:00
|
|
|
with Session(data_connection.get_db_engine()) as session:
|
|
|
|
|
data_connection.get_user_collection(session)
|
|
|
|
|
data_connection.get_user_owned_collection(session)
|
|
|
|
|
data_connection.get_user_wishlist_collection(session)
|
2024-08-16 10:13:47 +02:00
|
|
|
data_connection.get_plays(session)
|
|
|
|
|
|
|
|
|
|
is_refreshing = False
|
2024-08-15 11:07:11 +02:00
|
|
|
|
2024-08-08 16:35:06 +02:00
|
|
|
@asynccontextmanager
|
|
|
|
|
async def lifespan(app: FastAPI):
|
|
|
|
|
# Startup
|
2024-08-15 11:07:11 +02:00
|
|
|
#data_connection.delete_database()
|
2024-08-08 16:35:06 +02:00
|
|
|
data_connection.create_db_and_tables()
|
2024-08-15 11:07:11 +02:00
|
|
|
|
|
|
|
|
#refresh_data()
|
2024-08-08 16:35:06 +02:00
|
|
|
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
|
2024-08-15 11:18:27 +02:00
|
|
|
class BoardgameFilterParams(BaseModel):
|
2024-08-12 12:08:08 +02:00
|
|
|
filter_expansions_out: bool = False
|
|
|
|
|
only_expansions: bool = False
|
|
|
|
|
|
|
|
|
|
def do_filtering(self,boardgame_list):
|
|
|
|
|
if self.filter_expansions_out:
|
2024-08-15 11:18:27 +02:00
|
|
|
boardgame_list = boardgame_filters.filter_expansions_out(boardgame_list)
|
2024-08-12 12:08:08 +02:00
|
|
|
|
|
|
|
|
if self.only_expansions:
|
2024-08-15 11:18:27 +02:00
|
|
|
boardgame_list = boardgame_filters.filter_non_expansions_out(boardgame_list)
|
|
|
|
|
|
|
|
|
|
return boardgame_list
|
|
|
|
|
|
|
|
|
|
class PlayFilterParams(BaseModel):
|
|
|
|
|
filter_expansions_out: bool = False
|
|
|
|
|
only_expansions: bool = False
|
|
|
|
|
|
|
|
|
|
def do_filtering(self, play_list):
|
|
|
|
|
if self.filter_expansions_out:
|
|
|
|
|
play_list = play_filters.filter_expansions_out(play_list)
|
|
|
|
|
|
|
|
|
|
if self.only_expansions:
|
|
|
|
|
play_list = play_filters.filter_non_expansions_out(play_list)
|
|
|
|
|
|
|
|
|
|
return play_list
|
2024-08-12 12:08:08 +02:00
|
|
|
|
|
|
|
|
|
2024-07-24 22:53:56 +02:00
|
|
|
@app.get("/")
|
|
|
|
|
def read_root():
|
|
|
|
|
return {"Hello": "World"}
|
|
|
|
|
|
2024-08-16 10:13:47 +02:00
|
|
|
@app.get('/refresh')
|
|
|
|
|
def refresh():
|
|
|
|
|
if not is_refreshing:
|
|
|
|
|
Thread(target=refresh_data).start()
|
|
|
|
|
return {"Status": "Started refresh"}
|
|
|
|
|
else:
|
|
|
|
|
return {"Status": "Already refreshing"}
|
|
|
|
|
|
|
|
|
|
|
2024-08-21 21:03:49 +02:00
|
|
|
@app.get("/boardgame", response_model=boardgame_classes.BoardGamePublic)
|
2024-08-15 11:07:11 +02:00
|
|
|
def get_boardgame_by_id(id: int, session: Session = Depends(get_session)):
|
2024-08-18 17:12:19 +02:00
|
|
|
requested_boardgame = data_connection.get_boardgame(session, id)
|
2024-07-31 16:03:24 +02:00
|
|
|
return requested_boardgame
|
|
|
|
|
|
2024-08-22 09:18:18 +02:00
|
|
|
@app.get("/owned", response_model=list[boardgame_classes.BoardGamePublic])
|
2024-08-15 11:18:27 +02:00
|
|
|
def get_owned_collection(query: BoardgameFilterParams = Depends(), session: Session = Depends(get_session)):
|
2024-08-15 11:07:11 +02:00
|
|
|
to_return_boardgames = data_connection.get_user_owned_collection(session)
|
2024-08-12 09:10:03 +02:00
|
|
|
|
2024-08-15 11:18:27 +02:00
|
|
|
to_return_boardgames = query.do_filtering(to_return_boardgames)
|
2024-08-12 09:10:03 +02:00
|
|
|
|
2024-08-16 10:13:47 +02:00
|
|
|
to_return_boardgames.sort(key=lambda x: x.name)
|
|
|
|
|
|
2024-08-11 19:58:22 +02:00
|
|
|
return to_return_boardgames
|
2024-08-01 10:35:51 +02:00
|
|
|
|
2024-08-22 09:18:18 +02:00
|
|
|
@app.get('/collection', response_model=list[boardgame_classes.BoardGamePublic])
|
2024-08-19 13:50:27 +02:00
|
|
|
def get_collection(query: BoardgameFilterParams = Depends(), session: Session = Depends(get_session)):
|
|
|
|
|
to_return_boardgames = data_connection.get_user_collection(session)
|
|
|
|
|
|
|
|
|
|
to_return_boardgames = query.do_filtering(to_return_boardgames)
|
|
|
|
|
|
|
|
|
|
to_return_boardgames.sort(key=lambda x: x.name)
|
|
|
|
|
|
|
|
|
|
return to_return_boardgames
|
2024-08-01 10:35:51 +02:00
|
|
|
|
2024-08-22 09:18:18 +02:00
|
|
|
@app.get('/designers', response_model=list[people_classes.DesignerPublic])
|
|
|
|
|
def get_designers(session: Session = Depends(get_session)):
|
|
|
|
|
to_return_designers = data_connection.get_designers(session)
|
|
|
|
|
return to_return_designers
|
|
|
|
|
|
|
|
|
|
@app.get("/wishlist", response_model=list[boardgame_classes.BoardGamePublic])
|
2024-08-15 11:18:27 +02:00
|
|
|
def get_wishlist_collection(priority: int = 0, query: BoardgameFilterParams = Depends(), session: Session = Depends(get_session)):
|
2024-08-13 10:45:34 +02:00
|
|
|
|
2024-08-15 11:07:11 +02:00
|
|
|
to_return_boardgames = data_connection.get_user_wishlist_collection(session, priority)
|
2024-08-13 10:45:34 +02:00
|
|
|
|
2024-08-15 11:18:27 +02:00
|
|
|
to_return_boardgames = query.do_filtering(to_return_boardgames)
|
2024-08-13 10:45:34 +02:00
|
|
|
|
2024-08-16 10:13:47 +02:00
|
|
|
to_return_boardgames.sort(key=lambda x: x.name)
|
|
|
|
|
|
2024-08-13 10:45:34 +02:00
|
|
|
return to_return_boardgames
|
2024-08-12 12:08:08 +02:00
|
|
|
|
2024-08-02 12:48:35 +02:00
|
|
|
|
2024-08-22 09:55:31 +02:00
|
|
|
@app.get("/plays", response_model=list[play_classes.PlayPublic])
|
|
|
|
|
def get_plays(query: PlayFilterParams = Depends(), boardgame_id: int = -1, session: Session = Depends(get_session)):
|
2024-08-12 20:11:34 +02:00
|
|
|
|
2024-08-22 09:55:31 +02:00
|
|
|
requested_plays = data_connection.get_plays(session)
|
2024-08-12 20:11:34 +02:00
|
|
|
|
2024-08-22 09:55:31 +02:00
|
|
|
requested_plays = query.do_filtering(requested_plays)
|
2024-08-12 20:23:05 +02:00
|
|
|
|
2024-08-22 09:55:31 +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
|
|
|
|
2024-08-22 09:55:31 +02:00
|
|
|
return requested_plays
|
2024-08-11 21:27:59 +02:00
|
|
|
|
|
|
|
|
|
2024-08-12 12:16:28 +02:00
|
|
|
@app.get('/players', response_model=list[play_classes.PlayPlayerPublic])
|
2024-08-15 11:07:11 +02:00
|
|
|
def get_players_from_play(play_id: int, session: Session = Depends(get_session)):
|
|
|
|
|
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-15 11:18:27 +02:00
|
|
|
def get_amount_of_games(query: BoardgameFilterParams = Depends(), session: Session = Depends(get_session)):
|
2024-08-13 10:45:34 +02:00
|
|
|
|
2024-08-17 09:01:00 +02:00
|
|
|
statistic_to_return = statistic_creator.get_total_owned_games(session, query)
|
2024-08-11 21:52:36 +02:00
|
|
|
|
2024-08-11 22:18:12 +02:00
|
|
|
return statistic_to_return
|
|
|
|
|
|
2024-08-15 19:47:30 +02:00
|
|
|
@app.get('/statistics/total_collection_cost', response_model=statistic_classes.NumberStatistic)
|
|
|
|
|
def get_total_collection_cost(query: BoardgameFilterParams = Depends(), session: Session = Depends(get_session)):
|
|
|
|
|
|
2024-08-17 09:01:00 +02:00
|
|
|
statistic_to_return = statistic_creator.get_total_owned_collection_cost(session, query)
|
2024-08-15 19:47:30 +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-16 10:13:47 +02:00
|
|
|
def get_amount_of_games_over_time(query: BoardgameFilterParams = Depends(), day_step: int = 1, session: Session = Depends(get_session)):
|
2024-08-11 22:52:15 +02:00
|
|
|
|
2024-08-17 09:01:00 +02:00
|
|
|
statistic_to_return = statistic_creator.get_amount_of_games_over_time(session, query, day_step)
|
2024-08-11 22:52:15 +02:00
|
|
|
|
|
|
|
|
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-15 13:47:50 +02:00
|
|
|
def get_amount_of_games_played_per_year(query: PlayFilterParams = Depends(), session: Session = Depends(get_session)):
|
2024-08-12 12:08:08 +02:00
|
|
|
|
2024-08-17 09:01:00 +02:00
|
|
|
statistic_to_return = statistic_creator.get_amount_of_games_played_per_year(session, query)
|
2024-08-12 12:08:08 +02:00
|
|
|
|
|
|
|
|
return statistic_to_return
|
|
|
|
|
|
2024-08-11 22:18:12 +02:00
|
|
|
|
2024-08-15 10:29:54 +02:00
|
|
|
@app.get('/statistics/most_expensive_games', response_model=statistic_classes.GamesStatistic)
|
2024-08-17 09:01:00 +02:00
|
|
|
def get_most_expensive_games(query: BoardgameFilterParams = Depends(), top_amount: int = 10, session: Session = Depends(get_session)):
|
2024-08-13 10:45:34 +02:00
|
|
|
|
2024-08-17 09:01:00 +02:00
|
|
|
statistic_to_return = statistic_creator.get_most_expensive_games(session, query, top_amount)
|
2024-08-11 22:18:12 +02:00
|
|
|
|
|
|
|
|
return statistic_to_return
|
2024-08-15 10:29:54 +02:00
|
|
|
|
|
|
|
|
@app.get('/statistics/shelf_of_shame', response_model=statistic_classes.GamesStatistic)
|
2024-08-17 09:01:00 +02:00
|
|
|
def get_shelf_of_shame(query: BoardgameFilterParams = Depends(), session: Session = Depends(get_session)):
|
2024-08-15 13:47:50 +02:00
|
|
|
|
2024-08-17 09:01:00 +02:00
|
|
|
statistic_to_return = statistic_creator.get_shelf_of_shame(session, query)
|
2024-08-15 13:47:50 +02:00
|
|
|
|
|
|
|
|
return statistic_to_return
|