diff --git a/messenger_app/.idea/.gitignore b/messenger_app/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/messenger_app/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/messenger_app/.idea/inspectionProfiles/profiles_settings.xml b/messenger_app/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/messenger_app/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/messenger_app/.idea/messenger_app.iml b/messenger_app/.idea/messenger_app.iml new file mode 100644 index 0000000..d27d2cf --- /dev/null +++ b/messenger_app/.idea/messenger_app.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/messenger_app/.idea/misc.xml b/messenger_app/.idea/misc.xml new file mode 100644 index 0000000..3a91067 --- /dev/null +++ b/messenger_app/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/messenger_app/.idea/modules.xml b/messenger_app/.idea/modules.xml new file mode 100644 index 0000000..10da75e --- /dev/null +++ b/messenger_app/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/messenger_app/.idea/vcs.xml b/messenger_app/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/messenger_app/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/messenger_app/api/__init__.py b/messenger_app/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/messenger_app/api/__pycache__/__init__.cpython-310.pyc b/messenger_app/api/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..9cedb60 Binary files /dev/null and b/messenger_app/api/__pycache__/__init__.cpython-310.pyc differ diff --git a/messenger_app/api/v1/__init__.py b/messenger_app/api/v1/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/messenger_app/api/v1/__pycache__/__init__.cpython-310.pyc b/messenger_app/api/v1/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..dd98acc Binary files /dev/null and b/messenger_app/api/v1/__pycache__/__init__.cpython-310.pyc differ diff --git a/messenger_app/api/v1/database/__init__.py b/messenger_app/api/v1/database/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/messenger_app/api/v1/database/__pycache__/__init__.cpython-310.pyc b/messenger_app/api/v1/database/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..f658ddb Binary files /dev/null and b/messenger_app/api/v1/database/__pycache__/__init__.cpython-310.pyc differ diff --git a/messenger_app/api/v1/database/__pycache__/postgres_database.cpython-310.pyc b/messenger_app/api/v1/database/__pycache__/postgres_database.cpython-310.pyc new file mode 100644 index 0000000..88ab949 Binary files /dev/null and b/messenger_app/api/v1/database/__pycache__/postgres_database.cpython-310.pyc differ diff --git a/messenger_app/api/v1/database/__pycache__/user_queries.cpython-310.pyc b/messenger_app/api/v1/database/__pycache__/user_queries.cpython-310.pyc new file mode 100644 index 0000000..39b04cc Binary files /dev/null and b/messenger_app/api/v1/database/__pycache__/user_queries.cpython-310.pyc differ diff --git a/messenger_app/api/v1/database/postgres_database.py b/messenger_app/api/v1/database/postgres_database.py new file mode 100644 index 0000000..e04e293 --- /dev/null +++ b/messenger_app/api/v1/database/postgres_database.py @@ -0,0 +1,38 @@ +import psycopg2 + +DATABASE_CONFIG = { + 'dbname': 'messenger', + 'user': 'andrei_cerbu', + 'password': 'andrei', + 'host': 'localhost', + 'port': '5432' +} + + +def create_connection(): + try: + connection = psycopg2.connect(**DATABASE_CONFIG) + return connection + except psycopg2.Error as e: + print(f"Error: Unable to connect to the database\n{e}") + return None + + +def execute_query(connection, query, params=None, fetchall=False): + try: + with connection.cursor() as cursor: + cursor.execute(query, params) + if fetchall: + result = cursor.fetchall() + return result + else: + connection.commit() # Commit the transaction for INSERT, UPDATE, DELETE queries + except psycopg2.Error as e: + print(f"Error: Unable to execute the query\n{e}") + return None + + +def close_connection(connection): + if connection: + connection.close() + print("Connection closed.") diff --git a/messenger_app/api/v1/database/user_queries.py b/messenger_app/api/v1/database/user_queries.py new file mode 100644 index 0000000..e8c9e59 --- /dev/null +++ b/messenger_app/api/v1/database/user_queries.py @@ -0,0 +1,81 @@ +from api.v1.database.postgres_database import create_connection, execute_query, close_connection + + +def get_user_by_id(user_id): + connection = create_connection() + if connection: + try: + query = """ + SELECT * FROM users WHERE id = %s; + """ + params = (user_id,) + result = execute_query(connection, query, params, fetchall=True) + if result: + user_data = result[0] + return user_data + else: + return None + finally: + close_connection(connection) + + +def get_user_by_email(email): + connection = create_connection() + if connection: + try: + query = """ + SELECT * FROM users WHERE email = %s; + """ + params = (email,) + result = execute_query(connection, query, params, fetchall=True) + if result: + user_data = result[0] + return user_data + else: + return None + finally: + close_connection(connection) + + +def create_user_db(username, password, email): + connection = create_connection() + if connection: + try: + query = """ + INSERT INTO users (username, password, email) + VALUES (%s, %s, %s); + """ + params = (username, password, email) + execute_query(connection, query, params) + finally: + close_connection(connection) + + +def update_user_db(user_id, new_username, new_password, new_email): + connection = create_connection() + if connection: + try: + query = """ + UPDATE users + SET username = %s, password = %s, email = %s + WHERE id = %s; + """ + params = (new_username, new_password, new_email, user_id) + execute_query(connection, query, params) + print(f"User with ID {user_id} updated successfully.") + finally: + close_connection(connection) + + +def delete_user_db(user_id): + connection = create_connection() + if connection: + try: + query = """ + DELETE FROM users WHERE id = %s; + """ + params = (user_id,) + execute_query(connection, query, params) + print(f"User with ID {user_id} deleted successfully.") + finally: + close_connection(connection) \ No newline at end of file diff --git a/messenger_app/api/v1/dependencies/__init__.py b/messenger_app/api/v1/dependencies/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/messenger_app/api/v1/dependencies/__pycache__/__init__.cpython-310.pyc b/messenger_app/api/v1/dependencies/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..3fda7f2 Binary files /dev/null and b/messenger_app/api/v1/dependencies/__pycache__/__init__.cpython-310.pyc differ diff --git a/messenger_app/api/v1/dependencies/__pycache__/security.cpython-310.pyc b/messenger_app/api/v1/dependencies/__pycache__/security.cpython-310.pyc new file mode 100644 index 0000000..c3862c2 Binary files /dev/null and b/messenger_app/api/v1/dependencies/__pycache__/security.cpython-310.pyc differ diff --git a/messenger_app/api/v1/dependencies/auth.py b/messenger_app/api/v1/dependencies/auth.py new file mode 100644 index 0000000..e69de29 diff --git a/messenger_app/api/v1/dependencies/security.py b/messenger_app/api/v1/dependencies/security.py new file mode 100644 index 0000000..53ddfde --- /dev/null +++ b/messenger_app/api/v1/dependencies/security.py @@ -0,0 +1,11 @@ +import bcrypt + + +def hash_password(password): + salt = bcrypt.gensalt() + hashed_password = bcrypt.hashpw(password.encode('utf-8'), salt) + return hashed_password + + +def verify_password(password, hashed_password): + return bcrypt.checkpw(password.encode('utf-8'), hashed_password) \ No newline at end of file diff --git a/messenger_app/api/v1/endpoints/__init__.py b/messenger_app/api/v1/endpoints/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/messenger_app/api/v1/endpoints/__pycache__/__init__.cpython-310.pyc b/messenger_app/api/v1/endpoints/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..1d5cf1a Binary files /dev/null and b/messenger_app/api/v1/endpoints/__pycache__/__init__.cpython-310.pyc differ diff --git a/messenger_app/api/v1/endpoints/__pycache__/messages.cpython-310.pyc b/messenger_app/api/v1/endpoints/__pycache__/messages.cpython-310.pyc new file mode 100644 index 0000000..990361b Binary files /dev/null and b/messenger_app/api/v1/endpoints/__pycache__/messages.cpython-310.pyc differ diff --git a/messenger_app/api/v1/endpoints/__pycache__/users.cpython-310.pyc b/messenger_app/api/v1/endpoints/__pycache__/users.cpython-310.pyc new file mode 100644 index 0000000..b91d312 Binary files /dev/null and b/messenger_app/api/v1/endpoints/__pycache__/users.cpython-310.pyc differ diff --git a/messenger_app/api/v1/endpoints/messages.py b/messenger_app/api/v1/endpoints/messages.py index b7e3296..eec4f71 100644 --- a/messenger_app/api/v1/endpoints/messages.py +++ b/messenger_app/api/v1/endpoints/messages.py @@ -1,8 +1,8 @@ from fastapi import APIRouter -from messenger_app.api.v1.models.message import Message - +from api.v1.models.message import Message router = APIRouter() + @router.post("/messages/", response_model=Message) async def create_message(message: Message): # You would typically save the message to the database here diff --git a/messenger_app/api/v1/endpoints/users.py b/messenger_app/api/v1/endpoints/users.py index e210b84..f736961 100644 --- a/messenger_app/api/v1/endpoints/users.py +++ b/messenger_app/api/v1/endpoints/users.py @@ -1,9 +1,45 @@ -from fastapi import APIRouter, Depends -from messenger_app.api.v1.dependencies.auth import get_current_user -from messenger_app.api.v1.models.user import User +from fastapi import APIRouter, HTTPException +from api.v1.models.user import User +from api.v1.database.user_queries import get_user_by_email, get_user_by_id +from api.v1.database.user_queries import create_user_db, update_user_db, delete_user_db +from api.v1.dependencies.security import hash_password router = APIRouter() -@router.get("/users/me", response_model=User) -async def read_users_me(current_user: User = Depends(get_current_user)): - return current_user + +@router.post("/users") +async def create_user(user: User): + result = get_user_by_email(user.email) + if result: + raise HTTPException(status_code=409, detail='User already exists') + + create_user_db(username=user.username, password=hash_password(user.password), email=user.email) + created_user = get_user_by_email(user.email) + created_user_dict = { + "id": created_user[0], + "username": created_user[1], + "password": created_user[2], + "email": created_user[3], + } + + return {'status': 'successful', 'user': created_user_dict} + + +@router.put("/users") +async def update_user(new_info: User): + old_info = get_user_by_id(new_info.id) + if old_info is None: + raise HTTPException(status_code=404, detail='User not found') + update_user_db(user_id=new_info.id, new_email=new_info.email, + new_username=new_info.username, new_password=hash_password(new_info.password)) + + +@router.delete("/users") +async def delete_user(user_id: int): + user = get_user_by_id(user_id) + if user is None: + raise HTTPException(status_code=404, detail='User not found') + delete_user_db(user_id) + + + diff --git a/messenger_app/api/v1/models/__init__.py b/messenger_app/api/v1/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/messenger_app/api/v1/models/__pycache__/__init__.cpython-310.pyc b/messenger_app/api/v1/models/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..2cdda52 Binary files /dev/null and b/messenger_app/api/v1/models/__pycache__/__init__.cpython-310.pyc differ diff --git a/messenger_app/api/v1/models/__pycache__/message.cpython-310.pyc b/messenger_app/api/v1/models/__pycache__/message.cpython-310.pyc new file mode 100644 index 0000000..fb80689 Binary files /dev/null and b/messenger_app/api/v1/models/__pycache__/message.cpython-310.pyc differ diff --git a/messenger_app/api/v1/models/__pycache__/user.cpython-310.pyc b/messenger_app/api/v1/models/__pycache__/user.cpython-310.pyc new file mode 100644 index 0000000..c807701 Binary files /dev/null and b/messenger_app/api/v1/models/__pycache__/user.cpython-310.pyc differ diff --git a/messenger_app/api/v1/models/message.py b/messenger_app/api/v1/models/message.py index 87631fe..163006c 100644 --- a/messenger_app/api/v1/models/message.py +++ b/messenger_app/api/v1/models/message.py @@ -1,6 +1,9 @@ from pydantic import BaseModel +from datetime import datetime + class Message(BaseModel): text: str sender_id: int receiver_id: int + timestamp: datetime diff --git a/messenger_app/api/v1/models/user.py b/messenger_app/api/v1/models/user.py index 092f452..50ad3b2 100644 --- a/messenger_app/api/v1/models/user.py +++ b/messenger_app/api/v1/models/user.py @@ -1,5 +1,8 @@ from pydantic import BaseModel + class User(BaseModel): id: int username: str + password: str + email: str diff --git a/messenger_app/app/__pycache__/main.cpython-310.pyc b/messenger_app/app/__pycache__/main.cpython-310.pyc index e14832c..bf436c0 100644 Binary files a/messenger_app/app/__pycache__/main.cpython-310.pyc and b/messenger_app/app/__pycache__/main.cpython-310.pyc differ diff --git a/messenger_app/app/main.py b/messenger_app/app/main.py index b761b12..86434cf 100644 --- a/messenger_app/app/main.py +++ b/messenger_app/app/main.py @@ -1,7 +1,7 @@ from fastapi import FastAPI -from messenger_app.api.v1.endpoints import users, messages +from api.v1.endpoints import users, messages app = FastAPI() -app.include_router(users.router, prefix="/v1", tags=["users"]) -app.include_router(messages.router, prefix="/v1", tags=["messages"]) +app.include_router(users.router) +app.include_router(messages.router)