|
19 | 19 | description="Create a new field that can be associated with events.", |
20 | 20 | responses={ |
21 | 21 | 201: {"description": "Field created successfully"}, |
22 | | - 400: {"description": "Validation error"}, |
| 22 | + 400: {"description": "Validation error or field already exists"}, |
23 | 23 | }, |
24 | 24 | ) |
25 | 25 | def create_field_route(field: FieldCreate, db: Session = Depends(get_db)): |
26 | | - return field_crud.create_field(db=db, field=field) |
| 26 | + try: |
| 27 | + return field_crud.create_field(db=db, field=field) |
| 28 | + except ValueError as e: |
| 29 | + raise HTTPException( |
| 30 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 31 | + detail={ |
| 32 | + "code": "duplicate_resource", |
| 33 | + "message": str(e), |
| 34 | + }, |
| 35 | + ) from e |
27 | 36 |
|
28 | 37 |
|
29 | 38 | @router.get( |
@@ -79,22 +88,31 @@ def get_field_route( |
79 | 88 | responses={ |
80 | 89 | 200: {"description": "Field updated successfully"}, |
81 | 90 | 404: {"description": "Field not found"}, |
82 | | - 400: {"description": "Validation error"}, |
| 91 | + 400: {"description": "Validation error or field already exists"}, |
83 | 92 | }, |
84 | 93 | ) |
85 | 94 | def update_field_route( |
86 | 95 | field_id: int, field: FieldCreate, db: Session = Depends(get_db) |
87 | 96 | ): |
88 | | - db_field = field_crud.update_field(db=db, field_id=field_id, field=field) |
89 | | - if db_field is None: |
| 97 | + try: |
| 98 | + db_field = field_crud.update_field(db=db, field_id=field_id, field=field) |
| 99 | + if db_field is None: |
| 100 | + raise HTTPException( |
| 101 | + status_code=status.HTTP_404_NOT_FOUND, |
| 102 | + detail={ |
| 103 | + "code": "resource_not_found", |
| 104 | + "message": f"Field with id {field_id} not found", |
| 105 | + }, |
| 106 | + ) |
| 107 | + return db_field |
| 108 | + except ValueError as e: |
90 | 109 | raise HTTPException( |
91 | | - status_code=status.HTTP_404_NOT_FOUND, |
| 110 | + status_code=status.HTTP_400_BAD_REQUEST, |
92 | 111 | detail={ |
93 | | - "code": "resource_not_found", |
94 | | - "message": f"Field with id {field_id} not found", |
| 112 | + "code": "duplicate_resource", |
| 113 | + "message": str(e), |
95 | 114 | }, |
96 | | - ) |
97 | | - return db_field |
| 115 | + ) from e |
98 | 116 |
|
99 | 117 |
|
100 | 118 | @router.delete( |
|
0 commit comments