🏗️ Added first DTO

master
Alix JEUDI--LEMOINE 5 months ago
parent cb2c807406
commit a2cc91d681

@ -0,0 +1,5 @@
from pydantic import BaseModel
class UserRegisterDTO(BaseModel):
username: str
password: str

@ -0,0 +1 @@
from .UserRegisterDTO import UserRegisterDTO

@ -14,6 +14,9 @@ from app.serializers import * # Import all serializers (detailed in __init__.py)
# Import all models (detailed in __init__.py)
from app.models import *
# Import all DTOs (detailed in __init__.py)
from app.dto import *
# Contains all constants
from app.config import *
@ -27,6 +30,12 @@ app = FastAPI()
# OAuth2 scheme
oauth2_scheme = OAuth2PasswordBearer(tokenUrl=TOKEN_URL)
# Collections
users_collection = db["users"]
pins_collection = db["pins"]
friends_collection = db["friends"]
# Token management
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
to_encode = data.copy()
@ -62,14 +71,10 @@ async def get_current_user(token: str = Depends(oauth2_scheme)):
return user
# Collections
users_collection = db["users"]
pins_collection = db["pins"]
friends_collection = db["friends"]
# Routes - TODO: find workaround to display 401/409/... HTTP error codes in openapi.json
@app.post("/register", response_model=Token)
async def register(user: User):
async def register(user: UserRegisterDTO):
user_exists = users_collection.find_one({"username": user.username})
if user_exists:
raise HTTPException(

Loading…
Cancel
Save