users api done
This commit is contained in:
Generated
+3
@@ -0,0 +1,3 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<settings>
|
||||||
|
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||||
|
<version value="1.0" />
|
||||||
|
</settings>
|
||||||
|
</component>
|
||||||
Generated
+8
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="PYTHON_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="jdk" jdkName="Python 3.10 (venv)" jdkType="Python SDK" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
Generated
+7
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Black">
|
||||||
|
<option name="sdkName" value="Python 3.10 (venv)" />
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (venv)" project-jdk-type="Python SDK" />
|
||||||
|
</project>
|
||||||
Generated
+8
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/messenger_app.iml" filepath="$PROJECT_DIR$/.idea/messenger_app.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -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.")
|
||||||
@@ -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)
|
||||||
Binary file not shown.
Binary file not shown.
@@ -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)
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,8 +1,8 @@
|
|||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
from messenger_app.api.v1.models.message import Message
|
from api.v1.models.message import Message
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
@router.post("/messages/", response_model=Message)
|
@router.post("/messages/", response_model=Message)
|
||||||
async def create_message(message: Message):
|
async def create_message(message: Message):
|
||||||
# You would typically save the message to the database here
|
# You would typically save the message to the database here
|
||||||
|
|||||||
@@ -1,9 +1,45 @@
|
|||||||
from fastapi import APIRouter, Depends
|
from fastapi import APIRouter, HTTPException
|
||||||
from messenger_app.api.v1.dependencies.auth import get_current_user
|
from api.v1.models.user import User
|
||||||
from messenger_app.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 = APIRouter()
|
||||||
|
|
||||||
@router.get("/users/me", response_model=User)
|
|
||||||
async def read_users_me(current_user: User = Depends(get_current_user)):
|
@router.post("/users")
|
||||||
return current_user
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,6 +1,9 @@
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
class Message(BaseModel):
|
class Message(BaseModel):
|
||||||
text: str
|
text: str
|
||||||
sender_id: int
|
sender_id: int
|
||||||
receiver_id: int
|
receiver_id: int
|
||||||
|
timestamp: datetime
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
class User(BaseModel):
|
class User(BaseModel):
|
||||||
id: int
|
id: int
|
||||||
username: str
|
username: str
|
||||||
|
password: str
|
||||||
|
email: str
|
||||||
|
|||||||
Binary file not shown.
@@ -1,7 +1,7 @@
|
|||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from messenger_app.api.v1.endpoints import users, messages
|
from api.v1.endpoints import users, messages
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
app.include_router(users.router, prefix="/v1", tags=["users"])
|
app.include_router(users.router)
|
||||||
app.include_router(messages.router, prefix="/v1", tags=["messages"])
|
app.include_router(messages.router)
|
||||||
|
|||||||
Reference in New Issue
Block a user