Added experiment_category table

This commit is contained in:
AnnZhimol 2024-10-25 01:15:24 +03:00
parent 49e327005e
commit e52066f65d
5 changed files with 77 additions and 2 deletions

View File

@ -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 *
from .recycling_parameters_model import *
from .experiment_category import *

View File

@ -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"<ExperimentCategory>"

View File

@ -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"<ExperimentParameters>"

View File

@ -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 *
from .ch_experimentdb_experiment_data_repos import *
from .experiment_category_repos import *

View File

@ -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()