Skip to content

Commit e887546

Browse files
authored
Merge pull request #12 from Hanagotchi/HAN-29
Han-29: [MEDICIONES] Obtener relación sensor/planta
2 parents 8023f54 + 4231e50 commit e887546

File tree

5 files changed

+65
-34
lines changed

5 files changed

+65
-34
lines changed

.env.dist

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
POSTGRES_USER=user
2-
POSTGRES_PASSWORD=1234
3-
POSTGRES_DB="measurements"
4-
POSTGRES_HOST=sql
5-
POSTGRES_PORT=5432
6-
QUEUE_NAME="measurements"
7-
LOGGING_LEVEL="DEBUG"
8-
DATABASE_URL=postgresql://user:1234@sql:5432/measurements
9-
RABBITMQ_HOST=rabbitmq
1+
POSTGRES_USER=
2+
POSTGRES_PASSWORD=
3+
POSTGRES_DB=
4+
POSTGRES_HOST=
5+
POSTGRES_PORT=
6+
QUEUE_NAME=
7+
LOGGING_LEVEL=
8+
DATABASE_URL=
9+
RABBITMQ_HOST=

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, limit: int):
84+
return req.app.database.find_all(limit)

app/database/database.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
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

1213

1314
class SQLAlchemyClient():
14-
1515
db_url = engine.URL.create(
1616
"postgresql",
1717
database=environ["POSTGRES_DB"],
@@ -39,15 +39,25 @@ def clean_table(self, table: Union[DevicePlant, Measurement]):
3939
self.session.execute(query)
4040
self.session.commit()
4141

42-
def add_new(self, record: Union[DevicePlant, Measurement]):
42+
def add(self, record: Union[DevicePlant, Measurement]):
4343
self.session.add(record)
4444
self.session.commit()
4545

46-
def find_device_plant(self, id_device: str) -> DevicePlant:
46+
def find_by_device_id(self, id_device: str) -> DevicePlant:
4747
query = select(DevicePlant).where(DevicePlant.id_device == id_device)
4848
result = self.session.scalars(query).one()
4949
return result
5050

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

app/router/device_plant_router.py

Lines changed: 28 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,46 @@
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,
53+
id_plant: str = Query(None),
54+
limit: int = Query(10)):
55+
if id_plant is None:
56+
return controller.get_all_device_plant_relations(req, limit)
57+
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)