Skip to content

Commit

Permalink
Merge pull request #4 from lukas-holzner/addchatmessages
Browse files Browse the repository at this point in the history
Added chat message endpoint
  • Loading branch information
lukas-holzner authored Sep 30, 2024
2 parents 55d0152 + 4b37739 commit d7ce9e1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
13 changes: 12 additions & 1 deletion backend/app/models.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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")
30 changes: 28 additions & 2 deletions backend/app/routers/meetings.py
Original file line number Diff line number Diff line change
@@ -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()

Expand Down Expand Up @@ -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)):
Expand Down Expand Up @@ -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)
12 changes: 11 additions & 1 deletion backend/app/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,14 @@ class UserWithMeetings(User):
meetings: list[Meeting] = []

class MeetingWithUser(Meeting):
user: User
user: User

class ChatMessage(BaseModel):
id: int
message: str
user_id: int
meeting_id: int
timestamp: datetime

class ChatMessageCreate(BaseModel):
message: str

0 comments on commit d7ce9e1

Please sign in to comment.