2024-10-17 16:33:41 +04:00
|
|
|
from db.models.ch_experimentdb_experiment_data_model import ChExperimentDBExperimentData
|
2024-10-25 02:02:31 +04:00
|
|
|
from sqlalchemy import select,func
|
2024-10-17 16:33:41 +04:00
|
|
|
|
|
|
|
class ChExperimentDBExperimentDataRepository:
|
2024-10-25 02:02:31 +04:00
|
|
|
def __init__(self, session):
|
|
|
|
self.session = session
|
|
|
|
|
|
|
|
def get_all(self):
|
|
|
|
return self.session.query(ChExperimentDBExperimentData).all()
|
|
|
|
|
|
|
|
def get_by_id(self, id: int) -> ChExperimentDBExperimentData:
|
|
|
|
return self.session.query(ChExperimentDBExperimentData).filter(ChExperimentDBExperimentData.id == id).one_or_none()
|
|
|
|
|
2024-10-28 14:18:14 +04:00
|
|
|
def get_by_file_id(self, file_id: str) -> ChExperimentDBExperimentData:
|
|
|
|
return self.session.query(ChExperimentDBExperimentData).filter(ChExperimentDBExperimentData.file_id == file_id).all()
|
|
|
|
|
2024-10-25 02:02:31 +04:00
|
|
|
def create(self, ch_experiment_data: ChExperimentDBExperimentData) -> ChExperimentDBExperimentData:
|
|
|
|
max_id_query = select(func.max(ChExperimentDBExperimentData.id))
|
|
|
|
max_id_result = self.session.execute(max_id_query).scalar()
|
|
|
|
|
|
|
|
max_id = max_id_result or 0
|
|
|
|
|
|
|
|
ch_experiment_data.id = max_id + 1
|
|
|
|
|
|
|
|
self.session.add(ch_experiment_data)
|
|
|
|
self.session.commit()
|
|
|
|
|
|
|
|
return ch_experiment_data
|
|
|
|
|
|
|
|
def update(self, id: int, updated_data: dict) -> ChExperimentDBExperimentData:
|
|
|
|
self.session.query(ChExperimentDBExperimentData).filter(ChExperimentDBExperimentData.id == id).update(updated_data)
|
|
|
|
self.session.commit()
|
|
|
|
return self.get_by_id(id)
|
|
|
|
|
|
|
|
def delete(self, id: int) -> bool:
|
|
|
|
item = self.get_by_id(id)
|
|
|
|
if item:
|
|
|
|
self.session.delete(item)
|
|
|
|
self.session.commit()
|
|
|
|
return True
|
|
|
|
return False
|