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

81 lines
2.8 KiB
Python
Raw Normal View History

from fastapi import APIRouter, HTTPException
from db.clickhouse_db_connection import session_clickhouse
from db.models import ChExperimentDBExperimentData
from db.repositories.ch_experimentdb_experiment_data_repos import ChExperimentDBExperimentDataRepository
from network.schemas import ChExperimentDBExperimentDataBody
from typing import List
router = APIRouter()
repository = ChExperimentDBExperimentDataRepository(session_clickhouse)
@router.post('/create')
async def create_ch_experimentdb_experiment_data(data: ChExperimentDBExperimentDataBody):
try:
new_record = repository.create(ChExperimentDBExperimentData(
volume=data.volume,
nitrogen_oxide_emission=data.nitrogen_oxide_emission,
temperature=data.temperature,
co_fraction=data.co_fraction,
co2_fraction=data.co2_fraction,
x=data.x,
y=data.y,
z=data.z,
file_id=data.file_id
))
return {"message": "Новая запись успешно добавлена"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
@router.get('/all', response_model=List[ChExperimentDBExperimentDataBody])
async def get_all_ch_experimentdb_experiment_data():
try:
result = repository.get_all()
if result:
return result
else:
return {"message": "Нет записей"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
@router.get('/{id}')
async def get_ch_experimentdb_experiment_data_by_id(id: int):
try:
record = repository.get_by_id(id)
if record:
return record
else:
raise HTTPException(status_code=404, detail="Запись не найдена")
except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
@router.get('/file_id/{file_id}')
async def get_ch_experimentdb_experiment_data_by_file_id(file_id: str):
try:
record = repository.get_by_file_id(file_id)
if record:
return record
else:
raise HTTPException(status_code=404, detail="Запись не найдена")
except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
@router.delete('/{id}/delete')
async def delete_ch_experimentdb_experiment_data(id: int):
try:
is_deleted = repository.delete(id)
if is_deleted:
return {"message": "Запись успешно удалена"}
else:
raise HTTPException(status_code=404, detail="Запись не найдена")
except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")