|
1 |
| -from typing import Union |
2 |
| -from fastapi import Request, status, HTTPException |
| 1 | +from fastapi.responses import JSONResponse |
| 2 | +from typing import Optional, Union |
| 3 | +from fastapi import Request, status, HTTPException, Response |
3 | 4 | from database.models.device_plant import DevicePlant
|
4 | 5 | from schemas.device_plant import (
|
5 | 6 | DevicePlantPartialUpdateSchema,
|
|
9 | 10 | import logging
|
10 | 11 | from psycopg2.errors import UniqueViolation
|
11 | 12 | from sqlalchemy.exc import PendingRollbackError, IntegrityError, NoResultFound
|
| 13 | +from fastapi.responses import JSONResponse |
12 | 14 |
|
13 | 15 | logger = logging.getLogger("app")
|
14 | 16 | logger.setLevel("DEBUG")
|
@@ -85,12 +87,22 @@ def get_all_device_plant_relations(req: Request, limit: int):
|
85 | 87 |
|
86 | 88 |
|
87 | 89 | @withSQLExceptionsHandle
|
88 |
| -def delete_device_plant_relation_by_id_device(req: Request, id_device: str): |
89 |
| - result_rowcount = req.app.database.delete_by_device_id(id_device) |
90 |
| - if result_rowcount == 0: |
| 90 | +def delete_device_plant_relation(req: Request, response: Response, id_device: Optional[str] = None, plant_id: Optional[str] = None): |
| 91 | + if (id_device is None and plant_id is None) or (id_device and plant_id): |
91 | 92 | raise HTTPException(
|
92 | 93 | status_code=status.HTTP_400_BAD_REQUEST,
|
93 |
| - detail="No device-plant relation found with the given device id" |
| 94 | + detail="No identifier was provided, or both identifiers were provided." |
94 | 95 | )
|
95 |
| - |
96 |
| - return {"message": "Device-plant relation deleted successfully"} |
| 96 | + |
| 97 | + result_rowcount = 0 |
| 98 | + if id_device is not None: |
| 99 | + result_rowcount = req.app.database.delete_by_field("id_device", id_device) |
| 100 | + else: |
| 101 | + result_rowcount = req.app.database.delete_by_field("id_plant", plant_id) |
| 102 | + |
| 103 | + |
| 104 | + if result_rowcount == 0: |
| 105 | + response.status_code = status.HTTP_204_NO_CONTENT |
| 106 | + return # EMPTY RESPONSE! RESOURCE DID NOT EXIST |
| 107 | + else: |
| 108 | + return "Device-plant relation deleted successfully" |
0 commit comments