From b9a038a9c52a93aeb8ff33e0ab9657b95ba8c06c Mon Sep 17 00:00:00 2001 From: Agustinefe Date: Thu, 28 Mar 2024 00:22:47 -0300 Subject: [PATCH] Add fixes --- app/controller/Users.py | 40 ++++++++++++++++++++++++++++++---------- app/repository/Users.py | 7 ++++++- app/service/Users.py | 5 ++++- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/app/controller/Users.py b/app/controller/Users.py index ef9e624..92efe27 100644 --- a/app/controller/Users.py +++ b/app/controller/Users.py @@ -1,4 +1,6 @@ from fastapi import status +from fastapi.encoders import jsonable_encoder +from fastapi.responses import JSONResponse from service.Users import UsersService @@ -8,22 +10,40 @@ def __init__(self, users_service: UsersService): def handle_get_user(self, user_id: int): user = self.users_service.get_user(user_id) - return {"message": user, "status": status.HTTP_200_OK} + return JSONResponse( + status_code=status.HTTP_200_OK, + content=jsonable_encoder({ + "message": user, + "status": status.HTTP_200_OK, + }) + ) def handle_get_all_users(self): users = self.users_service.get_all_users() - return {"users": users, "status": status.HTTP_200_OK} + return JSONResponse( + status_code=status.HTTP_200_OK, + content=jsonable_encoder({ + "message": users, + "status": status.HTTP_200_OK, + }) + ) def handle_create_user(self, user_data: dict): self.users_service.create_user(user_data) - return { - "message": "User created successfully", - "status": status.HTTP_201_CREATED, - } + return JSONResponse( + status_code=status.HTTP_201_CREATED, + content=jsonable_encoder({ + "message": "User created successfully", + "status": status.HTTP_201_CREATED, + }) + ) def handle_login(self, auth_code: str): user = self.users_service.login(auth_code) - return { - "message": user, - "status": status.HTTP_200_OK, - } + return JSONResponse( + status_code=status.HTTP_200_OK, + content=jsonable_encoder({ + "message": user, + "status": status.HTTP_200_OK, + }) + ) diff --git a/app/repository/Users.py b/app/repository/Users.py index 5ff43d1..9701b42 100644 --- a/app/repository/Users.py +++ b/app/repository/Users.py @@ -2,6 +2,7 @@ from sqlalchemy.orm import Session from os import environ from typing import Optional +from models.database import Base from models.users import User @@ -25,6 +26,10 @@ def shutdown(self): def rollback(self): self.session.rollback() + def add(self, record: Base): + self.session.add(record) + self.session.commit() + def get_user(self, user_id: int): user = self.session.query(User).filter_by(id=user_id).first() return user.__dict__ if user else None @@ -43,7 +48,7 @@ def create_user( name: Optional[str] = None, gender: Optional[str] = None, photo: Optional[str] = None, - ): + ) -> User: user_data = {"email": email} if name is not None: diff --git a/app/service/Users.py b/app/service/Users.py index 1d7b7cf..14d1efe 100644 --- a/app/service/Users.py +++ b/app/service/Users.py @@ -1,5 +1,6 @@ from exceptions.UserException import UserNotFound from exceptions.LoginException import AuthenticationError +from models.users import User from repository.Users import UsersRepository import requests import os @@ -28,10 +29,12 @@ def login(self, auth_code: str): raise AuthenticationError("Authentication code is invalid") user_info = self._get_user_info(access_token) + print(user_info) user = self.user_repository.get_user_by_email(user_info["email"]) if user is None: - user = self.user_repository.create_user(**user_info) + self.user_repository.add(User(**user_info)) + user = self.user_repository.get_user_by_email(user_info["email"]) return user