From 45463fd2d300feb56dbb5db0645e18fcaa1cc71e Mon Sep 17 00:00:00 2001 From: Matheus Date: Mon, 3 Nov 2025 12:27:45 -0300 Subject: post sensor --- api/routes/sensors.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'api') diff --git a/api/routes/sensors.py b/api/routes/sensors.py index 89744e3..e9a2107 100644 --- a/api/routes/sensors.py +++ b/api/routes/sensors.py @@ -1,7 +1,9 @@ -from fastapi import APIRouter +from fastapi import APIRouter, HTTPException from sqlalchemy import select from ..database import SessionLocal -from ..models import SensorDevice +from ..models import SensorDevice, Station +from pydantic import BaseModel +from datetime import datetime router = APIRouter( prefix='/sensors', @@ -26,3 +28,26 @@ async def get_sensors(): dc['size'] = len(dc['sensors']) session.close() return dc + +class PostSensor(BaseModel): + name: str + installation_date: int + station_id: int + +@router.post('/') +async def post_sensor(sensor: PostSensor): + session = SessionLocal() + station = session.get(Station, sensor.station_id) + if not station: + session.close() + raise HTTPException( + status_code=404, + detail=f"Não existe estação com id {sensor.station_id}" + ) + installation_date = datetime.fromtimestamp(sensor.installation_date) + + session.add(SensorDevice(name=sensor.name, installation_date=installation_date, station_id=sensor.station_id, is_active=True)) + session.commit() + session.close() + return {'msg': 'Sensor criado com sucesso.'} + -- cgit v1.2.3 From e987f732a7ad2d85a5760061a20873b841c10fac Mon Sep 17 00:00:00 2001 From: Matheus Date: Mon, 3 Nov 2025 14:24:30 -0300 Subject: optional installation date --- api/routes/sensors.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'api') diff --git a/api/routes/sensors.py b/api/routes/sensors.py index e9a2107..d1c4403 100644 --- a/api/routes/sensors.py +++ b/api/routes/sensors.py @@ -31,7 +31,7 @@ async def get_sensors(): class PostSensor(BaseModel): name: str - installation_date: int + installation_date: int | None = None station_id: int @router.post('/') @@ -44,7 +44,10 @@ async def post_sensor(sensor: PostSensor): status_code=404, detail=f"Não existe estação com id {sensor.station_id}" ) - installation_date = datetime.fromtimestamp(sensor.installation_date) + if not sensor.installation_date: + installation_date = station.installation_date + else: + installation_date = datetime.fromtimestamp(sensor.installation_date) session.add(SensorDevice(name=sensor.name, installation_date=installation_date, station_id=sensor.station_id, is_active=True)) session.commit() -- cgit v1.2.3 From f392c3edaf52ccfa496d2e85f96e18ec81d11a65 Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Monteiro Date: Mon, 3 Nov 2025 23:39:34 -0300 Subject: refactor Post methods in schema files --- api/routes/locations.py | 7 +------ api/routes/sensors.py | 7 +------ api/routes/stations.py | 7 +------ api/schemas/station.py | 6 ++++++ 4 files changed, 9 insertions(+), 18 deletions(-) (limited to 'api') diff --git a/api/routes/locations.py b/api/routes/locations.py index 744fa7f..43578df 100644 --- a/api/routes/locations.py +++ b/api/routes/locations.py @@ -2,6 +2,7 @@ from fastapi import APIRouter from sqlalchemy import select from ..database import SessionLocal from ..models import Location +from ..schemas.location import PostLocation from pydantic import BaseModel @@ -28,12 +29,6 @@ async def get_locations(): session.close() return dc - -class PostLocation(BaseModel): - street: str - avenue: str - zip_code:str - @router.post('/') async def create_location(location: PostLocation): session = SessionLocal() diff --git a/api/routes/sensors.py b/api/routes/sensors.py index d1c4403..9c86309 100644 --- a/api/routes/sensors.py +++ b/api/routes/sensors.py @@ -2,7 +2,7 @@ from fastapi import APIRouter, HTTPException from sqlalchemy import select from ..database import SessionLocal from ..models import SensorDevice, Station -from pydantic import BaseModel +from ..schemas.sensor import PostSensor from datetime import datetime router = APIRouter( @@ -29,11 +29,6 @@ async def get_sensors(): session.close() return dc -class PostSensor(BaseModel): - name: str - installation_date: int | None = None - station_id: int - @router.post('/') async def post_sensor(sensor: PostSensor): session = SessionLocal() diff --git a/api/routes/stations.py b/api/routes/stations.py index 0a753fe..c7cdf7c 100644 --- a/api/routes/stations.py +++ b/api/routes/stations.py @@ -2,7 +2,7 @@ from fastapi import APIRouter, HTTPException from sqlalchemy import select from ..database import SessionLocal from ..models import Station, SensorDevice, Location -from pydantic import BaseModel +from ..schemas.station import PostStation from datetime import datetime router = APIRouter( @@ -35,11 +35,6 @@ async def get_stations(): session.close() return dc -class PostStation(BaseModel): - name: str - installation_date: int - location_id: int - @router.post('/') async def post_station(station: PostStation): session = SessionLocal() diff --git a/api/schemas/station.py b/api/schemas/station.py index e69de29..3e8dc10 100644 --- a/api/schemas/station.py +++ b/api/schemas/station.py @@ -0,0 +1,6 @@ +from pydantic import BaseModel + +class PostStation(BaseModel): + name: str + installation_date: int + location_id: int \ No newline at end of file -- cgit v1.2.3 From cc1b5e2f48ef62670be05ccfc6346b16b61844c8 Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Monteiro Date: Mon, 3 Nov 2025 23:40:37 -0300 Subject: refactor Post methods in schema files --- api/routes/measures.py | 6 +----- api/schemas/location.py | 6 ++++++ api/schemas/measures.py | 5 +++++ api/schemas/sensor.py | 6 ++++++ 4 files changed, 18 insertions(+), 5 deletions(-) (limited to 'api') diff --git a/api/routes/measures.py b/api/routes/measures.py index b435ae3..7aea38e 100644 --- a/api/routes/measures.py +++ b/api/routes/measures.py @@ -2,7 +2,7 @@ from fastapi import APIRouter from sqlalchemy import select from ..database import SessionLocal from ..models import Measure -from pydantic import BaseModel +from ..schemas.measures import PostMeasure router = APIRouter( prefix='/measures', @@ -26,10 +26,6 @@ async def get_measures(): session.close() return dc -class PostMeasure(BaseModel): - name: str - unit_code: str - @router.post('/') async def post_measure(measure: PostMeasure): session = SessionLocal() diff --git a/api/schemas/location.py b/api/schemas/location.py index e69de29..102e3d9 100644 --- a/api/schemas/location.py +++ b/api/schemas/location.py @@ -0,0 +1,6 @@ +from pydantic import BaseModel + +class PostLocation(BaseModel): + street: str + avenue: str + zip_code:str \ No newline at end of file diff --git a/api/schemas/measures.py b/api/schemas/measures.py index e69de29..999a82c 100644 --- a/api/schemas/measures.py +++ b/api/schemas/measures.py @@ -0,0 +1,5 @@ +from pydantic import BaseModel + +class PostMeasure(BaseModel): + name: str + unit_code: str \ No newline at end of file diff --git a/api/schemas/sensor.py b/api/schemas/sensor.py index e69de29..e038378 100644 --- a/api/schemas/sensor.py +++ b/api/schemas/sensor.py @@ -0,0 +1,6 @@ +from pydantic import BaseModel + +class PostSensor(BaseModel): + name: str + installation_date: int | None = None + station_id: int \ No newline at end of file -- cgit v1.2.3