44 lines
1.5 KiB
Python
44 lines
1.5 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
from datetime import datetime
|
|
|
|
from ..database import get_db
|
|
from ..models import Topic
|
|
from ..schemas import TopicResponse, PublishRequest
|
|
|
|
router = APIRouter(prefix="/api/topics", tags=["topics"])
|
|
|
|
@router.get("", response_model=List[TopicResponse])
|
|
def list_topics(
|
|
status: str = None,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
query = db.query(Topic)
|
|
if status:
|
|
query = query.filter(Topic.status == status)
|
|
topics = query.order_by(Topic.priority_score.desc(), Topic.created_at.desc()).all()
|
|
return topics
|
|
|
|
@router.get("/{topic_id}", response_model=TopicResponse)
|
|
def get_topic(topic_id: str, db: Session = Depends(get_db)):
|
|
topic = db.query(Topic).filter(Topic.id == topic_id).first()
|
|
if not topic:
|
|
raise HTTPException(status_code=404, detail="Topic not found")
|
|
return topic
|
|
|
|
@router.post("/{topic_id}/publish")
|
|
def publish_topic(topic_id: str, req: PublishRequest, db: Session = Depends(get_db)):
|
|
topic = db.query(Topic).filter(Topic.id == topic_id).first()
|
|
if not topic:
|
|
raise HTTPException(status_code=404, detail="Topic not found")
|
|
if topic.status != "ready":
|
|
raise HTTPException(status_code=400, detail="Topic not in ready status")
|
|
|
|
topic.status = "published"
|
|
topic.published_at = datetime.now().date()
|
|
topic.platform_urls = req.platform_urls
|
|
db.commit()
|
|
|
|
return {"message": "Topic marked as published", "topic_id": topic_id}
|