Skip to content

Commit cec5125

Browse files
committed
Add DELETE by id_plant
1 parent fa873a9 commit cec5125

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

app/controller/device_plant_controller.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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
34
from database.models.device_plant import DevicePlant
45
from schemas.device_plant import (
56
DevicePlantPartialUpdateSchema,
@@ -9,6 +10,7 @@
910
import logging
1011
from psycopg2.errors import UniqueViolation
1112
from sqlalchemy.exc import PendingRollbackError, IntegrityError, NoResultFound
13+
from fastapi.responses import JSONResponse
1214

1315
logger = logging.getLogger("app")
1416
logger.setLevel("DEBUG")
@@ -85,12 +87,22 @@ def get_all_device_plant_relations(req: Request, limit: int):
8587

8688

8789
@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):
9192
raise HTTPException(
9293
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."
9495
)
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"

app/database/database.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from sqlalchemy import create_engine, select, delete, engine
1+
from sqlalchemy import create_engine, select, delete, engine, column
22
from sqlalchemy.orm import Session
33
from dotenv import load_dotenv
44
from os import environ
@@ -84,15 +84,8 @@ def get_last_measurement(self, id_plant: int) -> Measurement:
8484

8585
return result
8686

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)
87+
def delete_by_field(self, field: str, value: str) -> int:
88+
query = delete(DevicePlant).where(column(field) == value)
9689
result = self.session.execute(query)
9790
self.session.commit()
9891
return result.rowcount

app/router/device_plant_router.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from typing import List
1+
from typing import List, Optional
22

3-
from fastapi import APIRouter, Body, Request, status, Query
3+
from fastapi import APIRouter, Body, Request, status, Query,Response
44
from schemas.device_plant import (
55
DevicePlantPartialUpdateSchema,
66
DevicePlantSchema,
@@ -58,8 +58,8 @@ async def get_device_plant(req: Request,
5858

5959

6060
@device_plant.delete(
61-
"/{id_device}",
61+
"/",
6262
status_code=status.HTTP_200_OK
6363
)
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)
64+
async def delete_device_plant_relation(req: Request, response: Response, id_device: Optional[str] = None, plant_id: Optional[str] = None):
65+
return controller.delete_device_plant_relation(req, response, id_device, plant_id)

0 commit comments

Comments
 (0)