diff options
| author | Matheus <matheus.guedes.mg.m@gmail.com> | 2025-11-03 12:27:45 -0300 |
|---|---|---|
| committer | Matheus <matheus.guedes.mg.m@gmail.com> | 2025-11-03 12:28:09 -0300 |
| commit | 45463fd2d300feb56dbb5db0645e18fcaa1cc71e (patch) | |
| tree | 76c2e6a61c4be77714b74a09250152db7d0abec9 /api/routes | |
| parent | 46d11f5ee2cad79aeda5c06c593d24e74cb97a43 (diff) | |
post sensor
Diffstat (limited to 'api/routes')
| -rw-r--r-- | api/routes/sensors.py | 29 |
1 files changed, 27 insertions, 2 deletions
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.'} + |
