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

64 lines
2.2 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
from network.schemas import ChExperimentDBExperimentDataBody
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()
@staticmethod
async def create_from_pydantic(body: ChExperimentDBExperimentDataBody):
new_data = ChExperimentDBExperimentData(
volume=body.volume,
nitrogen_oxide_emission=body.nitrogen_oxide_emission,
temperature=body.temperature,
co_fraction=body.co_fraction,
co2_fraction=body.co2_fraction,
x=body.x,
y=body.y,
z=body.z,
file_id=body.file_id
)
async with async_session() as session:
session.add(new_data)
await session.commit()