proiect finalizat

This commit is contained in:
andrei-mihnea-cerbu
2024-01-13 12:59:19 +02:00
parent 54542da299
commit 67b7a241d7
53 changed files with 321 additions and 19 deletions
+3
View File
@@ -0,0 +1,3 @@
"""
The 'api' package contains the logic behind the endpoints of the backend. The main version of the api is 'v1'.
"""
@@ -6,6 +6,10 @@ from .signup import signup
router = APIRouter()
"""
The 'auth' packages contains the router configuration for the 'Authentication' endpoints.
"""
@router.post("/login")
async def login_router(login_info: Login):
@@ -4,6 +4,15 @@ from database.queries.user import get_user_by_email
async def login(login_info: Login):
"""
Verify user credentials
Args:
login_info (Login)
Returns:
dict : full user information besides password
"""
result = get_user_by_email(login_info.email)
if not result:
raise HTTPException(status_code=404, detail='User not in system.')
@@ -4,6 +4,15 @@ from database.queries.user import get_user_by_email, create_user_db
async def signup(user: User):
"""
Verify user credentials
Args:
user (User)
Returns:
dict : full user information besides password
"""
result = get_user_by_email(user.email)
if result:
raise HTTPException(status_code=409, detail='User already exists.')
@@ -4,6 +4,10 @@ from .get_conversation import get_conversation
from .get_conversation_url import get_conversation_url
from app.models import Message
'''
The 'messages' packages contains the router configuration for the 'Messages' endpoints.
'''
router = APIRouter()
@@ -3,6 +3,15 @@ from database.queries.messages import find_conversation_db
async def get_conversation(conversation_url: str):
"""
Retrieves the full context of the conversation
Args:
conversation_url (str)
Returns:
HTTP Status
"""
conversation = find_conversation_db(conversation_url)
if conversation is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Conversation not found")
@@ -4,6 +4,16 @@ from fastapi import HTTPException
async def get_conversation_url(id_user1: str, id_user2: str):
"""
Retrieves the conversation identifier
Args:
id_user1 (str)
id_user2 (str)
Returns:
(str) : conversation_url
"""
if get_user_by_id(id_user1) is None or get_user_by_id(id_user2) is None:
raise HTTPException(status_code=404, detail="Users not in the system.")
@@ -4,6 +4,15 @@ from database.queries.messages import send_message_db
async def send(message: Message):
"""
Stores message in MongoDB
Args:
message (Message)
Returns:
HTTP Status
"""
success = send_message_db(message)
if success:
@@ -5,6 +5,10 @@ from .update import update_user
from .delete import delete_user
from .get_all import get_all_users
'''
The 'messages' packages contains the router configuration for the 'Messages' endpoints.
'''
router = APIRouter()
@@ -4,6 +4,15 @@ from database.queries.user import get_user_by_email, create_user_db
async def create_user(user: User):
"""
Creates a user
Args:
user (User)
Returns:
complete user information, including id, if successful
"""
result = get_user_by_email(user.email)
if result:
raise HTTPException(status_code=409, detail='User already exists.')
+10 -1
View File
@@ -2,7 +2,16 @@ from fastapi import HTTPException
from database.queries.user import get_user_by_id, delete_user_db
async def delete_user(user_id: int):
async def delete_user(user_id):
"""
Deletes a user
Args:
user_id (str)
Returns:
complete user information, including id, if successful
"""
user = get_user_by_id(user_id)
if user is None:
raise HTTPException(status_code=404, detail='User not found')
@@ -3,4 +3,13 @@ from database.queries.user import get_all_users_db
async def get_all_users():
users = get_all_users_db()
"""
Gets all users from database
Args:
Nan
Returns:
A list of users, if exists
"""
return users
+3
View File
@@ -0,0 +1,3 @@
"""
The 'app' package contains de main API object plus all the routing. It also contains the 'models' for the API requests.
"""
+7
View File
@@ -1,3 +1,10 @@
from .user import User
from .message import Message
from .login import Login
'''
The 'models' package contains the schematics for the API body structures. In more detail:
- 'Login' model is used for the auth/login endpoint
- 'User' model is used for the auto/signup and users endpoints
- 'Message' model is used for the messages/send endpoint
'''
-1
View File
@@ -1,5 +1,4 @@
from pydantic import BaseModel
from datetime import datetime
class Message(BaseModel):
@@ -1,4 +1,11 @@
from .connection import MONGODB_CONFIG, MongoDbHandler
mongo_db_handler = MongoDbHandler(**MONGODB_CONFIG)
mongo_db_handler.connect()
mongo_db_handler.connect()
'''
The 'mongodb_database' package contains the definition of the 'MongoDbHandler' class which connects to the MongoDB
database used for storing the messages between the clients.
The object 'mongo_db_handler' is created inside this file and can be used throughout the lifetime of the application
'''
@@ -1,4 +1,12 @@
from .connection import POSTGRESQL_CONFIG, PostgreSQLHandler
postgresql_db_handler = PostgreSQLHandler(**POSTGRESQL_CONFIG)
postgresql_db_handler.connect()
postgresql_db_handler.connect()
'''
The 'postgresql_database' package contains the definition of the 'PostgreSQlHandler' class which connects to the MongoDB
database used for storing information about the users and the existing chats
The object 'postgresql_db_handler' is created inside this file and can be used throughout the lifetime
of the application
'''
@@ -0,0 +1,8 @@
'''
The 'queries' package contains functions which are used by the API to execute database operations. This package contains
implementations for 'messages' and 'users' requirements. The list of the functions is:
- create_conversation_db()
- find_conversation_db()
- get_conversation_url_db()
- send_message_db
'''
@@ -5,6 +5,16 @@ from datetime import datetime
def create_conversation_db(user1_id, user2_id):
"""
Creates the conversation identifier for two users.
Args:
user1_id (str): ID of first user.
user1_id (str): ID of second user.
Returns:
str: Conversation identifier/URL
"""
current_datetime = datetime.now().strftime("%Y%m%d%H%M%S")
unique_id = str(uuid.uuid4()).replace('-', '')
conversation_url = f"{current_datetime}_{unique_id}"
@@ -20,6 +30,15 @@ def create_conversation_db(user1_id, user2_id):
def find_conversation_db(conversation_url):
"""
Retrieves the conversation from MongoDB
Args:
conversation_url (str): conversation identifier
Returns:
dic: conversation
"""
collection = mongo_db_handler.get_collection()
query = {'name': conversation_url}
result_set = collection.find_one(query)
+51
View File
@@ -2,6 +2,15 @@ from database import postgresql_db_handler
def get_user_by_id(user_id):
"""
Creates the conversation identifier for two users.
Args:
user_id (str): user id
Returns:
User: information about the user if exists
"""
query = """
SELECT * FROM users WHERE id = %s;
"""
@@ -15,6 +24,15 @@ def get_user_by_id(user_id):
def get_user_by_email(email):
"""
Creates the conversation identifier for two users.
Args:
email (str): email of the user
Returns:
User: information about the user if exists
"""
query = """
SELECT * FROM users WHERE email = %s;
"""
@@ -28,6 +46,17 @@ def get_user_by_email(email):
def create_user_db(username, password, email):
"""
Creates used based on provided information
Args:
username (str)
password (str)
email (str)
Returns:
Nothing
"""
query = """
INSERT INTO users (username, password, email)
VALUES (%s, %s, %s);
@@ -37,6 +66,19 @@ def create_user_db(username, password, email):
def update_user_db(user_id, new_username, new_password, new_email):
"""
Updates user based on the new information
Args:
user_id (int)
new_username (str)
new_password (str)
new_email (str)
Returns:
Nothing
"""
query = """
UPDATE users
SET username = %s, password = %s, email = %s
@@ -48,6 +90,15 @@ def update_user_db(user_id, new_username, new_password, new_email):
def delete_user_db(user_id):
"""
Deletes a user
Args:
user_id: int
Returns:
Nothing
"""
query = """
DELETE FROM users WHERE id = %s;
"""