PIbd-42_SSPR/db/repositories/ch_experimentdb_experiment_data_repos.py

46 lines
1.5 KiB
Python

from typing import Optional
from sqlalchemy import select
from db.db_connection import async_session
from db.models.ch_experimentdb_experiment_data_model import ChExperimentDBExperimentData
class ChExperimentDBExperimentDataRepository:
@staticmethod
async def get_all() -> Optional[list[ChExperimentDBExperimentData]]:
async with async_session() as session:
result = await session.execute(select(ChExperimentDBExperimentData))
return result.scalars().all()
@staticmethod
async def get_by_id(id_: int) -> Optional[ChExperimentDBExperimentData]:
async with async_session() as session:
result = session.get(ChExperimentDBExperimentData, id_)
return result
@staticmethod
async def create(volume: float,
nitrogen_oxide_emission: float,
temperature: float,
co_fraction: float,
co2_fraction: float,
x: float,
y: float,
z: float,
file_id: str):
new_data = ChExperimentDBExperimentData(
volume=volume,
nitrogen_oxide_emission=nitrogen_oxide_emission,
temperature=temperature,
co_fraction=co_fraction,
co2_fraction=co2_fraction,
x=x,
y=y,
z=z,
file_id=file_id
)
async with async_session() as session:
session.add(new_data)
await session.commit()