PIbd-42_SSPR/network/routes/experiment_parameters_router.py

92 lines
3.3 KiB
Python
Raw Normal View History

from fastapi import APIRouter, HTTPException
from db.crud import *
from db.models import LoadParameters
from db.repositories import get_exp_parameters_by_category, get_exp_parameters_by_exp_hash
from network.schemas import ExperimentParametersBody
router = APIRouter()
@router.post('/create')
async def create_experiment_parameters(body: ExperimentParametersBody):
try:
await create(ExperimentParameters,
outer_blades_count=body.outer_blades_count,
outer_blades_length=body.outer_blades_length,
outer_blades_angle=body.outer_blades_angle,
middle_blades_count=body.middle_blades_count,
load_id=body.load_id,
recycling_id=body.recycling_id,
experiment_hash=body.experiment_hash
)
return {"message": "Новая запись <ExperimentParameters> успешно добавлена"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
@router.get('/all')
async def get_all_experiment_parameters():
try:
result = await get_all(ExperimentParameters)
if result is not None:
return result
else:
return {"message": "Нет записей в <ExperimentParameters>"}
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_parameters(id: int):
try:
result = await get_by_id(ExperimentParameters, id)
if result is not None:
return result
else:
return {"message": "Запись <ExperimentParameters> не найдена"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
@router.get('/by_category/{id}')
async def get_experiment_parameters_by_exp_category(id: int):
try:
result = await get_exp_parameters_by_category(id)
if result is not None:
return result
else:
return {"message": f'<ExperimentParameters> с идентификатором категории - {id} - не найдены'}
except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
@router.get('/by_exp_hash/{hash}')
async def get_experiment_parameters_by_exp_category(hash: str):
try:
result = await get_exp_parameters_by_exp_hash(hash)
if result is not None:
return result
else:
return {"message": f'<ExperimentParameters> с experiment_hash = {id} - не найдены'}
except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
@router.delete('/{id}/delete')
async def delete_experiment_parameters(id: int):
try:
is_deleted = await delete(ExperimentParameters, id)
if is_deleted:
return {"message": "Запись <ExperimentParameters> успешно удалена"}
else:
return {"message": "Запись <ExperimentParameters> не найдена"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")