diff --git a/.env.dist b/.env.dist index 760d16c..206e655 100644 --- a/.env.dist +++ b/.env.dist @@ -7,4 +7,4 @@ QUEUE_NAME= LOGGING_LEVEL= DATABASE_URL= RABBITMQ_HOST= -RABBITMQ_PORT= \ No newline at end of file +RABBITMQ_PORT= diff --git a/app/controller/device_plant_controller.py b/app/controller/device_plant_controller.py index 2a3444d..5d51a81 100644 --- a/app/controller/device_plant_controller.py +++ b/app/controller/device_plant_controller.py @@ -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) @@ -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 @@ -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) diff --git a/app/database/database.py b/app/database/database.py index c5e348c..f53e620 100644 --- a/app/database/database.py +++ b/app/database/database.py @@ -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"], @@ -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], diff --git a/app/router/device_plant_router.py b/app/router/device_plant_router.py index 8ce0c1b..35d8022 100644 --- a/app/router/device_plant_router.py +++ b/app/router/device_plant_router.py @@ -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, @@ -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)] diff --git a/app/service/rabbitmq/consumer.py b/app/service/rabbitmq/consumer.py index 29e88cf..9be17ce 100644 --- a/app/service/rabbitmq/consumer.py +++ b/app/service/rabbitmq/consumer.py @@ -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 @@ -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))