diff --git a/app/api/endpoints/quizEntry.py b/app/api/endpoints/quizEntry.py index 68cb57f..4d21b16 100644 --- a/app/api/endpoints/quizEntry.py +++ b/app/api/endpoints/quizEntry.py @@ -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 @@ -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) + diff --git a/app/crud/quiz_crud.py b/app/crud/quiz_crud.py index 8c6aafe..1c832ae 100644 --- a/app/crud/quiz_crud.py +++ b/app/crud/quiz_crud.py @@ -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}") diff --git a/app/schemas/quizEntry_schema.py b/app/schemas/quizEntry_schema.py index edbebb4..45cf4f3 100644 --- a/app/schemas/quizEntry_schema.py +++ b/app/schemas/quizEntry_schema.py @@ -44,3 +44,7 @@ class LeaderBoard(BaseModel): room_key: int name: str score: int + +class DeleteRoomRequest(BaseModel): + room_key: int + user_id: int