diff --git a/papi/main.py b/papi/main.py index a1fc243..9c1360d 100644 --- a/papi/main.py +++ b/papi/main.py @@ -1,7 +1,8 @@ -from typing import Optional +from typing import Optional, List from pydantic import BaseModel -from fastapi import FastAPI, Request, Body +from fastapi import FastAPI, Request, Body, Depends +from sqlalchemy.orm import Session from collections import defaultdict @@ -9,6 +10,7 @@ from . import utils from papi.sqlapp.database import Base, SessionLocal, engine from papi.sqlapp import crud +from papi.sqlapp import schemas app = FastAPI() @@ -36,14 +38,9 @@ class Notifier: ) -class Sonde(BaseModel): - identifiant: str - nom: str - - Notifier = Notifier() -sondes = {"test": Sonde(identifiant="test", nom="Testlocal")} +sondes = {"test": schemas.SondeBase(identifiant="test", nom="Testlocal")} def default_sample(): @@ -56,9 +53,16 @@ def read_root(): return {"msg": "Hello World"} -@app.post("/sonde/") -def post_sonde(sonde: Sonde): - sondes[sonde.identifiant] = sonde +@app.post("/sonde/", response_model=List[schemas.SondeBase]) +def post_sonde(sonde: schemas.SondeBase, db: Session = Depends(get_db)): + db_sonde = crud.get_sonde(db, identifiant=sonde.identifiant) + if not db_sonde: + db_sonde = crud.create_sonde( + db, sonde.identifiant, sonde.nom or sonde.identifiant + ) + sondes[sonde.identifiant] = schemas.SondeBase.from_orm( + db_sonde + ) # Attention au typage ici return list(sondes.values()) diff --git a/papi/sqlapp/schemas.py b/papi/sqlapp/schemas.py index e69de29..e9ab104 100644 --- a/papi/sqlapp/schemas.py +++ b/papi/sqlapp/schemas.py @@ -0,0 +1,22 @@ +from typing import List, Optional + +from pydantic import BaseModel + + +class SondeBase(BaseModel): + identifiant: str + nom: str + + class Config: + orm_mode = True + + +class SondeCreate(SondeBase): + pass + + +class Sonde(SondeBase): + sonde_id: int + + class Config: + orm_mode = True