feat: Add comment system and outlets data to News API
- Add comment models and service with CRUD operations - Add comment endpoints (GET, POST, count) - Add outlets-extracted.json with people/topics/companies data - Fix database connection in comment_service to use centralized get_database() 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@ -1,11 +1,26 @@
|
||||
from fastapi import APIRouter, HTTPException, Query
|
||||
from typing import Optional
|
||||
from app.services.article_service import ArticleService
|
||||
from app.services.comment_service import CommentService
|
||||
from app.models.article import ArticleList, Article, ArticleSummary
|
||||
from app.models.comment import Comment, CommentCreate, CommentList
|
||||
from typing import List
|
||||
import json
|
||||
import os
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
# Load outlets data
|
||||
OUTLETS_FILE = os.path.join(os.path.dirname(__file__), '../../outlets-extracted.json')
|
||||
outlets_data = None
|
||||
|
||||
def load_outlets():
|
||||
global outlets_data
|
||||
if outlets_data is None:
|
||||
with open(OUTLETS_FILE, 'r', encoding='utf-8') as f:
|
||||
outlets_data = json.load(f)
|
||||
return outlets_data
|
||||
|
||||
@router.get("/{language}/articles", response_model=ArticleList)
|
||||
async def get_articles(
|
||||
language: str,
|
||||
@ -65,3 +80,46 @@ async def get_categories(language: str):
|
||||
raise HTTPException(status_code=400, detail=f"Unsupported language: {language}")
|
||||
|
||||
return await ArticleService.get_categories(language)
|
||||
|
||||
@router.get("/outlets")
|
||||
async def get_outlets(category: Optional[str] = Query(None, description="Filter by category: people, topics, companies")):
|
||||
"""Get outlets list - people, topics, companies"""
|
||||
data = load_outlets()
|
||||
|
||||
if category:
|
||||
if category in ['people', 'topics', 'companies']:
|
||||
return {category: data[category]}
|
||||
else:
|
||||
raise HTTPException(status_code=400, detail=f"Invalid category: {category}. Must be one of: people, topics, companies")
|
||||
|
||||
return data
|
||||
|
||||
@router.get("/outlets/{outlet_id}")
|
||||
async def get_outlet_by_id(outlet_id: str):
|
||||
"""Get specific outlet by ID"""
|
||||
data = load_outlets()
|
||||
|
||||
# Search in all categories
|
||||
for category in ['people', 'topics', 'companies']:
|
||||
for outlet in data[category]:
|
||||
if outlet['id'] == outlet_id:
|
||||
return outlet
|
||||
|
||||
raise HTTPException(status_code=404, detail=f"Outlet not found: {outlet_id}")
|
||||
|
||||
# Comment endpoints
|
||||
@router.get("/comments", response_model=CommentList)
|
||||
async def get_comments(article_id: str = Query(..., alias="articleId")):
|
||||
"""Get comments for an article"""
|
||||
return await CommentService.get_comments_by_article(article_id)
|
||||
|
||||
@router.post("/comments", response_model=Comment)
|
||||
async def create_comment(comment: CommentCreate):
|
||||
"""Create a new comment"""
|
||||
return await CommentService.create_comment(comment)
|
||||
|
||||
@router.get("/articles/{article_id}/comment-count")
|
||||
async def get_comment_count(article_id: str):
|
||||
"""Get comment count for an article"""
|
||||
count = await CommentService.get_comment_count(article_id)
|
||||
return {"count": count}
|
||||
|
||||
Reference in New Issue
Block a user