summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--api/routes/locations.py7
-rw-r--r--api/routes/measures.py6
-rw-r--r--api/routes/sensors.py27
-rw-r--r--api/routes/stations.py7
-rw-r--r--api/schemas/location.py6
-rw-r--r--api/schemas/measures.py5
-rw-r--r--api/schemas/sensor.py6
-rw-r--r--api/schemas/station.py6
8 files changed, 51 insertions, 19 deletions
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/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/routes/sensors.py b/api/routes/sensors.py
index 89744e3..9c86309 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 ..schemas.sensor import PostSensor
+from datetime import datetime
router = APIRouter(
prefix='/sensors',
@@ -26,3 +28,24 @@ async def get_sensors():
dc['size'] = len(dc['sensors'])
session.close()
return dc
+
+@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}"
+ )
+ 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()
+ session.close()
+ return {'msg': 'Sensor criado com sucesso.'}
+
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/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
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