Refactor crud module
This commit is contained in:
46
src/main.py
46
src/main.py
@@ -1,13 +1,14 @@
|
||||
from datetime import datetime, timedelta
|
||||
from typing import List, Optional
|
||||
|
||||
import jwt
|
||||
from fastapi import Depends, FastAPI, HTTPException
|
||||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
||||
from passlib.context import CryptContext
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
import crud
|
||||
from crud import authentication as crud_authentication
|
||||
from crud import courseprogress as crud_courseprogress
|
||||
from crud import highscores as crud_highscores
|
||||
from crud import users as crud_users
|
||||
from database import SessionLocal, engine
|
||||
from enums import CourseEnum, MinigameEnum
|
||||
from models import Base
|
||||
@@ -28,11 +29,16 @@ def get_db():
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
def get_current_user_name(
|
||||
token: HTTPAuthorizationCredentials = Depends(bearer_scheme),
|
||||
):
|
||||
try:
|
||||
payload = jwt.decode(token.credentials, crud.jwt_secret, algorithms=[crud.ALGORITHM])
|
||||
payload = jwt.decode(
|
||||
token.credentials,
|
||||
crud_authentication.jwt_secret,
|
||||
algorithms=[crud_authentication.ALGORITHM],
|
||||
)
|
||||
username = payload.get("sub")
|
||||
if username is None:
|
||||
raise HTTPException(status_code=401, detail="Invalid JWT token")
|
||||
@@ -40,6 +46,7 @@ def get_current_user_name(
|
||||
except jwt.exceptions.DecodeError:
|
||||
raise HTTPException(status_code=401, detail="Invalid JWT token")
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
return {"message": "Hello world!"}
|
||||
@@ -47,45 +54,42 @@ async def root():
|
||||
|
||||
@app.get("/users", response_model=List[users.User])
|
||||
async def read_users(db: Session = Depends(get_db)):
|
||||
users = crud.get_users(db)
|
||||
return users
|
||||
return crud_users.get_users(db)
|
||||
|
||||
|
||||
@app.patch("/users")
|
||||
async def patch_current_user(
|
||||
user: users.UserCreate,
|
||||
current_user_name = Depends(get_current_user_name),
|
||||
current_user_name=Depends(get_current_user_name),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
crud.patch_user(db, current_user_name, user)
|
||||
crud_users.patch_user(db, current_user_name, user)
|
||||
|
||||
|
||||
@app.post("/register", response_model=users.User)
|
||||
async def register(user: users.UserCreate, db: Session = Depends(get_db)):
|
||||
return crud.register(
|
||||
db, user.username, user.password
|
||||
)
|
||||
return crud_authentication.register(db, user.username, user.password)
|
||||
|
||||
|
||||
@app.post("/login")
|
||||
async def login(user: users.UserCreate, db: Session = Depends(get_db)):
|
||||
return crud.login(db, user)
|
||||
return crud_authentication.login(db, user)
|
||||
|
||||
|
||||
@app.get("/highscores", response_model=List[users.UserHighScore])
|
||||
async def read_high_scores(
|
||||
async def get_high_scores(
|
||||
db: Session = Depends(get_db),
|
||||
minigame: Optional[MinigameEnum] = None,
|
||||
nr_highest: Optional[int] = None,
|
||||
):
|
||||
return crud.get_high_scores(db, minigame, nr_highest)
|
||||
return crud_highscores.get_high_scores(db, minigame, nr_highest)
|
||||
|
||||
|
||||
@app.post("/highscores", response_model=highscores.HighScore)
|
||||
async def create_high_score(
|
||||
high_score: highscores.HighScoreCreate, db: Session = Depends(get_db)
|
||||
):
|
||||
return crud.create_high_score(db=db, high_score=high_score)
|
||||
return crud_highscores.create_high_score(db=db, high_score=high_score)
|
||||
|
||||
|
||||
#### TESTING!! DELETE LATER
|
||||
@@ -96,20 +100,10 @@ async def protected_route(current_user_name=Depends(get_current_user_name)):
|
||||
return {"message": f"Hello, {current_user_name}!"}
|
||||
|
||||
|
||||
def authenticate_user(user: users.UserCreate, db: Session = Depends(get_db)):
|
||||
db_user = crud.get_user_by_username(db=db, username=user.username)
|
||||
if not db_user:
|
||||
return False
|
||||
hashed_password = db_user.hashed_password
|
||||
if not hashed_password or not pwd_context.verify(user.password, hashed_password):
|
||||
return False
|
||||
return db_user
|
||||
|
||||
|
||||
@app.get("/courseprogress", response_model=List[courseprogress.CourseProgressBase])
|
||||
async def get_course_progress(
|
||||
course: Optional[CourseEnum] = CourseEnum.All,
|
||||
current_user_name=Depends(get_current_user_name),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
return crud.get_course_progress(db, current_user_name, course)
|
||||
return crud_courseprogress.get_course_progress(db, current_user_name, course)
|
||||
|
||||
Reference in New Issue
Block a user