-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
58 lines (42 loc) · 1.56 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from dotenv import load_dotenv
load_dotenv()
from fastapi import FastAPI, Depends, HTTPException
from sqlalchemy.orm import Session
from fastapi.staticfiles import StaticFiles
from app.db.model import Base
from app.schemas import Score
from app.db.database import SessionLocal, engine
from app.service import score_service
Base.metadata.create_all(bind=engine)
BASE_URL = "/api/v1"
app = FastAPI()
app.mount("/", StaticFiles(directory="web", html = True), name="web")
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
@app.get(f"{BASE_URL}/highscore", )
async def get_highscore(db: Session = Depends(get_db)) -> int:
"""
Http Endpoint to get the highscore over all played games
"""
return score_service.get_highscore(db)
@app.post(f"{BASE_URL}/score/")
async def add_new_score(score: Score, db: Session = Depends(get_db)) -> str:
"""
Http Endpoint to add a new Score to the collection. If no user_id is provied,
will the script automatic generate a random Userid
"""
if score.points < 0:
raise HTTPException(detail="The score needs to be a positive value (>= 0)", status_code=409)
return score_service.insert_new_score(score, db)
@app.get(f"{BASE_URL}/score/")
async def get_all_scores(user_id: str = "", db: Session = Depends(get_db)) -> list[Score]:
"""
Http Endpoint to get all played games of a sigel User
"""
if user_id == "":
raise HTTPException(detail="You have to provide a UserId", status_code=409)
return score_service.get_scores_of_user(user_id, db)