Skip to content

Commit 93b1082

Browse files
committed
endpoint get device-plant
1 parent df4c674 commit 93b1082

File tree

4 files changed

+54
-24
lines changed

4 files changed

+54
-24
lines changed

app/controller/device_plant_controller.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010
from psycopg2.errors import UniqueViolation
1111
from sqlalchemy.exc import PendingRollbackError, IntegrityError, NoResultFound
1212

13-
1413
logger = logging.getLogger("app")
1514
logger.setLevel("DEBUG")
1615

1716

1817
def withSQLExceptionsHandle(func):
19-
2018
def handleSQLException(*args, **kwargs):
2119
try:
2220
return func(*args, **kwargs)
@@ -51,8 +49,8 @@ def handleSQLException(*args, **kwargs):
5149
@withSQLExceptionsHandle
5250
def create_device_plant_relation(req: Request, device_plant: DevicePlantSchema):
5351
try:
54-
req.app.database.add_new(DevicePlant.from_pydantic(device_plant))
55-
return req.app.database.find_device_plant(device_plant.id_device)
52+
req.app.database.add(DevicePlant.from_pydantic(device_plant))
53+
return req.app.database.find_by_device_id(device_plant.id_device)
5654
except Exception as err:
5755
req.app.database.rollback()
5856
raise err
@@ -70,7 +68,17 @@ def update_device_plant(req: Request,
7068
device_plant_update_set.plant_type,
7169
device_plant_update_set.id_user,
7270
)
73-
return req.app.database.find_device_plant(id_device)
71+
return req.app.database.find_by_device_id(id_device)
7472
except Exception as err:
7573
req.app.database.rollback()
7674
raise err
75+
76+
77+
@withSQLExceptionsHandle
78+
def get_device_plant_relation(req: Request, id_plant: str):
79+
return req.app.database.find_by_plant_id(id_plant)
80+
81+
82+
@withSQLExceptionsHandle
83+
def get_all_device_plant_relations(req: Request):
84+
return req.app.database.find_all()

app/database/database.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from database.models.device_plant import DevicePlant
88
from database.models.measurement import Measurement
9+
from typing import List
910

1011
load_dotenv()
1112

@@ -39,15 +40,25 @@ def clean_table(self, table: Union[DevicePlant, Measurement]):
3940
self.session.execute(query)
4041
self.session.commit()
4142

42-
def add_new(self, record: Union[DevicePlant, Measurement]):
43+
def add(self, record: Union[DevicePlant, Measurement]):
4344
self.session.add(record)
4445
self.session.commit()
4546

46-
def find_device_plant(self, id_device: str) -> DevicePlant:
47+
def find_by_device_id(self, id_device: str) -> DevicePlant:
4748
query = select(DevicePlant).where(DevicePlant.id_device == id_device)
4849
result = self.session.scalars(query).one()
4950
return result
5051

52+
def find_by_plant_id(self, id_plant: str) -> DevicePlant:
53+
query = select(DevicePlant).where(DevicePlant.id_plant == id_plant)
54+
result = self.session.scalars(query).one()
55+
return result
56+
57+
def find_all(self) -> List[DevicePlant]:
58+
query = select(DevicePlant).limit(10)
59+
result = self.session.scalars(query)
60+
return result
61+
5162
def update_device_plant(self,
5263
id_device: str,
5364
id_plant: Optional[int],

app/router/device_plant_router.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from fastapi import APIRouter, Body, Request, status
1+
from typing import List
2+
3+
from fastapi import APIRouter, Body, Request, status, Query
24
from schemas.device_plant import (
35
DevicePlantPartialUpdateSchema,
46
DevicePlantSchema,
@@ -10,35 +12,44 @@
1012

1113

1214
@device_plant.post(
13-
"",
14-
status_code=status.HTTP_201_CREATED,
15-
response_model=DevicePlantSchema
16-
)
15+
"",
16+
status_code=status.HTTP_201_CREATED,
17+
response_model=DevicePlantSchema
18+
)
1719
async def create_device_plant_relation(req: Request,
1820
device_plant: DevicePlantSchema = Body(...)):
1921
return controller.create_device_plant_relation(req, device_plant)
2022

2123

2224
@device_plant.patch(
23-
"/{id_device}",
24-
status_code=status.HTTP_200_OK,
25-
response_model=DevicePlantSchema
26-
)
25+
"/{id_device}",
26+
status_code=status.HTTP_200_OK,
27+
response_model=DevicePlantSchema
28+
)
2729
async def update_fields_in_device_plant(id_device: str,
2830
req: Request,
2931
device_plant_update_set:
3032
DevicePlantPartialUpdateSchema = Body(...)):
31-
3233
return controller.update_device_plant(req, id_device, device_plant_update_set)
3334

3435

3536
@device_plant.put(
36-
"/{id_device}",
37-
status_code=status.HTTP_200_OK,
38-
response_model=DevicePlantSchema
39-
)
37+
"/{id_device}",
38+
status_code=status.HTTP_200_OK,
39+
response_model=DevicePlantSchema
40+
)
4041
async def update_all_in_device_plant(id_device: str,
4142
req: Request,
4243
device_plant: DevicePlantUpdateSchema = Body(...)):
43-
4444
return controller.update_device_plant(req, id_device, device_plant)
45+
46+
47+
@device_plant.get(
48+
"",
49+
status_code=status.HTTP_200_OK,
50+
response_model=List[DevicePlantSchema]
51+
)
52+
async def get_device_plant(req: Request, id_plant: str = Query(None)):
53+
if id_plant is None:
54+
return controller.get_all_device_plant_relations(req)
55+
return [controller.get_device_plant_relation(req, id_plant)]

app/service/rabbitmq/consumer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def run(self):
4141

4242
def obtain_device_plant(self, measurement_from_rabbit):
4343
try:
44-
dp = self.__sqlAlchemyClient.find_device_plant(
44+
dp = self.__sqlAlchemyClient.find_by_device_id(
4545
measurement_from_rabbit.id_device)
4646
logger.info(LoggerMessages.ROW_FOUND.format("DEVICE_PLANT", dp))
4747
return dp
@@ -77,7 +77,7 @@ def save_measurement(self, measurement_from_rabbit, device_plant):
7777
watering=measurement_from_rabbit.watering
7878
)
7979
try:
80-
self.__sqlAlchemyClient.add_new(measurement_from_db)
80+
self.__sqlAlchemyClient.add(measurement_from_db)
8181

8282
logger.info(LoggerMessages.NEW_ROW_INSERTED.format(
8383
"MEASUREMENT", measurement_from_db))

0 commit comments

Comments
 (0)