From 16ecd5789f8b0781d0c532ad9c170d601d34039a Mon Sep 17 00:00:00 2001 From: Inohara Date: Thu, 17 Oct 2024 17:13:21 +0400 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B5=D1=88=D0=B8=D0=BB=D0=B0=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=B8=D1=82=D1=8C=20=D0=BC=D0=B5?= =?UTF-8?q?=D1=82=D0=BE=D0=B4=D1=8B=20=D1=80=D0=B5=D0=BF=D0=BE=D0=B7=D0=B8?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=B8=D1=8F,=20=D0=BF=D0=BE=D0=BA=D0=B0=20?= =?UTF-8?q?=20=D0=BD=D0=B8=D1=87=D0=B5=D0=B3=D0=BE=20=D0=BD=D0=B5=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82=20.....?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/repositories/load_parameters_repos.py | 6 +++++ main.py | 31 ++++++++++++++++++++++-- network/bodies.py | 12 +++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 network/bodies.py diff --git a/db/repositories/load_parameters_repos.py b/db/repositories/load_parameters_repos.py index 4419a68..3d5eca7 100644 --- a/db/repositories/load_parameters_repos.py +++ b/db/repositories/load_parameters_repos.py @@ -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() diff --git a/main.py b/main.py index ac84965..f26c8f3 100644 --- a/main.py +++ b/main.py @@ -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)}") \ No newline at end of file diff --git a/network/bodies.py b/network/bodies.py new file mode 100644 index 0000000..3f843cf --- /dev/null +++ b/network/bodies.py @@ -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 \ No newline at end of file