Skip to content

Commit f8e8f07

Browse files
committed
update
1 parent fbd74be commit f8e8f07

File tree

2 files changed

+56
-60
lines changed

2 files changed

+56
-60
lines changed

routes/answer.py

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from config import initialize
77
from fastapi.responses import JSONResponse
88
from botocore.exceptions import ClientError
9+
from itertools import chain
910

1011
ans_app = FastAPI()
1112

@@ -20,14 +21,13 @@ class AnswerStruct(BaseModel):
2021
answers: List[str] = Field(...)
2122
score: Optional[int] = Field(None)
2223
round: int
23-
subdomain: str = Field(...)
2424

2525
domain_mapping = {
2626
"UI/UX": "ui",
27-
"Graphic Design": "graphic",
28-
"Video Editing": "video",
29-
'Events':'events',
30-
'PnM':'pnm',
27+
"GRAPHIC DESIGN": "graphic",
28+
"VIDEO EDITING": "video",
29+
'EVENTS':'events',
30+
'PNM':'pnm',
3131
'WEB':'web',
3232
'IOT':'iot',
3333
'APP':'app',
@@ -49,12 +49,12 @@ async def post_answers(answerReq: AnswerStruct, idToken: str = Depends(get_acces
4949

5050
if not user:
5151
return JSONResponse(status_code=404, content="User not found.")
52-
53-
all_subdomains = {sub for subdomains in user.get("domain", {}).values() for sub in subdomains}
54-
if answerReq.subdomain not in all_subdomains:
55-
return JSONResponse(status_code=408, content="Domain was not selected")
5652

57-
mapped_domain = domain_mapping.get(answerReq.subdomain)
53+
flat_domains = list(chain.from_iterable(user.get("domain", []).values()))
54+
print(flat_domains)
55+
if answerReq.domain not in flat_domains:
56+
return JSONResponse(status_code=408, content="Domain was not selected")
57+
mapped_domain = domain_mapping.get(answerReq.domain)
5858
domain_tables = resources['domain_tables']
5959
domain_table = domain_tables.get(mapped_domain)
6060

@@ -67,6 +67,8 @@ async def post_answers(answerReq: AnswerStruct, idToken: str = Depends(get_acces
6767
answers_dict = [{"question": q, "answer": a} for q, a in zip(answerReq.questions, answerReq.answers)]
6868
response = domain_table.get_item(Key={'email': email})
6969

70+
71+
7072
if answerReq.round == 1:
7173
if 'Item' in response:
7274
return JSONResponse(status_code=409, content="Answers already submitted")
@@ -103,33 +105,16 @@ async def post_answers(answerReq: AnswerStruct, idToken: str = Depends(get_acces
103105
)
104106

105107
user_table.update_item(
106-
Key={'uid': email},
107-
UpdateExpression=f"""
108-
SET round{answerReq.round} = if_not_exists(round{answerReq.round}, :empty_round)
109-
""",
110-
ExpressionAttributeValues={
111-
':empty_round': {}
112-
},
113-
)
114-
115-
user_table.update_item(Key={'uid': email},UpdateExpression=f"""
116-
SET round{answerReq.round}.#domain = list_append(
117-
if_not_exists(round{answerReq.round}.#domain, :empty_list),
118-
:new_value
119-
)
120-
""",
121-
ExpressionAttributeNames={
122-
'#domain': answerReq.domain
123-
},
124-
ExpressionAttributeValues={
125-
':new_value': [answerReq.subdomain],
126-
':empty_list': []
127-
},
128-
)
129-
108+
Key={'uid': email},
109+
UpdateExpression=f"SET round{answerReq.round} = list_append(if_not_exists(round{answerReq.round}, :empty_list), :new_value)",
110+
ExpressionAttributeValues={
111+
':new_value': [answerReq.domain],
112+
':empty_list': []
113+
},
114+
)
130115
return {"message": f"Answers for domain '{answerReq.domain}' submitted successfully for round {answerReq.round}."}
131116

132117
except ClientError as e:
133118
raise HTTPException(status_code=500, detail=f"DynamoDB error: {str(e)}")
134119
except Exception as e:
135-
raise HTTPException(status_code=400, detail=f"Error posting answers: {str(e)}")
120+
raise HTTPException(status_code=400, detail=f"Error posting answers: {str(e)}")

routes/user.py

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,21 @@ async def submit_username(
111111
except Exception as e:
112112
return JSONResponse(status_code=500, content= f"Internal Server Error: {str(e)}")
113113

114+
SUBDOMAIN_MAPPING = {
115+
"WEB": "Technical",
116+
"APP": "Technical",
117+
"AI/ML": "Technical",
118+
"IOT": "Technical",
119+
"PNM": "Management",
120+
"EVENTS": "Management",
121+
"UI/UX": "Design",
122+
"GRAPHIC DESIGN": "Design",
123+
"VIDEO EDITING": "Design"
124+
}
114125
@user.get("/dashboard")
115126
async def get_dashboard(
116-
round: int = Query(..., description="Round number"),
117-
authorization: str = Depends(get_access_token),
127+
round: int = Query(..., description="Round number"),
128+
authorization: str = Depends(get_access_token),
118129
resources: dict = Depends(get_resources)
119130
):
120131
try:
@@ -126,38 +137,38 @@ async def get_dashboard(
126137
except Exception as token_error:
127138
raise HTTPException(status_code=401, detail=f"Invalid token: {str(token_error)}")
128139

129-
email = decoded_token.get('email')
140+
email = decoded_token.get("email")
130141
if not email:
131142
raise HTTPException(status_code=400, detail="Email not found in ID token")
132143

133144
try:
134145
user_table = resources.get("user_table")
135-
response = user_table.get_item(Key={'uid': email})
136-
user = response.get('Item')
137-
if not user:
138-
raise HTTPException(status_code=404, detail="User not found")
146+
response = user_table.get_item(Key={"uid": email})
147+
user = response.get("Item", {})
148+
139149
except Exception as db_error:
140150
raise HTTPException(status_code=500, detail=f"Database lookup failed: {str(db_error)}")
141151

142-
all_domains = user.get('domain', None)
143-
completed_domains = user.get(f'round{round}', None)
144-
145-
if all_domains is None:
146-
raise HTTPException(status_code=400, detail="Domains not found in user data")
147-
if completed_domains is None:
148-
completed_domains = {}
152+
domain_data = user.get("domain", {}) # Dictionary of domains and their subdomains
153+
completed_subdomains = set(user.get(f"round{round}", [])) # Subdomains completed in the round
149154

155+
pending_list = []
156+
completed_list = []
150157

151-
pending = defaultdict(list)
152-
for domain, subdomains in all_domains.items():
153-
completed_subs = set(completed_domains.get(domain, []))
158+
for domain, subdomains in domain_data.items():
154159
for sub in subdomains:
155-
if sub not in completed_subs:
156-
pending[domain].append(sub)
157-
158-
pending = dict(pending)
159-
160-
return JSONResponse(status_code=200, content=pending)
161-
160+
category = SUBDOMAIN_MAPPING.get(sub.upper(), "Other")
161+
formatted_entry = f"{category}:{sub.upper()}"
162+
163+
if sub.upper() in completed_subdomains:
164+
completed_list.append(formatted_entry)
165+
else:
166+
pending_list.append(formatted_entry)
167+
168+
return JSONResponse(status_code=200, content={
169+
"pending": pending_list,
170+
"completed": completed_list
171+
})
172+
162173
except Exception as unexpected_error:
163-
raise HTTPException(status_code=500, detail=f"Unexpected server error: {str(unexpected_error)}")
174+
raise HTTPException(status_code=500, detail=f"Unexpected server error: {str(unexpected_error)}")

0 commit comments

Comments
 (0)