Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HAN-39 #6

Merged
merged 6 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
violetaperezandrade marked this conversation as resolved.
Show resolved Hide resolved
"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)
Loading