Skip to content

Commit fa873a9

Browse files
committed
Add DELETE endpoint for DevicePlant
1 parent 5009b06 commit fa873a9

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

app/controller/device_plant_controller.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,15 @@ def get_device_plant_relation(req: Request, id_plant: str):
8282
@withSQLExceptionsHandle
8383
def get_all_device_plant_relations(req: Request, limit: int):
8484
return req.app.database.find_all(limit)
85+
86+
87+
@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:
91+
raise HTTPException(
92+
status_code=status.HTTP_400_BAD_REQUEST,
93+
detail="No device-plant relation found with the given device id"
94+
)
95+
96+
return {"message": "Device-plant relation deleted successfully"}

app/database/database.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,16 @@ def get_last_measurement(self, id_plant: int) -> Measurement:
8383
result: Measurement = self.session.scalars(query).one()
8484

8585
return result
86+
87+
def delete_by_device_id(self, id_device: str) -> int:
88+
"""
89+
Delete a device-plant relation by device id
90+
Args:
91+
id_device (str): device id
92+
Returns:
93+
int: number of rows affected. 0 if no rows were affected
94+
"""
95+
query = delete(DevicePlant).where(DevicePlant.id_device == id_device)
96+
result = self.session.execute(query)
97+
self.session.commit()
98+
return result.rowcount

app/router/device_plant_router.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,11 @@ async def get_device_plant(req: Request,
5555
if id_plant is None:
5656
return controller.get_all_device_plant_relations(req, limit)
5757
return [controller.get_device_plant_relation(req, id_plant)]
58+
59+
60+
@device_plant.delete(
61+
"/{id_device}",
62+
status_code=status.HTTP_200_OK
63+
)
64+
async def delete_device_plant_relation_by_id_device(req: Request, id_device: str):
65+
return controller.delete_device_plant_relation_by_id_device(req, id_device)

0 commit comments

Comments
 (0)