Skip to content

Commit

Permalink
Merge pull request #10 from QuizCast/dev
Browse files Browse the repository at this point in the history
room deletion endpoint added with authentication
  • Loading branch information
PinsaraPerera authored Dec 13, 2024
2 parents 874bef2 + f866a38 commit 56a7970
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app/api/endpoints/quizEntry.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import List, Union
from fastapi import APIRouter, Request, HTTPException, Depends, WebSocket, WebSocketDisconnect
from fastapi import APIRouter, Depends
from app.schemas import quizEntry_schema
from app.core.config import SUPABASE_URL, SUPABASE_KEY
from app.db.db import supabase
from app.crud import quiz_crud
from app.utils.autherization import get_current_user
import json


Expand All @@ -26,4 +27,8 @@ async def submit_answer(answer: quizEntry_schema.UpdateScore):
async def add_question(request: quizEntry_schema.AddQuestionsRequest):
return quiz_crud.add_questions(request.questions, request.user_id, request.time)

@router.delete("/deleteRoom", dependencies=[Depends(get_current_user)])
async def delete_room(request: quizEntry_schema.DeleteRoomRequest):
return quiz_crud.delete_room(request)


31 changes: 31 additions & 0 deletions app/crud/quiz_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,34 @@ def add_questions(questions: List[quizEntry_schema.AddQuestion], user_id: int, t
except Exception as e:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Failed to add questions: {e}")

def delete_room(request: quizEntry_schema.DeleteRoomRequest):
try:
# Check if the room exists
room = supabase.table("leaderboard").select("id").eq("room_key", request.room_key).execute()
if not room.data:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Room not found")

# Check if the user is the host
if room.data[0]["id"] != request.user_id:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="You are not authorized to delete this room")

# Delete the participants in the room
response = supabase.table("participants").delete().eq("room_key", request.room_key).execute()
if response.data == []:
# Log the error for debugging, but do not raise an exception
print(f"Warning: No participants found for the room or failed to delete participants. Room key: {request.room_key}")

# Delete the questions in the room
response = supabase.table("questions").delete().eq("room_key", request.room_key).execute()
if not response.data:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Failed to delete questions")

# Delete the room
response = supabase.table("leaderboard").delete().eq("room_key", request.room_key).execute()
if not response.data:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Failed to delete room")

return {"message": "Room deleted successfully"}

except Exception as e:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Failed to delete room: {e}")
4 changes: 4 additions & 0 deletions app/schemas/quizEntry_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ class LeaderBoard(BaseModel):
room_key: int
name: str
score: int

class DeleteRoomRequest(BaseModel):
room_key: int
user_id: int

0 comments on commit 56a7970

Please sign in to comment.