- 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
53 lines
1.3 KiB
Python
53 lines
1.3 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from config.database import SessionLocal
|
|
from models.scenario import Scenario
|
|
from pydantic import BaseModel
|
|
from typing import Optional
|
|
from datetime import datetime
|
|
|
|
router = APIRouter(prefix="/api/scenarios", tags=["scenarios"])
|
|
|
|
# Pydantic schemas
|
|
|
|
|
|
class ScenarioCreate(BaseModel):
|
|
name: str
|
|
description: Optional[str] = None
|
|
|
|
|
|
class ScenarioRead(ScenarioCreate):
|
|
id: int
|
|
created_at: datetime
|
|
updated_at: Optional[datetime] = None
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
|
|
# Dependency
|
|
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
@router.post("/", response_model=ScenarioRead)
|
|
def create_scenario(scenario: ScenarioCreate, db: Session = Depends(get_db)):
|
|
db_s = db.query(Scenario).filter(Scenario.name == scenario.name).first()
|
|
if db_s:
|
|
raise HTTPException(status_code=400, detail="Scenario already exists")
|
|
new_s = Scenario(name=scenario.name, description=scenario.description)
|
|
db.add(new_s)
|
|
db.commit()
|
|
db.refresh(new_s)
|
|
return new_s
|
|
|
|
|
|
@router.get("/", response_model=list[ScenarioRead])
|
|
def list_scenarios(db: Session = Depends(get_db)):
|
|
return db.query(Scenario).all()
|