- 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>
54 lines
1.5 KiB
Python
54 lines
1.5 KiB
Python
from app.core.database import get_database
|
|
from app.models.comment import Comment, CommentCreate, CommentList
|
|
from datetime import datetime
|
|
from typing import List
|
|
import uuid
|
|
|
|
class CommentService:
|
|
|
|
@classmethod
|
|
async def create_comment(cls, comment_data: CommentCreate) -> Comment:
|
|
"""Create a new comment"""
|
|
db = get_database()
|
|
collection = db.comments
|
|
|
|
comment_dict = {
|
|
"id": str(uuid.uuid4()),
|
|
"articleId": comment_data.articleId,
|
|
"nickname": comment_data.nickname,
|
|
"content": comment_data.content,
|
|
"createdAt": datetime.utcnow()
|
|
}
|
|
|
|
await collection.insert_one(comment_dict)
|
|
|
|
return Comment(**comment_dict)
|
|
|
|
@classmethod
|
|
async def get_comments_by_article(cls, article_id: str) -> CommentList:
|
|
"""Get all comments for an article"""
|
|
db = get_database()
|
|
collection = db.comments
|
|
|
|
cursor = collection.find(
|
|
{"articleId": article_id},
|
|
{"_id": 0}
|
|
).sort("createdAt", -1)
|
|
|
|
comments = await cursor.to_list(length=None)
|
|
total = len(comments)
|
|
|
|
return CommentList(
|
|
comments=[Comment(**comment) for comment in comments],
|
|
total=total
|
|
)
|
|
|
|
@classmethod
|
|
async def get_comment_count(cls, article_id: str) -> int:
|
|
"""Get comment count for an article"""
|
|
db = get_database()
|
|
collection = db.comments
|
|
|
|
count = await collection.count_documents({"articleId": article_id})
|
|
return count
|