diff --git a/src/nominees/crud.py b/src/nominees/crud.py index 372ed91..5861f01 100644 --- a/src/nominees/crud.py +++ b/src/nominees/crud.py @@ -1,9 +1,20 @@ +from collections.abc import Sequence + import sqlalchemy from sqlalchemy.ext.asyncio import AsyncSession from nominees.tables import NomineeInfo +async def get_all_nominees( + db_session: AsyncSession, +) -> Sequence[NomineeInfo]: + nominees = (await db_session.scalars( + sqlalchemy + .select(NomineeInfo) + )).all() + return nominees + async def get_nominee_info( db_session: AsyncSession, computing_id: str, @@ -30,3 +41,13 @@ async def update_nominee_info( .where(NomineeInfo.computing_id == info.computing_id) .values(info.to_update_dict()) ) + +async def delete_nominee_info( + db_session: AsyncSession, + computing_id: str, +): + await db_session.execute( + sqlalchemy + .delete(NomineeInfo) + .where(NomineeInfo.computing_id == computing_id) + ) diff --git a/src/nominees/urls.py b/src/nominees/urls.py index 15d8136..dfe2af4 100644 --- a/src/nominees/urls.py +++ b/src/nominees/urls.py @@ -8,6 +8,7 @@ NomineeInfoUpdateParams, ) from nominees.tables import NomineeInfo +from utils.shared_models import DetailModel from utils.urls import admin_or_raise router = APIRouter( @@ -15,6 +16,60 @@ tags=["nominee"], ) +@router.get( + "", + description="Get all nominees", + response_model=list[NomineeInfoModel], + responses={ + 403: { "description": "need to be an admin", "model": DetailModel } + }, + operation_id="get_all_nominees" +) +async def get_all_nominees( + request: Request, + db_session: database.DBSession, +): + # Putting this behind a wall since there is private information here + await admin_or_raise(request, db_session) + nominees_list = await nominees.crud.get_all_nominees(db_session) + + return JSONResponse([ + item.serialize() for item in nominees_list + ]) + +@router.post( + "", + description="Nominee info is always publically tied to election, so be careful!", + response_model=NomineeInfoModel, + responses={ + 500: { "description": "failed to fetch new nominee", "model": DetailModel } + }, + operation_id="create_nominee" +) +async def create_nominee( + request: Request, + db_session: database.DBSession, + body: NomineeInfoModel +): + await admin_or_raise(request, db_session) + await nominees.crud.create_nominee_info(db_session, NomineeInfo( + computing_id=body.computing_id, + full_name=body.full_name, + linked_in=body.linked_in, + instagram=body.instagram, + email=body.email, + discord_username=body.discord_username, + )) + + nominee_info = await nominees.crud.get_nominee_info(db_session, body.computing_id) + if nominee_info is None: + raise HTTPException( + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + detail="couldn't fetch newly created nominee" + ) + + return JSONResponse(nominee_info) + @router.get( "/{computing_id:str}", description="Nominee info is always publically tied to election, so be careful!", @@ -40,7 +95,21 @@ async def get_nominee_info( return JSONResponse(nominee_info.serialize()) -@router.patch( +@router.delete( + "/{computing_id:str}", + description="Delete a nominee", + operation_id="delete_nominee" +) +async def delete_nominee_info( + request: Request, + db_session: database.DBSession, + computing_id: str +): + await admin_or_raise(request, db_session) + await nominees.crud.delete_nominee_info(db_session, computing_id) + await db_session.commit() + +@router.put( "/{computing_id:str}", description="Will create or update nominee info. Returns an updated copy of their nominee info.", response_model=NomineeInfoModel, @@ -71,6 +140,7 @@ async def provide_nominee_info( if body.discord_username is not None: updated_data["discord_username"] = body.discord_username + # TODO: Look into using something built into SQLAlchemy/Pydantic for better entry updates existing_info = await nominees.crud.get_nominee_info(db_session, computing_id) # if not already existing, create it if not existing_info: diff --git a/src/registrations/crud.py b/src/registrations/crud.py index 6d135b5..0a6d482 100644 --- a/src/registrations/crud.py +++ b/src/registrations/crud.py @@ -7,6 +7,15 @@ from registrations.tables import NomineeApplication +async def get_all_registrations( + db_session: AsyncSession +) -> Sequence[NomineeApplication]: + registrations = (await db_session.scalars( + sqlalchemy + .select(NomineeApplication) + )).all() + return registrations + async def get_all_registrations_of_user( db_session: AsyncSession, computing_id: str, diff --git a/src/registrations/urls.py b/src/registrations/urls.py index 1fe552f..7bbcaeb 100644 --- a/src/registrations/urls.py +++ b/src/registrations/urls.py @@ -25,6 +25,17 @@ tags=["registration"], ) +@router.get( + "", + description="get all the registrations", + response_model=list[NomineeApplicationModel], + operation_id="get_registrations" +) +async def get_all_registrations( + db_session: database.DBSession, +): + return await registrations.crud.get_all_registrations(db_session) + @router.get( "/{election_name:str}", description="get all the registrations of a single election",