7a34807fa0
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
73 lines
1.9 KiB
Python
73 lines
1.9 KiB
Python
from fastapi import APIRouter, HTTPException
|
|
from pydantic import BaseModel
|
|
from services import storage, classifier
|
|
|
|
router = APIRouter(prefix="/api/topics", tags=["topics"])
|
|
|
|
|
|
class TopicCreate(BaseModel):
|
|
name: str
|
|
description: str = ""
|
|
color: str = "#6366f1"
|
|
|
|
|
|
class TopicUpdate(BaseModel):
|
|
name: str | None = None
|
|
description: str | None = None
|
|
color: str | None = None
|
|
|
|
|
|
class SuggestRequest(BaseModel):
|
|
document_id: str
|
|
|
|
|
|
@router.get("")
|
|
async def list_topics():
|
|
topics = storage.load_topics()
|
|
counts = storage.topic_doc_counts()
|
|
for t in topics:
|
|
t["doc_count"] = counts.get(t["name"], 0)
|
|
return {"topics": topics}
|
|
|
|
|
|
@router.post("")
|
|
async def create_topic(body: TopicCreate):
|
|
topic = storage.create_topic(body.name, body.description, body.color)
|
|
topic["doc_count"] = 0
|
|
return topic
|
|
|
|
|
|
@router.patch("/{topic_id}")
|
|
async def update_topic(topic_id: str, body: TopicUpdate):
|
|
topic = storage.update_topic(
|
|
topic_id,
|
|
name=body.name,
|
|
description=body.description,
|
|
color=body.color,
|
|
)
|
|
if topic is None:
|
|
raise HTTPException(404, "Topic not found")
|
|
counts = storage.topic_doc_counts()
|
|
topic["doc_count"] = counts.get(topic["name"], 0)
|
|
return topic
|
|
|
|
|
|
@router.delete("/{topic_id}")
|
|
async def delete_topic(topic_id: str):
|
|
name = storage.delete_topic(topic_id)
|
|
if name is None:
|
|
raise HTTPException(404, "Topic not found")
|
|
return {"success": True, "removed_from_documents": True}
|
|
|
|
|
|
@router.post("/suggest")
|
|
async def suggest_topics(body: SuggestRequest):
|
|
meta = storage.get_metadata(body.document_id)
|
|
if meta is None:
|
|
raise HTTPException(404, "Document not found")
|
|
try:
|
|
suggestions = await classifier.suggest_topics_for_document(body.document_id)
|
|
except Exception as e:
|
|
raise HTTPException(500, f"Suggestion failed: {e}")
|
|
return {"suggested": suggestions}
|