diff --git a/papi/main.py b/papi/main.py index 4e17ee9..0204f79 100644 --- a/papi/main.py +++ b/papi/main.py @@ -6,7 +6,8 @@ from fastapi import FastAPI, Request, Body from collections import defaultdict from . import utils -from papi.sqlapp.database import Base + +from papi.sqlapp.database import Base, SessionLocal, engine app = FastAPI() diff --git a/papi/sqlapp/crud.py b/papi/sqlapp/crud.py index e69de29..baebabb 100644 --- a/papi/sqlapp/crud.py +++ b/papi/sqlapp/crud.py @@ -0,0 +1,29 @@ +from sqlalchemy.orm import Session + +from . import models + + +def get_sonde(db: Session, identifiant: str): + return ( + db.query(models.Sonde).filter(models.Sonde.identifiant == identifiant).first() + ) + + +def create_sonde(db: Session, identifiant: str, nom: str): + db_sonde = models.Sonde(identifiant=identifiant, nom=nom) + db.add(db_sonde) + db.commit() + db.refresh(db_sonde) + return db_sonde + + +def create_mesure(db: Session, sonde_id: int, content: dict): + db_mesure = models.Mesure(sonde_id=sonde_id, content=content) + db.add(db_mesure) + db.commit() + db.refesh(db_mesure) + return db_mesure + + +def get_mesure(db: Session, sonde_id: int): + return db.query(models.Mesure).filter(models.Mesure.sonde_id == sonde_id).all() diff --git a/papi/sqlapp/models.py b/papi/sqlapp/models.py index 691e422..6bde411 100644 --- a/papi/sqlapp/models.py +++ b/papi/sqlapp/models.py @@ -6,14 +6,14 @@ from .database import Base class Sonde(Base): __tablename__ = "sonde" - id = Column(Integer, primary_key=True, index=True) + sonde_id = Column(Integer, primary_key=True, index=True) nom = Column(String, index=True) identifiant = Column(String, index=True, unique=True, nullable=False) - # mesures = relationship("Mesure") + mesures = relationship("Mesure") class Mesure(Base): __tablename__ == "mesure" - id = Column(Integer, primary_key=True, index=True) - # sonde_id = Column(Integer, Foreignkey("sonde.id") + mesure_id = Column(Integer, primary_key=True, index=True) + sonde_id = Column(Integer, Foreignkey("sonde.id")) content = Column(JSON, nullable=False)