Add orm models for Probes

This commit is contained in:
Colin Goutte 2021-09-29 11:45:23 +02:00
parent 6b4c8b69b8
commit 97c7ab970c
2 changed files with 37 additions and 11 deletions

View File

@ -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())

View File

@ -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