- Create .env.example for environment variables - Update README with project structure and development setup instructions - Implement FastAPI application with API routes for scenarios and parameters - Add database models for scenarios, parameters, and simulation results - Introduce validation middleware for JSON requests - Create services for running simulations and generating reports - Add testing strategy and directory structure in documentation
51 lines
1.2 KiB
Python
51 lines
1.2 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from config.database import SessionLocal
|
|
from models.parameters import Parameter
|
|
from models.scenario import Scenario
|
|
from pydantic import BaseModel
|
|
from typing import Optional, List
|
|
|
|
router = APIRouter(prefix="/api/parameters", tags=["parameters"])
|
|
|
|
|
|
class ParameterCreate(BaseModel):
|
|
scenario_id: int
|
|
name: str
|
|
value: float
|
|
|
|
|
|
class ParameterRead(ParameterCreate):
|
|
id: int
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
|
|
# Dependency
|
|
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
@router.post("/", response_model=ParameterRead)
|
|
def create_parameter(param: ParameterCreate, db: Session = Depends(get_db)):
|
|
scen = db.query(Scenario).filter(Scenario.id == param.scenario_id).first()
|
|
if not scen:
|
|
raise HTTPException(status_code=404, detail="Scenario not found")
|
|
new_param = Parameter(scenario_id=param.scenario_id,
|
|
name=param.name, value=param.value)
|
|
db.add(new_param)
|
|
db.commit()
|
|
db.refresh(new_param)
|
|
return new_param
|
|
|
|
|
|
@router.get("/", response_model=List[ParameterRead])
|
|
def list_parameters(db: Session = Depends(get_db)):
|
|
return db.query(Parameter).all()
|