modified_db #2

Merged
Sosees04ka merged 6 commits from modified_db into main 2024-11-30 01:45:57 +04:00
3 changed files with 31 additions and 5 deletions
Showing only changes of commit f394139d35 - Show all commits

View File

@ -42,6 +42,24 @@ async def update(model_class: Type[T], id: int, updated_data: Dict[str, Any]) ->
await session.commit()
return await get_by_id(model_class, id)
# Надо переписать/раасмотреть update
async def update_exp(model_class: Type[T], id: int, updated_data: Dict[str, Any]) -> Optional[T]:
async with async_session_postgres() as session:
async with session.begin(): # Явная транзакция
stmt = (
update_(model_class)
.where(model_class.id == id)
.values(**updated_data)
.returning(model_class) # Возвращаем обновленный объект
)
result = await session.execute(stmt)
updated_instance = result.scalars().first()
if not updated_instance:
return None
return updated_instance # Возвращаем сразу обновленный объект
async def delete(model_class: Type[T], id: int) -> bool:
async with async_session_postgres() as session:

View File

@ -54,3 +54,4 @@ async def save_experiment_to_db(df: pd.DataFrame):
except Exception as e:
print(f"Ошибка при сохранении данных: {e}")
raise HTTPException(status_code=500, detail=f"Ошибка при сохранении данных: {e}")

View File

@ -130,14 +130,21 @@ async def process_and_save_experiment_data(id: int):
o2=o2
)
# Обновляем ExperimentParameters, чтобы сохранить связи
experiment.load_id = load_params.id
experiment.recycling_id = recycling_params.id
# Обновляем ExperimentParameters
experiment = await update_exp(
ExperimentParameters,
id=experiment.id,
updated_data={
"load_id": load_params.id,
"recycling_id": recycling_params.id
}
)
return {
"message": "Данные успешно обработаны и сохранены.",
"load_parameters": load_params,
"recycling_parameters": recycling_params
"recycling_parameters": recycling_params,
"updated_experiment_parameters": experiment
}
except Exception as e: