решила проверить методы репозитория, пока ничего не работает .....

This commit is contained in:
Inohara 2024-10-17 17:13:21 +04:00
parent cc2e45619a
commit 16ecd5789f
3 changed files with 47 additions and 2 deletions

View File

@ -31,3 +31,9 @@ class LoadParametersRepository:
async with async_session() as session:
session.add(new_data)
await session.commit()
@staticmethod
async def create(new_data: LoadParameters):
async with async_session() as session:
session.add(new_data)
await session.commit()

31
main.py
View File

@ -1,8 +1,11 @@
import asyncio
from fastapi import FastAPI, HTTPException
from typing import Optional
from fastapi import FastAPI, HTTPException, Body, Response
from pydantic import BaseModel
from new_experiment_planner import run_experiment # Импортируем функцию из твоего скрипта
from db.csv_to_db import csv_to_db
from db.repositories.load_parameters_repos import LoadParametersRepository
from network.bodies import LoadParametersBody
app = FastAPI()
@ -15,6 +18,8 @@ class ExperimentParameters(BaseModel):
load: str
recycling: str
# Эндпоинт для запуска эксперимента
@app.post("/run_experiment/")
def run_experiment_api(params: ExperimentParameters):
@ -37,6 +42,28 @@ def run_experiment_api(params: ExperimentParameters):
def init_db_data():
try:
asyncio.run(csv_to_db())
return {"status": "success", "message": "Experiment started successfully."}
return {"status": "success", "message": "База данных инициализирована. Данные из файлов csv успешно добавлены."}
except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
@app.post('/test')
def test(data = LoadParametersBody):
try:
asyncio.run(LoadParametersRepository.create(data))
return {"status": "success", "message": "test +"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
@app.get('/all')
def get_all():
try:
result = asyncio.run(LoadParametersRepository.get_all())
if result is not None:
return {"status": "success", "data": [LoadParametersBody.model_validate(param) for param in result]}
else:
return {"status": "success", "message":"result is not None"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")

12
network/bodies.py Normal file
View File

@ -0,0 +1,12 @@
from pydantic import BaseModel
class LoadParametersBody(BaseModel):
id:int
load:int
primary_air_consumption: float
secondary_air_consumption:float
gas_inlet_consumption:float
class Config:
from_attributes = True # Позволяет Pydantic работать с объектами SQLAlchemy