- Updated test functions in various test files to enhance code clarity by formatting long lines and improving indentation. - Adjusted assertions to use multi-line formatting for better readability. - Added new test cases for theme settings API to ensure proper functionality. - Ensured consistent use of line breaks and spacing across test files for uniformity.
43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from pydantic import BaseModel, ConfigDict
|
|
from sqlalchemy.orm import Session
|
|
|
|
from models.scenario import Scenario
|
|
from routes.dependencies import get_db
|
|
|
|
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
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
|
|
@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()
|