govorov/backend/app/routers/calculation.py

58 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from typing import List
from app.schemas.calculation import CalculationCreate, CalculationRead
from app.services import calculation_service as service
from app.db.session import get_db
router = APIRouter(
prefix="/calculation",
tags=["Calculations"],
)
@router.post("/", response_model=CalculationRead, status_code=status.HTTP_201_CREATED)
async def run_calculation(
calc_in: CalculationCreate,
db: Session = Depends(get_db)
):
"""
Запускает новый расчет раскройки стекла (асинхронно).
- Принимает `input_params` и опционально `model_name`.
- Вызывает внутреннюю асинхронную логику расчета (заглушку).
- Сохраняет входные параметры и результаты в БД.
- Возвращает созданную запись о расчете.
"""
print("Received request to run calculation")
calculation = await service.create_calculation(db=db, calc_in=calc_in)
print("Calculation service finished, returning response")
return calculation
@router.get("/{calculation_id}", response_model=CalculationRead)
async def read_calculation(
calculation_id: int,
db: Session = Depends(get_db)
):
"""
Получает информацию о конкретном расчете по его ID.
"""
db_calc = service.get_calculation_by_id(db=db, calc_id=calculation_id)
if db_calc is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Calculation not found"
)
return db_calc
@router.get("/history/", response_model=List[CalculationRead])
async def read_calculation_history(
skip: int = 0,
limit: int = 100,
db: Session = Depends(get_db)
):
"""
Получает историю всех выполненных расчетов (с пагинацией).
"""
calculations = service.get_calculations(db=db, skip=skip, limit=limit)
return calculations