diff options
| author | Matheus <matheus.guedes.mg.m@gmail.com> | 2025-11-03 10:03:29 -0300 |
|---|---|---|
| committer | Matheus <matheus.guedes.mg.m@gmail.com> | 2025-11-03 10:03:29 -0300 |
| commit | 46d11f5ee2cad79aeda5c06c593d24e74cb97a43 (patch) | |
| tree | b3461b76b298c9f1087a52558ae0fa0012199a87 /api/routes/stations.py | |
| parent | 1557fa9e537b4b6c5f1f188983ad5ef67f73067d (diff) | |
post station
Diffstat (limited to 'api/routes/stations.py')
| -rw-r--r-- | api/routes/stations.py | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/api/routes/stations.py b/api/routes/stations.py index 00a5fa8..0a753fe 100644 --- a/api/routes/stations.py +++ b/api/routes/stations.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 Station +from ..models import Station, SensorDevice, Location +from pydantic import BaseModel +from datetime import datetime router = APIRouter( prefix='/stations', @@ -32,3 +34,24 @@ async def get_stations(): dc['size'] = len(dc['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() + session.begin() + location = session.get(Location, station.location_id) + if not location: + session.close() + raise HTTPException( + status_code=404, + detail=f"Location {station.location_id} não encontrada." + ) + session.add(Station(name=station.name, installation_date=datetime.fromtimestamp(station.installation_date), location_id=station.location_id, is_active=True)) + session.commit() + session.close() + return {'msg': 'Estação adicionada com sucesso.'} |
