feat: Refactor outlets with multilingual support and dynamic queries
- Replace static articles array with dynamic source_keyword queries
- Use MongoDB _id as unique identifier for outlets
- Add multilingual translations (9 languages: ko, en, zh_cn, zh_tw, ja, fr, de, es, it)
- Add OutletService for database operations
- Add outlet migration script with Korean source_keyword matching
- Remove JSON file-based outlet loading
- Add /outlets/{outlet_id}/articles endpoint for dynamic article retrieval
This resolves the design issues with:
1. Static articles array requiring constant updates
2. Lack of multilingual support for outlet names/descriptions
3. Broken image URLs
4. Korean entity matching for article queries
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@ -2,25 +2,14 @@ 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.services.outlet_service import OutletService
|
||||
from app.models.article import ArticleList, Article, ArticleSummary
|
||||
from app.models.comment import Comment, CommentCreate, CommentList
|
||||
from app.models.outlet import Outlet
|
||||
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,
|
||||
@ -84,28 +73,48 @@ async def get_categories(language: str):
|
||||
@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")
|
||||
# Get outlets for specific category
|
||||
outlets = await OutletService.get_all_outlets(category=category)
|
||||
return {category: outlets}
|
||||
|
||||
return data
|
||||
# Get all outlets grouped by category
|
||||
result = {}
|
||||
for cat in ['people', 'topics', 'companies']:
|
||||
outlets = await OutletService.get_all_outlets(category=cat)
|
||||
result[cat] = outlets
|
||||
|
||||
return result
|
||||
|
||||
@router.get("/outlets/{outlet_id}")
|
||||
async def get_outlet_by_id(outlet_id: str):
|
||||
"""Get specific outlet by ID"""
|
||||
data = load_outlets()
|
||||
"""Get specific outlet by ID (_id)"""
|
||||
outlet = await OutletService.get_outlet_by_id(outlet_id)
|
||||
return outlet
|
||||
|
||||
# Search in all categories
|
||||
for category in ['people', 'topics', 'companies']:
|
||||
for outlet in data[category]:
|
||||
if outlet['id'] == outlet_id:
|
||||
return outlet
|
||||
@router.get("/{language}/outlets/{outlet_id}/articles")
|
||||
async def get_outlet_articles(
|
||||
language: str,
|
||||
outlet_id: str,
|
||||
page: int = Query(1, ge=1, description="Page number"),
|
||||
page_size: int = Query(20, ge=1, le=100, description="Items per page")
|
||||
):
|
||||
"""Get articles for a specific outlet using source_keyword"""
|
||||
if not ArticleService.validate_language(language):
|
||||
raise HTTPException(status_code=400, detail=f"Unsupported language: {language}")
|
||||
|
||||
raise HTTPException(status_code=404, detail=f"Outlet not found: {outlet_id}")
|
||||
# Get outlet to retrieve source_keyword
|
||||
outlet = await OutletService.get_outlet_by_id(outlet_id)
|
||||
|
||||
# Query articles by source_keyword dynamically
|
||||
articles_result = await ArticleService.get_articles_by_source_keyword(
|
||||
language,
|
||||
outlet['source_keyword'],
|
||||
page,
|
||||
page_size
|
||||
)
|
||||
|
||||
return articles_result
|
||||
|
||||
# Comment endpoints
|
||||
@router.get("/comments", response_model=CommentList)
|
||||
|
||||
47
services/news-api/backend/app/models/outlet.py
Normal file
47
services/news-api/backend/app/models/outlet.py
Normal file
@ -0,0 +1,47 @@
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import List, Optional, Dict
|
||||
|
||||
class OutletTranslations(BaseModel):
|
||||
ko: Optional[str] = None
|
||||
en: Optional[str] = None
|
||||
zh_cn: Optional[str] = None
|
||||
zh_tw: Optional[str] = None
|
||||
ja: Optional[str] = None
|
||||
fr: Optional[str] = None
|
||||
de: Optional[str] = None
|
||||
es: Optional[str] = None
|
||||
it: Optional[str] = None
|
||||
|
||||
class OutletBase(BaseModel):
|
||||
source_keyword: str # Used to query articles dynamically
|
||||
category: str # people, topics, companies
|
||||
name_translations: OutletTranslations = Field(default_factory=lambda: OutletTranslations())
|
||||
description_translations: OutletTranslations = Field(default_factory=lambda: OutletTranslations())
|
||||
image: Optional[str] = None
|
||||
|
||||
# Deprecated - kept for backward compatibility during migration
|
||||
name: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
|
||||
class OutletCreate(OutletBase):
|
||||
pass
|
||||
|
||||
class OutletUpdate(BaseModel):
|
||||
source_keyword: Optional[str] = None
|
||||
category: Optional[str] = None
|
||||
name_translations: Optional[OutletTranslations] = None
|
||||
description_translations: Optional[OutletTranslations] = None
|
||||
image: Optional[str] = None
|
||||
|
||||
# Deprecated
|
||||
name: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
articles: Optional[List[str]] = None
|
||||
|
||||
class Outlet(OutletBase):
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
class OutletList(BaseModel):
|
||||
outlets: List[Outlet]
|
||||
total: int
|
||||
111
services/news-api/backend/app/services/outlet_service.py
Normal file
111
services/news-api/backend/app/services/outlet_service.py
Normal file
@ -0,0 +1,111 @@
|
||||
from app.core.database import get_database
|
||||
from app.models.outlet import Outlet, OutletCreate, OutletUpdate, OutletList
|
||||
from typing import Optional, List
|
||||
from fastapi import HTTPException
|
||||
from bson import ObjectId
|
||||
|
||||
class OutletService:
|
||||
|
||||
@classmethod
|
||||
async def get_all_outlets(cls, category: Optional[str] = None) -> List[dict]:
|
||||
"""Get all outlets, optionally filtered by category"""
|
||||
db = get_database()
|
||||
collection = db.outlets
|
||||
|
||||
query = {}
|
||||
if category:
|
||||
if category not in ['people', 'topics', 'companies']:
|
||||
raise HTTPException(status_code=400, detail=f"Invalid category: {category}. Must be one of: people, topics, companies")
|
||||
query['category'] = category
|
||||
|
||||
cursor = collection.find(query)
|
||||
outlets = await cursor.to_list(length=None)
|
||||
|
||||
# Convert _id to string
|
||||
for outlet in outlets:
|
||||
outlet['_id'] = str(outlet['_id'])
|
||||
|
||||
return outlets
|
||||
|
||||
@classmethod
|
||||
async def get_outlet_by_id(cls, outlet_id: str) -> dict:
|
||||
"""Get specific outlet by ID (_id)"""
|
||||
db = get_database()
|
||||
collection = db.outlets
|
||||
|
||||
try:
|
||||
outlet = await collection.find_one({"_id": ObjectId(outlet_id)})
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=f"Invalid outlet ID: {outlet_id}")
|
||||
|
||||
if not outlet:
|
||||
raise HTTPException(status_code=404, detail=f"Outlet not found: {outlet_id}")
|
||||
|
||||
# Convert _id to string
|
||||
outlet['_id'] = str(outlet['_id'])
|
||||
return outlet
|
||||
|
||||
@classmethod
|
||||
async def create_outlet(cls, outlet_data: OutletCreate) -> Outlet:
|
||||
"""Create a new outlet"""
|
||||
db = get_database()
|
||||
collection = db.outlets
|
||||
|
||||
# Check if outlet with this ID already exists
|
||||
existing = await collection.find_one({"id": outlet_data.id})
|
||||
if existing:
|
||||
raise HTTPException(status_code=400, detail=f"Outlet with ID {outlet_data.id} already exists")
|
||||
|
||||
outlet_dict = outlet_data.model_dump()
|
||||
await collection.insert_one(outlet_dict)
|
||||
|
||||
return Outlet(**outlet_dict)
|
||||
|
||||
@classmethod
|
||||
async def update_outlet(cls, outlet_id: str, outlet_data: OutletUpdate) -> Outlet:
|
||||
"""Update an existing outlet"""
|
||||
db = get_database()
|
||||
collection = db.outlets
|
||||
|
||||
# Check if outlet exists
|
||||
existing = await collection.find_one({"id": outlet_id})
|
||||
if not existing:
|
||||
raise HTTPException(status_code=404, detail=f"Outlet not found: {outlet_id}")
|
||||
|
||||
# Only update fields that are provided
|
||||
update_data = outlet_data.model_dump(exclude_unset=True)
|
||||
|
||||
if update_data:
|
||||
await collection.update_one(
|
||||
{"id": outlet_id},
|
||||
{"$set": update_data}
|
||||
)
|
||||
|
||||
# Return updated outlet
|
||||
updated = await collection.find_one({"id": outlet_id}, {"_id": 0})
|
||||
return Outlet(**updated)
|
||||
|
||||
@classmethod
|
||||
async def delete_outlet(cls, outlet_id: str) -> bool:
|
||||
"""Delete an outlet"""
|
||||
db = get_database()
|
||||
collection = db.outlets
|
||||
|
||||
result = await collection.delete_one({"id": outlet_id})
|
||||
|
||||
if result.deleted_count == 0:
|
||||
raise HTTPException(status_code=404, detail=f"Outlet not found: {outlet_id}")
|
||||
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
async def get_count(cls, category: Optional[str] = None) -> int:
|
||||
"""Get total count of outlets"""
|
||||
db = get_database()
|
||||
collection = db.outlets
|
||||
|
||||
query = {}
|
||||
if category:
|
||||
query['category'] = category
|
||||
|
||||
return await collection.count_documents(query)
|
||||
Reference in New Issue
Block a user