Created refresh endpoint
This commit is contained in:
parent
767214b1aa
commit
b27b6a2fd7
2 changed files with 27 additions and 2 deletions
27
src/main.py
27
src/main.py
|
|
@ -2,6 +2,7 @@ from typing import Union
|
||||||
from datetime import date, timedelta, datetime
|
from datetime import date, timedelta, datetime
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from sqlmodel import Session
|
from sqlmodel import Session
|
||||||
|
from threading import Thread
|
||||||
|
|
||||||
|
|
||||||
from fastapi import FastAPI, Depends
|
from fastapi import FastAPI, Depends
|
||||||
|
|
@ -12,16 +13,25 @@ from src.classes import boardgame_classes, play_classes, statistic_classes
|
||||||
from src.modules import data_connection
|
from src.modules import data_connection
|
||||||
from src.filters import boardgame_filters, play_filters
|
from src.filters import boardgame_filters, play_filters
|
||||||
|
|
||||||
|
is_refreshing = False
|
||||||
|
|
||||||
def get_session():
|
def get_session():
|
||||||
with Session(data_connection.get_db_engine()) as session:
|
with Session(data_connection.get_db_engine()) as session:
|
||||||
yield session
|
yield session
|
||||||
|
|
||||||
def refresh_data():
|
def refresh_data():
|
||||||
|
global is_refreshing
|
||||||
|
|
||||||
|
is_refreshing = True
|
||||||
data_connection.delete_database()
|
data_connection.delete_database()
|
||||||
|
data_connection.create_db_and_tables()
|
||||||
with Session(data_connection.get_db_engine()) as session:
|
with Session(data_connection.get_db_engine()) as session:
|
||||||
data_connection.get_user_collection(session)
|
data_connection.get_user_collection(session)
|
||||||
data_connection.get_user_owned_collection(session)
|
data_connection.get_user_owned_collection(session)
|
||||||
data_connection.get_user_wishlist_collection(session)
|
data_connection.get_user_wishlist_collection(session)
|
||||||
|
data_connection.get_plays(session)
|
||||||
|
|
||||||
|
is_refreshing = False
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
|
|
@ -80,6 +90,15 @@ class PlayFilterParams(BaseModel):
|
||||||
def read_root():
|
def read_root():
|
||||||
return {"Hello": "World"}
|
return {"Hello": "World"}
|
||||||
|
|
||||||
|
@app.get('/refresh')
|
||||||
|
def refresh():
|
||||||
|
if not is_refreshing:
|
||||||
|
Thread(target=refresh_data).start()
|
||||||
|
return {"Status": "Started refresh"}
|
||||||
|
else:
|
||||||
|
return {"Status": "Already refreshing"}
|
||||||
|
|
||||||
|
|
||||||
@app.get("/boardgame", response_model=Union[boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion])
|
@app.get("/boardgame", response_model=Union[boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion])
|
||||||
def get_boardgame_by_id(id: int, session: Session = Depends(get_session)):
|
def get_boardgame_by_id(id: int, session: Session = Depends(get_session)):
|
||||||
requested_boardgame: Union[boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion] = data_connection.get_boardgame(session, id)
|
requested_boardgame: Union[boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion] = data_connection.get_boardgame(session, id)
|
||||||
|
|
@ -91,6 +110,8 @@ def get_owned_collection(query: BoardgameFilterParams = Depends(), session: Sess
|
||||||
|
|
||||||
to_return_boardgames = query.do_filtering(to_return_boardgames)
|
to_return_boardgames = query.do_filtering(to_return_boardgames)
|
||||||
|
|
||||||
|
to_return_boardgames.sort(key=lambda x: x.name)
|
||||||
|
|
||||||
return to_return_boardgames
|
return to_return_boardgames
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -101,6 +122,8 @@ def get_wishlist_collection(priority: int = 0, query: BoardgameFilterParams = De
|
||||||
|
|
||||||
to_return_boardgames = query.do_filtering(to_return_boardgames)
|
to_return_boardgames = query.do_filtering(to_return_boardgames)
|
||||||
|
|
||||||
|
to_return_boardgames.sort(key=lambda x: x.name)
|
||||||
|
|
||||||
return to_return_boardgames
|
return to_return_boardgames
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -158,7 +181,7 @@ def get_total_collection_cost(query: BoardgameFilterParams = Depends(), session:
|
||||||
return statistic_to_return
|
return statistic_to_return
|
||||||
|
|
||||||
@app.get('/statistics/amount_of_games_over_time', response_model=statistic_classes.TimeLineStatistic)
|
@app.get('/statistics/amount_of_games_over_time', response_model=statistic_classes.TimeLineStatistic)
|
||||||
def get_amount_of_games_over_time(query: BoardgameFilterParams = Depends(), day_step: int = 1, filter_expansions_out: bool = False, only_expansions: bool = False, session: Session = Depends(get_session)):
|
def get_amount_of_games_over_time(query: BoardgameFilterParams = Depends(), day_step: int = 1, session: Session = Depends(get_session)):
|
||||||
|
|
||||||
def daterange(start_date: date, end_date: date, day_step):
|
def daterange(start_date: date, end_date: date, day_step):
|
||||||
days = int((end_date - start_date).days)
|
days = int((end_date - start_date).days)
|
||||||
|
|
@ -259,6 +282,8 @@ def get_shelf_of_shame(query: BoardgameFilterParams = Depends(), top_amount: int
|
||||||
|
|
||||||
owned_boardgames_no_plays = list(filter(lambda x: len(x.plays) == 0, owned_boardgames_in_collection))
|
owned_boardgames_no_plays = list(filter(lambda x: len(x.plays) == 0, owned_boardgames_in_collection))
|
||||||
|
|
||||||
|
owned_boardgames_no_plays.sort(key=lambda x: x.name)
|
||||||
|
|
||||||
statistic_dict = {
|
statistic_dict = {
|
||||||
"name":"Shelf of Shame",
|
"name":"Shelf of Shame",
|
||||||
"result":owned_boardgames_no_plays[0:top_amount]
|
"result":owned_boardgames_no_plays[0:top_amount]
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,7 @@ def get_user_wishlist_collection(session: Session, wishlist_priority: int = 0) -
|
||||||
to_return_boardgames = wishlisted_boardgames_from_db + wishlisted_boardgame_expansions_from_db
|
to_return_boardgames = wishlisted_boardgames_from_db + wishlisted_boardgame_expansions_from_db
|
||||||
|
|
||||||
if wishlist_priority != 0:
|
if wishlist_priority != 0:
|
||||||
to_return_boardgames = filter(lambda game: game.wishlist_priority == wishlist_priority, to_return_boardgames)
|
to_return_boardgames = list(filter(lambda game: game.wishlist_priority == wishlist_priority, to_return_boardgames))
|
||||||
|
|
||||||
return to_return_boardgames
|
return to_return_boardgames
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue