2024-10-22 16:46:39 +04:00
|
|
|
|
from fastapi import APIRouter, HTTPException
|
2024-10-27 23:29:52 +04:00
|
|
|
|
|
|
|
|
|
from db.crud import *
|
|
|
|
|
from db.models import ExperimentData
|
2024-10-28 13:30:02 +04:00
|
|
|
|
from db.repositories import get_exp_data_by_file_id
|
2024-10-22 16:46:39 +04:00
|
|
|
|
from network.schemas import ExperimentDataBody
|
|
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post('/create')
|
2024-10-27 23:29:52 +04:00
|
|
|
|
async def create_experiment_data(body: ExperimentDataBody):
|
2024-10-22 16:46:39 +04:00
|
|
|
|
try:
|
2024-10-27 23:29:52 +04:00
|
|
|
|
await create(ExperimentData,
|
|
|
|
|
direction=body.direction,
|
|
|
|
|
temperature=body.temperature,
|
|
|
|
|
nox=body.nox,
|
|
|
|
|
co2=body.co2,
|
|
|
|
|
co=body.co,
|
|
|
|
|
file_id=body.file_id
|
|
|
|
|
)
|
2024-10-22 16:46:39 +04:00
|
|
|
|
|
|
|
|
|
return {"message": "Новая запись <ExperimentData> успешно добавлена"}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get('/all')
|
2024-10-22 20:55:30 +04:00
|
|
|
|
async def get_all_experiment_data():
|
2024-10-22 16:46:39 +04:00
|
|
|
|
try:
|
2024-10-27 23:29:52 +04:00
|
|
|
|
result = await get_all(ExperimentData)
|
|
|
|
|
|
|
|
|
|
if result is not None:
|
|
|
|
|
return result
|
|
|
|
|
else:
|
|
|
|
|
return {"message": "Нет записей в <ExperimentData>"}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get('/{id}')
|
|
|
|
|
async def get_by_id_experiment_data(id: int):
|
|
|
|
|
try:
|
|
|
|
|
result = await get_by_id(ExperimentData, id)
|
2024-10-22 16:46:39 +04:00
|
|
|
|
|
|
|
|
|
if result is not None:
|
|
|
|
|
return result
|
|
|
|
|
else:
|
2024-10-27 23:29:52 +04:00
|
|
|
|
return {"message": "Запись <ExperimentData> не найдена"}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
2024-10-28 13:30:02 +04:00
|
|
|
|
@router.get('/{file_id}')
|
|
|
|
|
async def get_experiment_data_by_file_id(file_id: str):
|
|
|
|
|
try:
|
|
|
|
|
result = await get_exp_data_by_file_id(file_id)
|
|
|
|
|
|
|
|
|
|
if result is not None:
|
|
|
|
|
return result
|
|
|
|
|
else:
|
|
|
|
|
return {"message": f'<ExperimentData> с file_id = {id} - не найдены'}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
2024-10-27 23:29:52 +04:00
|
|
|
|
@router.delete('/{id}/delete')
|
|
|
|
|
async def delete_experiment_data(id: int):
|
|
|
|
|
try:
|
|
|
|
|
is_deleted = await delete(ExperimentData, id)
|
|
|
|
|
if is_deleted:
|
|
|
|
|
return {"message": "Запись <ExperimentData> успешно удалена"}
|
|
|
|
|
else:
|
|
|
|
|
return {"message": "Запись <ExperimentData> не найдена"}
|
2024-10-22 16:46:39 +04:00
|
|
|
|
except Exception as e:
|
|
|
|
|
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|