2024-10-22 16:46:39 +04:00
|
|
|
from fastapi import APIRouter, HTTPException
|
2024-10-25 02:02:31 +04:00
|
|
|
|
|
|
|
from db.clickhouse_db_connection import session_clickhouse
|
|
|
|
from db.models import ChExperimentDBExperimentData
|
2024-10-22 16:46:39 +04:00
|
|
|
from db.repositories.ch_experimentdb_experiment_data_repos import ChExperimentDBExperimentDataRepository
|
|
|
|
from network.schemas import ChExperimentDBExperimentDataBody
|
2024-10-25 02:02:31 +04:00
|
|
|
from typing import List
|
2024-10-22 16:46:39 +04:00
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
2024-10-25 02:02:31 +04:00
|
|
|
repository = ChExperimentDBExperimentDataRepository(session_clickhouse)
|
2024-10-22 16:46:39 +04:00
|
|
|
|
|
|
|
@router.post('/create')
|
2024-10-22 20:55:30 +04:00
|
|
|
async def create_ch_experimentdb_experiment_data(data: ChExperimentDBExperimentDataBody):
|
2024-10-22 16:46:39 +04:00
|
|
|
try:
|
2024-10-25 02:02:31 +04:00
|
|
|
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
|
|
|
|
))
|
2024-10-22 16:46:39 +04:00
|
|
|
|
2024-10-25 02:02:31 +04:00
|
|
|
return {"message": "Новая запись успешно добавлена"}
|
2024-10-22 16:46:39 +04:00
|
|
|
except Exception as e:
|
|
|
|
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
|
|
|
|
|
|
|
|
2024-10-25 02:02:31 +04:00
|
|
|
@router.get('/all', response_model=List[ChExperimentDBExperimentDataBody])
|
2024-10-22 20:55:30 +04:00
|
|
|
async def get_all_ch_experimentdb_experiment_data():
|
2024-10-22 16:46:39 +04:00
|
|
|
try:
|
2024-10-25 02:02:31 +04:00
|
|
|
result = repository.get_all()
|
2024-10-22 16:46:39 +04:00
|
|
|
|
2024-10-25 02:02:31 +04:00
|
|
|
if result:
|
2024-10-22 16:46:39 +04:00
|
|
|
return result
|
|
|
|
else:
|
2024-10-25 02:02:31 +04:00
|
|
|
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)}")
|
|
|
|
|
2024-10-28 14:18:14 +04:00
|
|
|
|
|
|
|
@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)}")
|
|
|
|
|
|
|
|
|
2024-10-25 02:02:31 +04:00
|
|
|
@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="Запись не найдена")
|
2024-10-22 16:46:39 +04:00
|
|
|
except Exception as e:
|
|
|
|
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|