|
1 | 1 | from fastapi import APIRouter, Depends, HTTPException, status |
2 | 2 | from fastapi.responses import JSONResponse |
| 3 | +from sqlalchemy.exc import IntegrityError |
3 | 4 |
|
4 | 5 | import database |
5 | 6 | import nominees.crud |
@@ -77,12 +78,22 @@ async def get_nominee_info(db_session: database.DBSession, computing_id: str): |
77 | 78 | "/{computing_id}", |
78 | 79 | description="Delete a nominee", |
79 | 80 | response_model=SuccessResponse, |
| 81 | + responses={ |
| 82 | + 409: {"description": "Nominee is still referenced by election applications.", "model": DetailModel}, |
| 83 | + }, |
80 | 84 | operation_id="delete_nominee", |
81 | 85 | dependencies=[Depends(perm_election)], |
82 | 86 | ) |
83 | 87 | async def delete_nominee_info(db_session: database.DBSession, computing_id: str): |
84 | | - await nominees.crud.delete_nominee_info(db_session, computing_id) |
85 | | - await db_session.commit() |
| 88 | + try: |
| 89 | + await nominees.crud.delete_nominee_info(db_session, computing_id) |
| 90 | + await db_session.commit() |
| 91 | + except IntegrityError as err: |
| 92 | + await db_session.rollback() |
| 93 | + raise HTTPException( |
| 94 | + status_code=status.HTTP_409_CONFLICT, |
| 95 | + detail=f"{computing_id} is still referenced by election applications.", |
| 96 | + ) from err |
86 | 97 |
|
87 | 98 | return SuccessResponse(success=True) |
88 | 99 |
|
|
0 commit comments