This commit is contained in:
andrei-mihnea-cerbu
2024-01-12 22:19:19 +02:00
parent 37b2c068b8
commit e65b05b451
59 changed files with 349 additions and 190 deletions
@@ -0,0 +1,19 @@
from fastapi import APIRouter
from .send import send
from .get_conversation import get_conversation
from app.models import Message
router = APIRouter()
@router.post("/send")
async def send_router(message: Message):
return await send(message)
@router.get("/get_conversation/{conversation_url}")
async def get_conversation_router(conversation_url: str):
return await get_conversation(conversation_url)
__all__ = ["router"]
@@ -0,0 +1,9 @@
from fastapi import HTTPException, status
from database.queries.messages import find_conversation
async def get_conversation(conversation_url: str):
conversation = find_conversation(conversation_url)
if conversation is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Conversation not found")
return conversation
@@ -0,0 +1,17 @@
from fastapi import HTTPException
from app.models import Message
from database.queries.messages import create_conversation, send_message_to_mongodb
async def send(message: Message):
conversation_url = create_conversation(message.sender, message.receiver)
if conversation_url is not None:
success = send_message_to_mongodb(conversation_url, message.sender, message.conversation)
if success:
return {"success": True, "message": "Message sent successfully"}
raise HTTPException(status_code=500, detail="Internal Server Error")
raise HTTPException(status_code=500, detail="Error creating conversation")