Files
2026-03-27 20:59:06 +01:00

84 lines
2.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Router template messaggi (Feature 1).
Endpoint:
GET /templates lista template del tenant
POST /templates crea template (admin)
GET /templates/{id} dettaglio template
PUT /templates/{id} aggiorna template (admin)
DELETE /templates/{id} elimina template (admin)
"""
import uuid
from fastapi import APIRouter, Query, status
from app.dependencies import AdminUser, CurrentUser, DB
from app.schemas.template import TemplateCreate, TemplateListResponse, TemplateResponse, TemplateUpdate
from app.services.template_service import TemplateService
router = APIRouter(tags=["Templates"])
@router.get("/templates", response_model=TemplateListResponse)
async def list_templates(
current_user: CurrentUser,
db: DB,
q: str | None = Query(None, description="Filtro per nome"),
) -> TemplateListResponse:
"""Elenca i template del tenant corrente."""
svc = TemplateService(db)
items, total = await svc.list_templates(current_user.tenant_id, q=q)
return TemplateListResponse(
items=[TemplateResponse.model_validate(t) for t in items],
total=total,
)
@router.post("/templates", response_model=TemplateResponse, status_code=status.HTTP_201_CREATED)
async def create_template(
data: TemplateCreate,
current_user: AdminUser,
db: DB,
) -> TemplateResponse:
"""Crea un nuovo template (solo admin)."""
svc = TemplateService(db)
template = await svc.create_template(current_user.tenant_id, data, created_by=current_user.id)
return TemplateResponse.model_validate(template)
@router.get("/templates/{template_id}", response_model=TemplateResponse)
async def get_template(
template_id: uuid.UUID,
current_user: CurrentUser,
db: DB,
) -> TemplateResponse:
"""Restituisce il dettaglio di un template."""
svc = TemplateService(db)
template = await svc.get_template(current_user.tenant_id, template_id)
return TemplateResponse.model_validate(template)
@router.put("/templates/{template_id}", response_model=TemplateResponse)
async def update_template(
template_id: uuid.UUID,
data: TemplateUpdate,
current_user: AdminUser,
db: DB,
) -> TemplateResponse:
"""Aggiorna un template esistente (solo admin)."""
svc = TemplateService(db)
template = await svc.update_template(current_user.tenant_id, template_id, data)
return TemplateResponse.model_validate(template)
@router.delete("/templates/{template_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_template(
template_id: uuid.UUID,
current_user: AdminUser,
db: DB,
) -> None:
"""Elimina un template (solo admin)."""
svc = TemplateService(db)
await svc.delete_template(current_user.tenant_id, template_id)