feat(fase4): add FlowTemplate model and API
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
86
services/api-gateway/app/routers/flow_templates.py
Normal file
86
services/api-gateway/app/routers/flow_templates.py
Normal file
@@ -0,0 +1,86 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List, Optional
|
||||
from uuid import UUID
|
||||
from app.core.database import get_db
|
||||
from app.core.security import get_current_user
|
||||
from app.models.user import User
|
||||
from app.models.flow_template import FlowTemplate
|
||||
from app.models.flow import Flow, TriggerType
|
||||
from app.schemas.flow_template import FlowTemplateCreate, FlowTemplateResponse
|
||||
|
||||
router = APIRouter(prefix="/api/flow-templates", tags=["flow-templates"])
|
||||
|
||||
|
||||
@router.get("", response_model=List[FlowTemplateResponse])
|
||||
def list_templates(
|
||||
category: Optional[str] = None,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
query = db.query(FlowTemplate).filter(FlowTemplate.is_public == True)
|
||||
if category:
|
||||
query = query.filter(FlowTemplate.category == category)
|
||||
return query.all()
|
||||
|
||||
|
||||
@router.post("", response_model=FlowTemplateResponse)
|
||||
def create_template(
|
||||
request: FlowTemplateCreate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
template = FlowTemplate(
|
||||
**request.model_dump(),
|
||||
created_by=current_user.id,
|
||||
)
|
||||
db.add(template)
|
||||
db.commit()
|
||||
db.refresh(template)
|
||||
return template
|
||||
|
||||
|
||||
@router.post("/{template_id}/use")
|
||||
def use_template(
|
||||
template_id: UUID,
|
||||
flow_name: str,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Create a new flow from template"""
|
||||
template = db.query(FlowTemplate).filter(FlowTemplate.id == template_id).first()
|
||||
if not template:
|
||||
raise HTTPException(status_code=404, detail="Template not found")
|
||||
|
||||
flow = Flow(
|
||||
name=flow_name,
|
||||
description=f"Created from template: {template.name}",
|
||||
trigger_type=TriggerType.KEYWORD,
|
||||
nodes=template.nodes,
|
||||
edges=template.edges,
|
||||
variables=template.variables,
|
||||
is_active=False,
|
||||
)
|
||||
db.add(flow)
|
||||
db.commit()
|
||||
db.refresh(flow)
|
||||
|
||||
return {"success": True, "flow_id": str(flow.id)}
|
||||
|
||||
|
||||
@router.delete("/{template_id}")
|
||||
def delete_template(
|
||||
template_id: UUID,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
template = db.query(FlowTemplate).filter(FlowTemplate.id == template_id).first()
|
||||
if not template:
|
||||
raise HTTPException(status_code=404, detail="Template not found")
|
||||
|
||||
if template.created_by != current_user.id:
|
||||
raise HTTPException(status_code=403, detail="Not authorized")
|
||||
|
||||
db.delete(template)
|
||||
db.commit()
|
||||
return {"success": True}
|
||||
Reference in New Issue
Block a user