2024-10-25 02:02:31 +04:00
|
|
|
from typing import Optional, List, Sequence
|
2024-10-14 21:18:07 +04:00
|
|
|
from sqlalchemy import select
|
2024-10-25 02:02:31 +04:00
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from sqlalchemy.future import select
|
|
|
|
from sqlalchemy import update, delete
|
|
|
|
from typing import List
|
2024-10-14 21:18:07 +04:00
|
|
|
from db.models.experiment_parameters_model import ExperimentParameters
|
|
|
|
|
|
|
|
class ExperimentParametersRepository:
|
2024-10-25 02:02:31 +04:00
|
|
|
def __init__(self, session: AsyncSession):
|
|
|
|
self.session = session
|
2024-10-14 21:18:07 +04:00
|
|
|
|
2024-10-25 02:02:31 +04:00
|
|
|
async def get_all(self) -> List[ExperimentParameters]:
|
|
|
|
result = await self.session.execute(select(ExperimentParameters))
|
|
|
|
return result.scalars().all()
|
2024-10-14 21:18:07 +04:00
|
|
|
|
2024-10-25 02:02:31 +04:00
|
|
|
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
|
2024-10-22 16:46:39 +04:00
|
|
|
|
2024-10-25 02:02:31 +04:00
|
|
|
async def update(self, id: int, updated_data: dict) -> ExperimentParameters:
|
|
|
|
stmt = (
|
|
|
|
update(ExperimentParameters).
|
|
|
|
where(ExperimentParameters.id == id).
|
|
|
|
values(**updated_data)
|
2024-10-22 16:46:39 +04:00
|
|
|
)
|
2024-10-25 02:02:31 +04:00
|
|
|
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()
|