Skip to content

Commit

Permalink
change patch endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
violetaperezandrade committed Apr 20, 2024
1 parent e4b84a1 commit 3858cef
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 53 deletions.
11 changes: 3 additions & 8 deletions app/controller/Users.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from service.Users import UsersService
from service.Auth import AuthService


class UsersController:
def __init__(self, users_service: UsersService,
auth_service: AuthService):
def __init__(self, users_service: UsersService):
self.users_service = users_service
self.auth_service = auth_service

def handle_get_user(self, user_id: int):
user = self.users_service.get_user(user_id)
Expand Down Expand Up @@ -54,10 +51,8 @@ def handle_login(self, auth_code: str):
},
)

def handle_update_user(self, user_id: int,
update_data: dict,
request: Request):
self.auth_service.authenticate(user_id, request)
def handle_update_user(self, update_data: dict, request: Request):
user_id = self.users_service.retrieve_user_id(request)
self.users_service.update_user(user_id, update_data)
return {
"message": "User updated successfully",
Expand Down
11 changes: 4 additions & 7 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
from fastapi import FastAPI, Request
from controller.Users import UsersController
from service.Users import UsersService
from service.Auth import AuthService
from repository.Users import UsersRepository
from schemas.Schemas import CreateUserSchema, UpdateUserSchema
from schemas.Schemas import LoginRequest


app = FastAPI()
users_repository = UsersRepository()
auth_service = AuthService()
users_service = UsersService(users_repository)
users_controller = UsersController(users_service, auth_service)
users_controller = UsersController(users_service)


@app.get("/")
Expand Down Expand Up @@ -39,9 +37,8 @@ def login_with_google(request: LoginRequest):
return users_controller.handle_login(request.auth_code)


@app.patch("/users/{user_id}")
async def update_user(user_id: int, update_data: UpdateUserSchema,
@app.patch("/users/me")
async def update_user(update_data: UpdateUserSchema,
request: Request):
return users_controller.handle_update_user(user_id,
update_data.dict(),
return users_controller.handle_update_user(update_data.dict(),
request)
38 changes: 0 additions & 38 deletions app/service/Auth.py

This file was deleted.

18 changes: 18 additions & 0 deletions app/service/Users.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import re
import jwt

TOKEN_FIELD_NAME = "x-access-token"


class UsersService:
def __init__(self, user_repository: UsersRepository):
Expand Down Expand Up @@ -103,3 +105,19 @@ def _validate_location(self, location):
-180 <= location["long"] <= 180:
return True
return False

def retrieve_user_id(self, request):
token = self.__get_token(request.headers)
payload = jwt.decode(token,
os.environ["JWT_SECRET"],
algorithms=["HS256"])
return int(payload.get("user_id"))

def __get_token(self, headers: dict):
keyName = None
for key in headers.keys():
if key.lower() == TOKEN_FIELD_NAME:
keyName = key
if not keyName:
return None
return headers.get(keyName)

0 comments on commit 3858cef

Please sign in to comment.