Skip to content

Commit

Permalink
plant last measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
violetaperezandrade committed May 30, 2024
1 parent 9b0c607 commit 67b4990
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
8 changes: 5 additions & 3 deletions app/controller/measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ def __init__(self, measurements_service: MeasurementsService,
self.measurements_service = measurements_service
self.plants_service = plants_service

def handle_get_plant_last_measurement(self,
id_plant: int) -> MeasurementSavedSchema:
measurement = self.measurements_service.get_plant_last_measurement(id_plant)
async def handle_get_plant_last_measurement(self,
id_plant: int,
token: str) -> MeasurementSavedSchema:
measurement = await self.measurements_service.get_plant_last_measurement(
id_plant, token)
return measurement

async def handle_create_device_plant_relation(self, device_plant: dict):
Expand Down
10 changes: 3 additions & 7 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@


async def get_access_token(x_access_token: str = Header(...)):
print(f"Token: {x_access_token}")
return x_access_token
return x_access_token.split(" ")[1]


@app.get("/")
Expand All @@ -37,8 +36,8 @@ async def root():


@app.get("/measurements/{id_plant}/last", response_model=MeasurementSavedSchema)
async def get_plant_measurements(id_plant: int):
return controller.handle_get_plant_last_measurement(id_plant)
async def get_plant_measurements(id_plant: int, token: str = Depends(get_access_token)):
return await controller.handle_get_plant_last_measurement(id_plant, token)


@app.post("/measurements/device-plant", response_model=DevicePlantSchema)
Expand Down Expand Up @@ -66,9 +65,6 @@ async def get_device_plant(
query_params: DevicePlantQueryParams = Depends(DevicePlantQueryParams),
token: str = Depends(get_access_token)
):
print(f"Token: {token}")
token = token.split(" ")[1]
print(f"Token stripped: {token}")
return await controller.handle_get_device_plant(query_params.get_query_params(),
token)

Expand Down
11 changes: 9 additions & 2 deletions app/service/measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ class MeasurementsService:
def __init__(self, measurements_repository: MeasurementsRepository):
self.measurements_repository = measurements_repository

def get_plant_last_measurement(self, id_plant):
async def get_plant_last_measurement(self, id_plant, token):
user_id = await UsersService.get_user_id(token)
owner_id = self.__get_plant_owner(id_plant)
if owner_id != user_id:
raise UserUnauthorized
last_measurement = self.measurements_repository.get_plant_last_measurement(
id_plant)
if not last_measurement:
Expand Down Expand Up @@ -52,7 +56,6 @@ def update_device_plant(self, id_device, plant, plant_id):
async def get_device_plant(self, token: str, query_params: dict = None,
device_id: str = None):
user_id = await UsersService.get_user_id(token)
print(f"User performing request ID: {user_id}")
if device_id:
result = self.measurements_repository.find_by_device_id(device_id)
if user_id != result.get("id_user"):
Expand All @@ -75,3 +78,7 @@ def delete_device_plant_relation(self, type_id, id):
result_rowcount = self.measurements_repository.delete_by_field(type_id, id)

return result_rowcount if result_rowcount else None

def __get_plant_owner(self, plant_id: int) -> int:
plant = self.measurements_repository.find_by_plant_id(plant_id)
return plant.get("id_user") if plant else None

0 comments on commit 67b4990

Please sign in to comment.