Skip to content

Commit

Permalink
create device plant endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
violetaperezandrade committed May 31, 2024
1 parent 67b4990 commit 7684bf4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
13 changes: 6 additions & 7 deletions app/controller/measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@ async def handle_get_plant_last_measurement(self,
id_plant, token)
return measurement

async def handle_create_device_plant_relation(self, device_plant: dict):
plant_id = device_plant["id_plant"]
async def handle_create_device_plant_relation(self, device_plant: dict, token: str):
plant_id = device_plant.get("id_plant")
plant = await self.plants_service.get_plant(plant_id)
device_plant = self.measurements_service.create_device_plant_relation(
device_plant = await self.measurements_service.create_device_plant_relation(
plant,
device_plant)
device_plant, token)
if not device_plant:
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
status_code=status.HTTP_404_NOT_FOUND,
content={
"plant_id": (
"Could not found any plant "
f"with id {device_plant.get('id_plant')}"
"Could not find any plant"
)
},
)
Expand Down
6 changes: 4 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ async def get_plant_measurements(id_plant: int, token: str = Depends(get_access_


@app.post("/measurements/device-plant", response_model=DevicePlantSchema)
async def create_device_plant_relation(device_plant: DevicePlantCreateSchema):
return await controller.handle_create_device_plant_relation(device_plant.dict())
async def create_device_plant_relation(device_plant: DevicePlantCreateSchema,
token: str = Depends(get_access_token)):
return await controller.handle_create_device_plant_relation(device_plant.dict(),
token)


@app.patch("/measurements/device-plant/{id_device}", response_model=DevicePlantSchema)
Expand Down
13 changes: 9 additions & 4 deletions app/service/measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from schemas.measurement import MeasurementSavedSchema
from resources.parser import apply_rules
from external.Users import UsersService
from external.Plants import PlantsService


class MeasurementsService:
Expand All @@ -28,9 +29,13 @@ async def get_plant_last_measurement(self, id_plant, token):
)
return last_measurement

def create_device_plant_relation(self, plant, device_plant):
async def create_device_plant_relation(self, plant, device_plant, token):
if not plant:
return None
user_id = await UsersService.get_user_id(token)
plant_owner = await self.__get_plant_owner(plant.id)
if plant_owner != user_id:
raise UserUnauthorized
try:
device_plant = self.measurements_repository.create_device_plant_relation(
plant, device_plant
Expand Down Expand Up @@ -79,6 +84,6 @@ def delete_device_plant_relation(self, 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
async def __get_plant_owner(self, plant_id: int) -> int:
plant = await PlantsService.get_plant(plant_id)
return plant.id_user if plant else None

0 comments on commit 7684bf4

Please sign in to comment.