Skip to content

Commit be36ac3

Browse files
committed
wip: add POST nominee
1 parent 7a1974d commit be36ac3

File tree

1 file changed

+43
-26
lines changed

1 file changed

+43
-26
lines changed

src/nominees/urls.py

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,71 @@
88
NomineeInfoUpdateParams,
99
)
1010
from nominees.tables import NomineeInfo
11+
from utils.shared_models import DetailModel
1112
from utils.urls import AdminTypeEnum, admin_or_raise
1213

1314
router = APIRouter(
1415
prefix="/nominee",
1516
tags=["nominee"],
1617
)
1718

19+
20+
@router.post(
21+
"",
22+
description="Nominee info is always publically tied to election, so be careful!",
23+
response_model=NomineeInfoModel,
24+
responses={500: {"description": "failed to fetch new nominee", "model": DetailModel}},
25+
operation_id="create_nominee",
26+
)
27+
async def create_nominee(request: Request, db_session: database.DBSession, body: NomineeInfoModel):
28+
await admin_or_raise(request, db_session)
29+
await nominees.crud.create_nominee_info(
30+
db_session,
31+
NomineeInfo(
32+
computing_id=body.computing_id,
33+
full_name=body.full_name,
34+
linked_in=body.linked_in,
35+
instagram=body.instagram,
36+
email=body.email,
37+
discord_username=body.discord_username,
38+
),
39+
)
40+
41+
nominee_info = await nominees.crud.get_nominee_info(db_session, body.computing_id)
42+
if nominee_info is None:
43+
raise HTTPException(
44+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="couldn't fetch newly created nominee"
45+
)
46+
47+
return JSONResponse(nominee_info)
48+
49+
1850
@router.get(
1951
"/{computing_id:str}",
2052
description="Nominee info is always publically tied to election, so be careful!",
2153
response_model=NomineeInfoModel,
22-
responses={
23-
404: { "description": "nominee doesn't exist" }
24-
},
25-
operation_id="get_nominee"
54+
responses={404: {"description": "nominee doesn't exist"}},
55+
operation_id="get_nominee",
2656
)
27-
async def get_nominee_info(
28-
request: Request,
29-
db_session: database.DBSession,
30-
computing_id: str
31-
):
57+
async def get_nominee_info(request: Request, db_session: database.DBSession, computing_id: str):
3258
# Putting this one behind the admin wall since it has contact information
3359
await admin_or_raise(request, db_session, AdminTypeEnum.Election)
3460
nominee_info = await nominees.crud.get_nominee_info(db_session, computing_id)
3561
if nominee_info is None:
36-
raise HTTPException(
37-
status_code=status.HTTP_404_NOT_FOUND,
38-
detail="nominee doesn't exist"
39-
)
62+
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="nominee doesn't exist")
4063

4164
return JSONResponse(nominee_info.serialize())
4265

66+
4367
@router.patch(
4468
"/{computing_id:str}",
4569
description="Will create or update nominee info. Returns an updated copy of their nominee info.",
4670
response_model=NomineeInfoModel,
47-
responses={
48-
500: { "description": "Failed to retrieve updated nominee." }
49-
},
50-
operation_id="update_nominee"
71+
responses={500: {"description": "Failed to retrieve updated nominee."}},
72+
operation_id="update_nominee",
5173
)
5274
async def provide_nominee_info(
53-
request: Request,
54-
db_session: database.DBSession,
55-
body: NomineeInfoUpdateParams,
56-
computing_id: str
75+
request: Request, db_session: database.DBSession, body: NomineeInfoUpdateParams, computing_id: str
5776
):
5877
# TODO: There needs to be a lot more validation here.
5978
await admin_or_raise(request, db_session, AdminTypeEnum.Election)
@@ -63,14 +82,15 @@ async def provide_nominee_info(
6382
if body.full_name is not None:
6483
updated_data["full_name"] = body.full_name
6584
if body.linked_in is not None:
66-
updated_data["linked_in"] = body.linked_in
85+
updated_data["linked_in"] = body.linked_in
6786
if body.instagram is not None:
6887
updated_data["instagram"] = body.instagram
6988
if body.email is not None:
7089
updated_data["email"] = body.email
7190
if body.discord_username is not None:
7291
updated_data["discord_username"] = body.discord_username
7392

93+
# TODO: Look into using something built into SQLAlchemy/Pydantic for better entry updates
7494
existing_info = await nominees.crud.get_nominee_info(db_session, computing_id)
7595
# if not already existing, create it
7696
if not existing_info:
@@ -97,8 +117,5 @@ async def provide_nominee_info(
97117

98118
nominee_info = await nominees.crud.get_nominee_info(db_session, computing_id)
99119
if not nominee_info:
100-
raise HTTPException(
101-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
102-
detail="failed to get updated nominee"
103-
)
120+
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="failed to get updated nominee")
104121
return JSONResponse(nominee_info.serialize())

0 commit comments

Comments
 (0)