решила проверить методы репозитория, пока ничего не работает .....
This commit is contained in:
parent
cc2e45619a
commit
16ecd5789f
@ -31,3 +31,9 @@ class LoadParametersRepository:
|
|||||||
async with async_session() as session:
|
async with async_session() as session:
|
||||||
session.add(new_data)
|
session.add(new_data)
|
||||||
await session.commit()
|
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
31
main.py
@ -1,8 +1,11 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
from fastapi import FastAPI, HTTPException
|
from typing import Optional
|
||||||
|
from fastapi import FastAPI, HTTPException, Body, Response
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from new_experiment_planner import run_experiment # Импортируем функцию из твоего скрипта
|
from new_experiment_planner import run_experiment # Импортируем функцию из твоего скрипта
|
||||||
from db.csv_to_db import csv_to_db
|
from db.csv_to_db import csv_to_db
|
||||||
|
from db.repositories.load_parameters_repos import LoadParametersRepository
|
||||||
|
from network.bodies import LoadParametersBody
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
@ -15,6 +18,8 @@ class ExperimentParameters(BaseModel):
|
|||||||
load: str
|
load: str
|
||||||
recycling: str
|
recycling: str
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Эндпоинт для запуска эксперимента
|
# Эндпоинт для запуска эксперимента
|
||||||
@app.post("/run_experiment/")
|
@app.post("/run_experiment/")
|
||||||
def run_experiment_api(params: ExperimentParameters):
|
def run_experiment_api(params: ExperimentParameters):
|
||||||
@ -37,6 +42,28 @@ def run_experiment_api(params: ExperimentParameters):
|
|||||||
def init_db_data():
|
def init_db_data():
|
||||||
try:
|
try:
|
||||||
asyncio.run(csv_to_db())
|
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:
|
except Exception as e:
|
||||||
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
12
network/bodies.py
Normal file
12
network/bodies.py
Normal 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
|
Loading…
Reference in New Issue
Block a user