|
20 | 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
21 | 21 | # SOFTWARE. |
22 | 22 |
|
| 23 | +import uuid |
| 24 | + |
23 | 25 | from fastapi import APIRouter, Depends, status, HTTPException |
24 | 26 | from fastapi.responses import Response |
25 | 27 | from sqlalchemy.exc import SQLAlchemyError |
@@ -93,12 +95,13 @@ async def create_resource(data: ResourceInputSchema, |
93 | 95 | response: Response, |
94 | 96 | session: Session = Depends(get_db)): |
95 | 97 | try: |
96 | | - resource = Resource(name=data.name, owner_uid=data.owner_uid) |
| 98 | + resource = Resource(uid=str(uuid.uuid4()), name=data.name, owner_uid=data.owner_uid) |
97 | 99 | session.add(resource) |
98 | 100 | session.commit() |
99 | 101 | response.status_code = 201 |
100 | 102 | return resource |
101 | 103 | except SQLAlchemyError as e: |
| 104 | + session.rollback() |
102 | 105 | raise HTTPException(status_code=500, detail="A database error occurred") from e |
103 | 106 |
|
104 | 107 |
|
@@ -131,6 +134,7 @@ async def modify_resource(uid: str, |
131 | 134 | session.commit() |
132 | 135 | return resource |
133 | 136 | except SQLAlchemyError as e: |
| 137 | + session.rollback() |
134 | 138 | raise HTTPException(status_code=500, detail="A database error occurred") from e |
135 | 139 |
|
136 | 140 |
|
@@ -161,6 +165,7 @@ async def remove_resource(uid: str, |
161 | 165 | response.status_code = 204 |
162 | 166 | return {} |
163 | 167 | except SQLAlchemyError as e: |
| 168 | + session.rollback() |
164 | 169 | raise HTTPException(status_code=500, detail="A database error occurred") from e |
165 | 170 |
|
166 | 171 |
|
@@ -216,12 +221,13 @@ async def create_owner(data: OwnerInputSchema, |
216 | 221 | response: Response, |
217 | 222 | session: Session = Depends(get_db)): |
218 | 223 | try: |
219 | | - owner = Owner(name=data.name, client_id='TODO') |
| 224 | + owner = Owner(uid=str(uuid.uuid4()), name=data.name) |
220 | 225 | session.add(owner) |
221 | 226 | session.commit() |
222 | 227 | response.status_code = 201 |
223 | 228 | return owner |
224 | 229 | except SQLAlchemyError as e: |
| 230 | + session.rollback() |
225 | 231 | # Handle the error appropriately, maybe raise an HTTPException |
226 | 232 | raise HTTPException(status_code=500, detail="A database error occurred") from e |
227 | 233 |
|
@@ -255,6 +261,7 @@ async def modify_owner(uid: str, |
255 | 261 | session.commit() |
256 | 262 | return owner |
257 | 263 | except SQLAlchemyError as e: |
| 264 | + session.rollback() |
258 | 265 | raise HTTPException(status_code=500, detail="A database error occurred") from e |
259 | 266 |
|
260 | 267 |
|
@@ -284,4 +291,5 @@ async def remove_owner(uid: str, |
284 | 291 | session.commit() |
285 | 292 | response.status_code = status.HTTP_204_NO_CONTENT |
286 | 293 | except SQLAlchemyError as e: |
| 294 | + session.rollback() |
287 | 295 | raise HTTPException(status_code=500, detail="A database error occurred") from e |
0 commit comments