Skip to content

Commit

Permalink
Merge branch 'main' into HAN-7
Browse files Browse the repository at this point in the history
  • Loading branch information
violetaperezandrade committed Jan 30, 2024
2 parents 8e207d8 + e887546 commit 08c1727
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ QUEUE_NAME=
LOGGING_LEVEL=
DATABASE_URL=
RABBITMQ_HOST=
RABBITMQ_PORT=
RABBITMQ_PORT=
18 changes: 13 additions & 5 deletions app/controller/device_plant_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
from psycopg2.errors import UniqueViolation
from sqlalchemy.exc import PendingRollbackError, IntegrityError, NoResultFound


logger = logging.getLogger("app")
logger.setLevel("DEBUG")


def withSQLExceptionsHandle(func):

def handleSQLException(*args, **kwargs):
try:
return func(*args, **kwargs)
Expand Down Expand Up @@ -51,8 +49,8 @@ def handleSQLException(*args, **kwargs):
@withSQLExceptionsHandle
def create_device_plant_relation(req: Request, device_plant: DevicePlantSchema):
try:
req.app.database.add_new(DevicePlant.from_pydantic(device_plant))
return req.app.database.find_device_plant(device_plant.id_device)
req.app.database.add(DevicePlant.from_pydantic(device_plant))
return req.app.database.find_by_device_id(device_plant.id_device)
except Exception as err:
req.app.database.rollback()
raise err
Expand All @@ -70,7 +68,17 @@ def update_device_plant(req: Request,
device_plant_update_set.plant_type,
device_plant_update_set.id_user,
)
return req.app.database.find_device_plant(id_device)
return req.app.database.find_by_device_id(id_device)
except Exception as err:
req.app.database.rollback()
raise err


@withSQLExceptionsHandle
def get_device_plant_relation(req: Request, id_plant: str):
return req.app.database.find_by_plant_id(id_plant)


@withSQLExceptionsHandle
def get_all_device_plant_relations(req: Request, limit: int):
return req.app.database.find_all(limit)
16 changes: 13 additions & 3 deletions app/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

from database.models.device_plant import DevicePlant
from database.models.measurement import Measurement
from typing import List

load_dotenv()


class SQLAlchemyClient():

db_url = engine.URL.create(
"postgresql",
database=environ["POSTGRES_DB"],
Expand Down Expand Up @@ -39,15 +39,25 @@ def clean_table(self, table: Union[DevicePlant, Measurement]):
self.session.execute(query)
self.session.commit()

def add_new(self, record: Union[DevicePlant, Measurement]):
def add(self, record: Union[DevicePlant, Measurement]):
self.session.add(record)
self.session.commit()

def find_device_plant(self, id_device: str) -> DevicePlant:
def find_by_device_id(self, id_device: str) -> DevicePlant:
query = select(DevicePlant).where(DevicePlant.id_device == id_device)
result = self.session.scalars(query).one()
return result

def find_by_plant_id(self, id_plant: str) -> DevicePlant:
query = select(DevicePlant).where(DevicePlant.id_plant == id_plant)
result = self.session.scalars(query).one()
return result

def find_all(self, limit: int) -> List[DevicePlant]:
query = select(DevicePlant).limit(limit)
result = self.session.scalars(query)
return result

def update_device_plant(self,
id_device: str,
id_plant: Optional[int],
Expand Down
43 changes: 28 additions & 15 deletions app/router/device_plant_router.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from fastapi import APIRouter, Body, Request, status
from typing import List

from fastapi import APIRouter, Body, Request, status, Query
from schemas.device_plant import (
DevicePlantPartialUpdateSchema,
DevicePlantSchema,
Expand All @@ -10,35 +12,46 @@


@device_plant.post(
"",
status_code=status.HTTP_201_CREATED,
response_model=DevicePlantSchema
)
"",
status_code=status.HTTP_201_CREATED,
response_model=DevicePlantSchema
)
async def create_device_plant_relation(req: Request,
device_plant: DevicePlantSchema = Body(...)):
return controller.create_device_plant_relation(req, device_plant)


@device_plant.patch(
"/{id_device}",
status_code=status.HTTP_200_OK,
response_model=DevicePlantSchema
)
"/{id_device}",
status_code=status.HTTP_200_OK,
response_model=DevicePlantSchema
)
async def update_fields_in_device_plant(id_device: str,
req: Request,
device_plant_update_set:
DevicePlantPartialUpdateSchema = Body(...)):

return controller.update_device_plant(req, id_device, device_plant_update_set)


@device_plant.put(
"/{id_device}",
status_code=status.HTTP_200_OK,
response_model=DevicePlantSchema
)
"/{id_device}",
status_code=status.HTTP_200_OK,
response_model=DevicePlantSchema
)
async def update_all_in_device_plant(id_device: str,
req: Request,
device_plant: DevicePlantUpdateSchema = Body(...)):

return controller.update_device_plant(req, id_device, device_plant)


@device_plant.get(
"",
status_code=status.HTTP_200_OK,
response_model=List[DevicePlantSchema]
)
async def get_device_plant(req: Request,
id_plant: str = Query(None),
limit: int = Query(10)):
if id_plant is None:
return controller.get_all_device_plant_relations(req, limit)
return [controller.get_device_plant_relation(req, id_plant)]
4 changes: 2 additions & 2 deletions app/service/rabbitmq/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def run(self):

def obtain_device_plant(self, measurement_from_rabbit):
try:
dp = self.__sqlAlchemyClient.find_device_plant(
dp = self.__sqlAlchemyClient.find_by_device_id(
measurement_from_rabbit.id_device)
logger.info(LoggerMessages.ROW_FOUND.format("DEVICE_PLANT", dp))
return dp
Expand Down Expand Up @@ -77,7 +77,7 @@ def save_measurement(self, measurement_from_rabbit, device_plant):
watering=measurement_from_rabbit.watering
)
try:
self.__sqlAlchemyClient.add_new(measurement_from_db)
self.__sqlAlchemyClient.add(measurement_from_db)

logger.info(LoggerMessages.NEW_ROW_INSERTED.format(
"MEASUREMENT", measurement_from_db))
Expand Down

0 comments on commit 08c1727

Please sign in to comment.