From 4b3773986d1e40ceb76fe560f7578b5b1368fe2c Mon Sep 17 00:00:00 2001 From: Lukas Holzner Date: Mon, 30 Sep 2024 16:06:31 +0200 Subject: [PATCH] Added chat message endpoint --- backend/app/models.py | 13 ++++++++++++- backend/app/routers/meetings.py | 30 ++++++++++++++++++++++++++++-- backend/app/schemas.py | 12 +++++++++++- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/backend/app/models.py b/backend/app/models.py index 59f63af..31e8777 100644 --- a/backend/app/models.py +++ b/backend/app/models.py @@ -1,4 +1,4 @@ -from datetime import datetime +from datetime import datetime, UTC from sqlalchemy import Column, Integer, String, ForeignKey, DateTime from sqlalchemy.orm import relationship from .database import Base @@ -22,3 +22,14 @@ class Meeting(Base): user = relationship("User", back_populates="meetings") +class ChatMessage(Base): + __tablename__ = "chat_messages" + + id = Column(Integer, primary_key=True, index=True) + message = Column(String) + user_id = Column(Integer, ForeignKey("users.id")) + meeting_id = Column(Integer, ForeignKey("meetings.id")) + timestamp = Column(DateTime, default=lambda: datetime.now(UTC)) + + user = relationship("User") + meeting = relationship("Meeting") diff --git a/backend/app/routers/meetings.py b/backend/app/routers/meetings.py index 42517d1..499091e 100644 --- a/backend/app/routers/meetings.py +++ b/backend/app/routers/meetings.py @@ -1,10 +1,11 @@ from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session from typing import List - +from datetime import datetime, UTC from .. import schemas from ..database import get_db -from ..models import Meeting +from ..models import Meeting, ChatMessage +from users import get_user router = APIRouter() @@ -38,6 +39,16 @@ def delete_meeting(db: Session, meeting_id: int): db.commit() return db_meeting +def create_chat_message(db: Session, meeting_id: int, user_id: int, chat_message: schemas.ChatMessageCreate): + db_chat_message = ChatMessage(**chat_message.dict(), meeting_id=meeting_id, user_id=user_id, timestamp=datetime.now(UTC)) + db.add(db_chat_message) + db.commit() + db.refresh(db_chat_message) + return db_chat_message + +def get_chat_messages(db: Session, meeting_id: int, skip: int = 0, limit: int = 100): + return db.query(ChatMessage).filter(ChatMessage.meeting_id == meeting_id).offset(skip).limit(limit).all() + # Meeting CRUD endpoints @router.post("/meetings/", response_model=schemas.Meeting) def create_meeting_route(meeting: schemas.MeetingCreate, db: Session = Depends(get_db)): @@ -68,3 +79,18 @@ def delete_meeting_route(meeting_id: int, db: Session = Depends(get_db)): if db_meeting is None: raise HTTPException(status_code=404, detail="Meeting not found") return db_meeting + + +@router.get("/meetings/{meeting_id}/user/{user_id}", response_model=List[schemas.ChatMessage]) +def get_chat_messages_route(meeting_id: int, user_id: int, db: Session = Depends(get_db)): + meeting = get_meeting(db, meeting_id=meeting_id) + if meeting is None: + raise HTTPException(status_code=404, detail="Meeting not found") + user = get_user(db, user_id=user_id) + if user is None: + raise HTTPException(status_code=404, detail="User not found") + return get_chat_messages(db, meeting_id=meeting_id, user_id=user_id) + +@router.post("/meetings/{meeting_id}/user/{user_id}", response_model=schemas.ChatMessage) +def create_chat_message(meeting_id: int, user_id: int, chat_message: schemas.ChatMessageCreate, db: Session = Depends(get_db)): + return create_chat_message(db, meeting_id=meeting_id, user_id=user_id, chat_message=chat_message) \ No newline at end of file diff --git a/backend/app/schemas.py b/backend/app/schemas.py index 4ca057a..215865b 100644 --- a/backend/app/schemas.py +++ b/backend/app/schemas.py @@ -32,4 +32,14 @@ class UserWithMeetings(User): meetings: list[Meeting] = [] class MeetingWithUser(Meeting): - user: User \ No newline at end of file + user: User + +class ChatMessage(BaseModel): + id: int + message: str + user_id: int + meeting_id: int + timestamp: datetime + +class ChatMessageCreate(BaseModel): + message: str