+ch_experiment_data in click_house +experiment_data and experiment_parameters in postgresql +experiment_data has foreign_key from experiment_parameters +new connections +realize routes for ch_experiment_data +new alg csv_to_db +new methods in repos +update dockerfile +update readme "how to init db"
56 lines
1.9 KiB
Python
56 lines
1.9 KiB
Python
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy.future import select
|
|
from sqlalchemy import update, delete
|
|
from typing import List
|
|
from db.models.experiment_data_model import ExperimentData
|
|
from network.schemas import ExperimentDataBody
|
|
|
|
|
|
class ExperimentDataRepository:
|
|
def __init__(self, session: AsyncSession):
|
|
self.session = session
|
|
|
|
async def get_all(self) -> List[ExperimentData]:
|
|
result = await self.session.execute(select(ExperimentData))
|
|
return result.scalars().all()
|
|
|
|
async def get_by_id(self, id: int) -> ExperimentData:
|
|
result = await self.session.execute(select(ExperimentData).where(ExperimentData.id == id))
|
|
return result.scalar_one_or_none()
|
|
|
|
async def create(self, experiment_data: ExperimentData) -> ExperimentData:
|
|
self.session.add(experiment_data)
|
|
await self.session.commit()
|
|
await self.session.refresh(experiment_data)
|
|
return experiment_data
|
|
|
|
async def update(self, id: int, updated_data: dict) -> ExperimentData:
|
|
stmt = (
|
|
update(ExperimentData).
|
|
where(ExperimentData.id == id).
|
|
values(**updated_data)
|
|
)
|
|
await self.session.execute(stmt)
|
|
await self.session.commit()
|
|
return await self.get_by_id(id)
|
|
|
|
async def delete(self, id: int) -> bool:
|
|
stmt = delete(ExperimentData).where(ExperimentData.id == id)
|
|
result = await self.session.execute(stmt)
|
|
await self.session.commit()
|
|
return result.rowcount > 0
|
|
|
|
# @staticmethod
|
|
# async def create_from_pydantic(body: ExperimentDataBody):
|
|
# new_data = ExperimentData(
|
|
# direction=body.direction,
|
|
# temperature=body.temperature,
|
|
# nox=body.nox,
|
|
# co2=body.co2,
|
|
# co=body.co,
|
|
# file_id=body.file_id
|
|
# )
|
|
# async with async_session() as session:
|
|
# session.add(new_data)
|
|
# await session.commit()
|