Skip to content

Commit d48ce01

Browse files
authored
Merge pull request #16 from IEEECS-VIT/aryan
login responses
2 parents aa5bfd9 + 80e2f1b commit d48ce01

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

routes/answer.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,58 @@
11
from fastapi import FastAPI, HTTPException, Depends
2-
from typing import List, Dict
3-
from pydantic import BaseModel
2+
from typing import List
3+
from pydantic import BaseModel, Field
44
from middleware.verifyToken import get_access_token
55
from firebase_admin import auth
66
from config import initialize
7+
from botocore.exceptions import ClientError
78

89
ans_app = FastAPI()
910

1011
resources = initialize()
1112
user_table = resources['user_table']
1213
firebase_app = resources['firebase_app']
1314

14-
class QuestionAnswer(BaseModel):
15-
question: str
16-
answer: str
17-
1815
class AnswerStruct(BaseModel):
19-
domain: str
20-
questions: List[str]
21-
answers: List[str]
22-
23-
# Route for posting answers
16+
domain: str = Field(..., title="Domain for the answers")
17+
questions: List[str] = Field(..., title="List of questions")
18+
answers: List[str] = Field(..., title="List of answers")
19+
2420
@ans_app.post("/post-answer")
2521
async def post_answers(answerReq: AnswerStruct, idToken: str = Depends(get_access_token)):
2622
try:
2723
# Validate token and fetch user records
28-
decoded_token = auth.verify_id_token(idToken, app=firebase_app)
24+
decoded_token = auth.verify_id_token(idToken, app=resources["firebase_app"])
2925
email = decoded_token.get("email")
26+
print(answerReq)
27+
28+
if not email:
29+
raise HTTPException(status_code=401, detail="Invalid or missing email in token.")
3030

3131
response = user_table.get_item(Key={"uid": email})
3232
user = response.get("Item")
3333

3434
if not user:
35-
raise HTTPException(status_code=404, detail="User not found")
35+
raise HTTPException(status_code=404, detail="User not found.")
3636

37-
# Extract existing Q&A data
38-
existing_qna = user.get("qna", {})
37+
# Validate input lengths
38+
if len(answerReq.questions) != len(answerReq.answers):
39+
raise HTTPException(
40+
status_code=400,
41+
detail="Questions and answers lists must have the same length."
42+
)
3943

44+
# Check for existing domain
45+
existing_qna = user.get("qna", {})
4046
if answerReq.domain in existing_qna:
4147
raise HTTPException(
4248
status_code=400,
4349
detail=f"Answers for domain '{answerReq.domain}' have already been submitted."
4450
)
4551

4652
# Convert Pydantic model to dictionary (array of question/answer pairs)
47-
answers_dict = [{"question": q, "answer": a} for q, a in zip(answerReq.questions, answerReq.answers)]
53+
answers_dict = [
54+
{"question": q, "answer": a} for q, a in zip(answerReq.questions, answerReq.answers)
55+
]
4856

4957
# Add answers for the new domain
5058
existing_qna[answerReq.domain] = {"answers": answers_dict}
@@ -58,7 +66,9 @@ async def post_answers(answerReq: AnswerStruct, idToken: str = Depends(get_acces
5866
ReturnValues="UPDATED_NEW",
5967
)
6068

61-
return {"message": f"Answers for domain '{answerReq.domain}' submitted successfully.", "qna": existing_qna}
69+
return {"message": f"Answers for domain '{answerReq.domain}' submitted successfully."}
6270

71+
except ClientError as e:
72+
raise HTTPException(status_code=500, detail=f"DynamoDB error: {str(e)}")
6373
except Exception as e:
64-
raise HTTPException(status_code=400, detail=f"Error posting answers: {str(e)}")
74+
raise HTTPException(status_code=400, detail=f"Error posting answers: {str(e)}")

routes/user.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from fastapi import FastAPI, HTTPException, Depends
2+
from fastapi.responses import JSONResponse
23
from firebase_admin import auth
34
from pydantic import BaseModel
45
from middleware.verifyToken import get_access_token
@@ -24,13 +25,9 @@ async def login(authorization: str = Depends(get_access_token), resources: dict
2425
response = user_table.get_item(Key={'uid': email})
2526
user = response.get('Item')
2627
if user is not None:
27-
username = user.get("username")
28-
if username not in [None, ""]:
29-
return {"status": "success", "message": "Username exists", "email": email, "username": username}
30-
else:
31-
return {"status": "success", "message": "Username does not exist", "email": email}
28+
return JSONResponse(status_code=200, content={"detail": "Logged In Successfully"})
3229
else:
33-
raise HTTPException(status_code=401, detail="User not registered on VTOP")
30+
return JSONResponse(status_code=402, content={"detail":"User not registered on VTOP"})
3431

3532
except auth.InvalidIdTokenError:
3633
raise HTTPException(status_code=401, detail="Invalid ID token")

0 commit comments

Comments
 (0)