Files
site11/services/news-api/backend/app/api/endpoints.py
jungwoo choi deb52e51f2 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>
2025-10-10 18:52:12 +09:00

126 lines
4.7 KiB
Python

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,
page: int = Query(1, ge=1, description="Page number"),
page_size: int = Query(20, ge=1, le=100, description="Items per page"),
category: Optional[str] = Query(None, description="Filter by category")
):
"""기사 목록 조회"""
if not ArticleService.validate_language(language):
raise HTTPException(status_code=400, detail=f"Unsupported language: {language}")
return await ArticleService.get_articles(language, page, page_size, category)
@router.get("/{language}/articles/latest", response_model=List[ArticleSummary])
async def get_latest_articles(
language: str,
limit: int = Query(10, ge=1, le=50, description="Number of articles")
):
"""최신 기사 조회"""
if not ArticleService.validate_language(language):
raise HTTPException(status_code=400, detail=f"Unsupported language: {language}")
return await ArticleService.get_latest_articles(language, limit)
@router.get("/{language}/articles/search", response_model=ArticleList)
async def search_articles(
language: str,
q: str = Query(..., min_length=1, description="Search keyword"),
page: int = Query(1, ge=1, description="Page number"),
page_size: int = Query(20, ge=1, le=100, description="Items per page")
):
"""기사 검색"""
if not ArticleService.validate_language(language):
raise HTTPException(status_code=400, detail=f"Unsupported language: {language}")
return await ArticleService.search_articles(language, q, page, page_size)
@router.get("/{language}/articles/{article_id}", response_model=Article)
async def get_article_by_id(
language: str,
article_id: str
):
"""ID로 기사 조회"""
if not ArticleService.validate_language(language):
raise HTTPException(status_code=400, detail=f"Unsupported language: {language}")
article = await ArticleService.get_article_by_id(language, article_id)
if not article:
raise HTTPException(status_code=404, detail="Article not found")
return article
@router.get("/{language}/categories", response_model=List[str])
async def get_categories(language: str):
"""카테고리 목록 조회"""
if not ArticleService.validate_language(language):
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}