Skip to content

Commit

Permalink
HAN-39 (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
violetaperezandrade authored Apr 1, 2024
2 parents 3bc7d6e + 7bb17c9 commit f0d4452
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
7 changes: 7 additions & 0 deletions app/controller/Users.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@ def handle_login(self, auth_code: str):
"status": status.HTTP_200_OK,
})
)

def handle_update_user(self, user_id: int, update_data: dict):
self.users_service.update_user(user_id, update_data)
return {
"message": "User updated successfully",
"status": status.HTTP_200_OK,
}
7 changes: 6 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from controller.Users import UsersController
from service.Users import UsersService
from repository.Users import UsersRepository
from schemas.Schemas import CreateUserSchema
from schemas.Schemas import CreateUserSchema, UpdateUserSchema
from schemas.Schemas import LoginRequest


Expand Down Expand Up @@ -35,3 +35,8 @@ async def create_user(user_data: CreateUserSchema):
@app.post("/login")
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):
return users_controller.handle_update_user(user_id, update_data.dict())
8 changes: 8 additions & 0 deletions app/repository/Users.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ def create_user(
self.session.commit()
return new_user

def edit_user(self, user_id: int, data_to_edit: dict):
user = self.session.query(User).filter_by(id=user_id).first()
for field, value in data_to_edit.items():
setattr(user, field, value)

self.session.commit()
return user

def __parse_result(self, result):
if not result:
return []
Expand Down
9 changes: 8 additions & 1 deletion app/schemas/Schemas.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pydantic import BaseModel
from pydantic import BaseModel, HttpUrl
from typing import Optional


class UserSchema(BaseModel):
Expand All @@ -15,3 +16,9 @@ class CreateUserSchema(BaseModel):

class LoginRequest(BaseModel):
auth_code: str


class UpdateUserSchema(BaseModel):
name: Optional[str] = None
gender: Optional[str] = None
photo: Optional[HttpUrl] = None
7 changes: 7 additions & 0 deletions app/service/Users.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,10 @@ def _get_user_info(self, access_token):
if response.json().get("picture") is not None:
user_data['photo'] = response.json().get("picture")
return user_data

def update_user(self, user_id: int, update_data: dict):
# TODO: aca habria que chequear a partir del token, session o algo que
# es el propio usuario editando sus datos y no permitir
# que un usuario edite los de un tercero
self.get_user(user_id)
self.user_repository.edit_user(user_id, update_data)

0 comments on commit f0d4452

Please sign in to comment.