11from 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
44from middleware .verifyToken import get_access_token
55from firebase_admin import auth
66from config import initialize
7+ from botocore .exceptions import ClientError
78
89ans_app = FastAPI ()
910
1011resources = initialize ()
1112user_table = resources ['user_table' ]
1213firebase_app = resources ['firebase_app' ]
1314
14- class QuestionAnswer (BaseModel ):
15- question : str
16- answer : str
17-
1815class 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" )
2521async 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 )} " )
0 commit comments