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

56 lines
2.3 KiB
Python
Raw Normal View History

from typing import Optional, List, Sequence
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from sqlalchemy import update, delete
from typing import List
from db.models.experiment_parameters_model import ExperimentParameters
class ExperimentParametersRepository:
def __init__(self, session: AsyncSession):
self.session = session
async def get_all(self) -> List[ExperimentParameters]:
result = await self.session.execute(select(ExperimentParameters))
return result.scalars().all()
async def get_by_id(self, id: int) -> ExperimentParameters:
result = await self.session.execute(select(ExperimentParameters).where(ExperimentParameters.id == id))
return result.scalar_one_or_none()
async def create(self, experiment_parameters: ExperimentParameters) -> ExperimentParameters:
self.session.add(experiment_parameters)
await self.session.commit()
await self.session.refresh(experiment_parameters)
return experiment_parameters
async def update(self, id: int, updated_data: dict) -> ExperimentParameters:
stmt = (
update(ExperimentParameters).
where(ExperimentParameters.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(ExperimentParameters).where(ExperimentParameters.id == id)
result = await self.session.execute(stmt)
await self.session.commit()
return result.rowcount > 0
# @staticmethod
# async def create_from_pydantic(body: ExperimentParametersBody):
# new_data = ExperimentParameters(
# outer_blades_count=body.outer_blades_count,
# outer_blades_length=body.outer_blades_length,
# outer_blades_angle=body.outer_blades_angle,
# middle_blades_count=body.middle_blades_count,
# load_id=body.load_id,
# recycling_id=body.recycling_id,
# experiment_hash=body.experiment_hash
# )
# async with async_session() as session:
# session.add(new_data)
# await session.commit()