From e52066f65d6c66e0d41c9d4e2f7f9a642806a108 Mon Sep 17 00:00:00 2001 From: AnnZhimol Date: Fri, 25 Oct 2024 01:15:24 +0300 Subject: [PATCH] Added experiment_category table --- db/models/__init__.py | 3 +- db/models/experiment_category.py | 17 ++++++ db/models/experiment_parameters_model.py | 1 + db/repositories/__init__.py | 3 +- db/repositories/experiment_category_repos.py | 55 ++++++++++++++++++++ 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 db/models/experiment_category.py create mode 100644 db/repositories/experiment_category_repos.py diff --git a/db/models/__init__.py b/db/models/__init__.py index 4fa29b3..6cbd7d5 100644 --- a/db/models/__init__.py +++ b/db/models/__init__.py @@ -3,4 +3,5 @@ from .experiment_parameters_model import * from .experiment_data_model import * from .ch_experimentdb_experiment_data_model import * from .load_parameters_model import * -from .recycling_parameters_model import * \ No newline at end of file +from .recycling_parameters_model import * +from .experiment_category import * \ No newline at end of file diff --git a/db/models/experiment_category.py b/db/models/experiment_category.py new file mode 100644 index 0000000..a28f75c --- /dev/null +++ b/db/models/experiment_category.py @@ -0,0 +1,17 @@ +from typing import Optional + +from sqlalchemy import Identity, ForeignKey +from sqlalchemy.orm import Mapped, mapped_column + +from db.models.base import Base + + +class ExperimentCategory(Base): + __tablename__ = 'experiment_category' + + id: Mapped[int] = mapped_column(Identity(start=21, cycle=True), + primary_key=True) + name: Mapped[str] + + def __repr__(self): + return f"" diff --git a/db/models/experiment_parameters_model.py b/db/models/experiment_parameters_model.py index 927be7b..03e5ae1 100644 --- a/db/models/experiment_parameters_model.py +++ b/db/models/experiment_parameters_model.py @@ -18,6 +18,7 @@ class ExperimentParameters(Base): load_id: Mapped[Optional[int]] = mapped_column(ForeignKey('load_parameters.id', ondelete='SET NULL')) recycling_id: Mapped[Optional[int]] = mapped_column(ForeignKey('recycling_parameters.id', ondelete='SET NULL')) experiment_hash: Mapped[str] = mapped_column(unique=True) + experiment_category_id: Mapped[Optional[int]] = mapped_column(ForeignKey('experiment_category.id', ondelete='SET NULL'), nullable=True) def __repr__(self): return f"" diff --git a/db/repositories/__init__.py b/db/repositories/__init__.py index f6f7ebc..ee1f375 100644 --- a/db/repositories/__init__.py +++ b/db/repositories/__init__.py @@ -2,4 +2,5 @@ from .experiment_data_repos import * from .experiment_parameters_repos import * from .load_parameters_repos import * from .recycling_parameters_repos import * -from .ch_experimentdb_experiment_data_repos import * \ No newline at end of file +from .ch_experimentdb_experiment_data_repos import * +from .experiment_category_repos import * \ No newline at end of file diff --git a/db/repositories/experiment_category_repos.py b/db/repositories/experiment_category_repos.py new file mode 100644 index 0000000..a885c22 --- /dev/null +++ b/db/repositories/experiment_category_repos.py @@ -0,0 +1,55 @@ +from sqlalchemy.ext.asyncio import AsyncSession +from sqlalchemy.future import select +from sqlalchemy import update, delete +from typing import List + +from db.models import ExperimentCategory + + +class ExperimentCategoryRepository: + def __init__(self, session: AsyncSession): + self.session = session + + async def get_all(self) -> List[ExperimentCategory]: + result = await self.session.execute(select(ExperimentCategory)) + return result.scalars().all() + + async def get_by_id(self, id: int) -> ExperimentCategory: + result = await self.session.execute(select(ExperimentCategory).where(ExperimentCategory.id == id)) + return result.scalar_one_or_none() + + async def create(self, experiment_category: ExperimentCategory) -> ExperimentCategory: + self.session.add(experiment_category) + await self.session.commit() + await self.session.refresh(experiment_category) + return experiment_category + + async def update(self, id: int, updated_data: dict) -> ExperimentCategory: + stmt = ( + update(ExperimentCategory). + where(ExperimentCategory.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(ExperimentCategory).where(ExperimentCategory.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()