2025-04-07 09:02:54 +02:00
|
|
|
|
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(
|
2025-04-07 09:18:31 +02:00
|
|
|
|
prefix="/calculation",
|
|
|
|
|
tags=["Calculations"],
|
2025-04-07 09:02:54 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@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
|