Skip to content

Commit

Permalink
Merge branch 'master' into HAN-39
Browse files Browse the repository at this point in the history
  • Loading branch information
violetamedallia committed Apr 1, 2024
2 parents 9e58d69 + 3bc7d6e commit d914a12
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
36 changes: 30 additions & 6 deletions app/controller/Users.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from fastapi import status
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from service.Users import UsersService


Expand All @@ -8,25 +10,47 @@ 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,
})
)

def handle_update_user(self, user_id: int, update_data: dict):
self.users_service.update_user(user_id, update_data)
Expand Down
7 changes: 6 additions & 1 deletion app/repository/Users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
Expand All @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion app/service/Users.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit d914a12

Please sign in to comment.