PIbd-42_SSPR/network/routes/ch_experimentdb_experiment_data_router.py
AnnZhimol 49e327005e Update Database:
+ch_experiment_data in click_house
+experiment_data and experiment_parameters in postgresql
+experiment_data has foreign_key from experiment_parameters
+new connections
+realize routes for ch_experiment_data
+new alg csv_to_db
+new methods in repos
+update dockerfile
+update readme "how to init db"
2024-10-25 01:02:31 +03:00

68 lines
2.4 KiB
Python

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.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)}")