feat: Implement backend core functionality for news-engine-console

Phase 1 Backend Implementation:
-  MongoDB data models (Keyword, Pipeline, User, Application)
-  Pydantic schemas for all models with validation
-  KeywordService: Full CRUD, filtering, pagination, stats, toggle status
-  PipelineService: Full CRUD, start/stop/restart, logs, config management
-  Keywords API: 8 endpoints with complete functionality
-  Pipelines API: 11 endpoints with complete functionality
-  Updated TODO.md to reflect completion

Key Features:
- Async MongoDB operations with Motor
- Comprehensive filtering and pagination support
- Pipeline logging system
- Statistics tracking for keywords and pipelines
- Proper error handling with HTTP status codes
- Type-safe request/response models

Files Added:
- models/: 4 data models with PyObjectId support
- schemas/: 4 schema modules with Create/Update/Response patterns
- services/: KeywordService (234 lines) + PipelineService (332 lines)

Files Modified:
- api/keywords.py: 40 → 212 lines (complete implementation)
- api/pipelines.py: 25 → 300 lines (complete implementation)
- TODO.md: Updated checklist with completed items

Next Steps:
- UserService with authentication
- ApplicationService for OAuth2
- MonitoringService
- Redis integration
- Frontend implementation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
jungwoo choi
2025-11-04 16:24:14 +09:00
parent 7649844023
commit 07088e60e9
16 changed files with 1572 additions and 39 deletions

View File

@ -0,0 +1,44 @@
# Pydantic Schemas for Request/Response
from .keyword import (
KeywordCreate,
KeywordUpdate,
KeywordResponse,
KeywordListResponse
)
from .pipeline import (
PipelineCreate,
PipelineUpdate,
PipelineResponse,
PipelineListResponse
)
from .user import (
UserCreate,
UserUpdate,
UserResponse,
UserLogin,
Token
)
from .application import (
ApplicationCreate,
ApplicationUpdate,
ApplicationResponse
)
__all__ = [
"KeywordCreate",
"KeywordUpdate",
"KeywordResponse",
"KeywordListResponse",
"PipelineCreate",
"PipelineUpdate",
"PipelineResponse",
"PipelineListResponse",
"UserCreate",
"UserUpdate",
"UserResponse",
"UserLogin",
"Token",
"ApplicationCreate",
"ApplicationUpdate",
"ApplicationResponse",
]

View File

@ -0,0 +1,44 @@
from datetime import datetime
from typing import Optional, List
from pydantic import BaseModel, Field
class ApplicationBase(BaseModel):
"""Base application schema"""
name: str = Field(..., min_length=1, max_length=100)
redirect_uris: List[str] = Field(default_factory=list)
grant_types: List[str] = Field(
default_factory=lambda: ["authorization_code", "refresh_token"]
)
scopes: List[str] = Field(default_factory=lambda: ["read", "write"])
class ApplicationCreate(ApplicationBase):
"""Schema for creating a new application"""
pass
class ApplicationUpdate(BaseModel):
"""Schema for updating an application (all fields optional)"""
name: Optional[str] = Field(None, min_length=1, max_length=100)
redirect_uris: Optional[List[str]] = None
grant_types: Optional[List[str]] = None
scopes: Optional[List[str]] = None
class ApplicationResponse(ApplicationBase):
"""Schema for application response"""
id: str = Field(..., alias="_id")
client_id: str
owner_id: str
created_at: datetime
updated_at: datetime
class Config:
populate_by_name = True
from_attributes = True
class ApplicationWithSecret(ApplicationResponse):
"""Schema for application response with client secret (only on creation)"""
client_secret: str = Field(..., description="Plain text client secret (only shown once)")

View File

@ -0,0 +1,56 @@
from datetime import datetime
from typing import Optional, Dict, Any, List
from pydantic import BaseModel, Field
class KeywordBase(BaseModel):
"""Base keyword schema"""
keyword: str = Field(..., min_length=1, max_length=200)
category: str = Field(..., description="Category: people, topics, companies")
pipeline_type: str = Field(default="all", description="Pipeline type: rss, translation, all")
priority: int = Field(default=5, ge=1, le=10)
metadata: Dict[str, Any] = Field(default_factory=dict)
class KeywordCreate(KeywordBase):
"""Schema for creating a new keyword"""
pass
class KeywordUpdate(BaseModel):
"""Schema for updating a keyword (all fields optional)"""
keyword: Optional[str] = Field(None, min_length=1, max_length=200)
category: Optional[str] = None
status: Optional[str] = Field(None, description="Status: active, inactive")
pipeline_type: Optional[str] = None
priority: Optional[int] = Field(None, ge=1, le=10)
metadata: Optional[Dict[str, Any]] = None
class KeywordResponse(KeywordBase):
"""Schema for keyword response"""
id: str = Field(..., alias="_id")
status: str
created_at: datetime
updated_at: datetime
created_by: Optional[str] = None
class Config:
populate_by_name = True
from_attributes = True
class KeywordStats(BaseModel):
"""Keyword statistics"""
total_articles: int = 0
articles_last_24h: int = 0
articles_last_7d: int = 0
last_article_date: Optional[datetime] = None
class KeywordListResponse(BaseModel):
"""Schema for keyword list response"""
keywords: List[KeywordResponse]
total: int
page: int = 1
page_size: int = 50

View File

@ -0,0 +1,62 @@
from datetime import datetime
from typing import Optional, Dict, Any, List
from pydantic import BaseModel, Field
class PipelineStatsSchema(BaseModel):
"""Pipeline statistics schema"""
total_processed: int = 0
success_count: int = 0
error_count: int = 0
last_run: Optional[datetime] = None
average_duration_seconds: Optional[float] = None
class PipelineBase(BaseModel):
"""Base pipeline schema"""
name: str = Field(..., min_length=1, max_length=100)
type: str = Field(..., description="Type: rss_collector, translator, image_generator")
config: Dict[str, Any] = Field(default_factory=dict)
schedule: Optional[str] = Field(None, description="Cron expression")
class PipelineCreate(PipelineBase):
"""Schema for creating a new pipeline"""
pass
class PipelineUpdate(BaseModel):
"""Schema for updating a pipeline (all fields optional)"""
name: Optional[str] = Field(None, min_length=1, max_length=100)
status: Optional[str] = Field(None, description="Status: running, stopped, error")
config: Optional[Dict[str, Any]] = None
schedule: Optional[str] = None
class PipelineResponse(PipelineBase):
"""Schema for pipeline response"""
id: str = Field(..., alias="_id")
status: str
stats: PipelineStatsSchema
last_run: Optional[datetime] = None
next_run: Optional[datetime] = None
created_at: datetime
updated_at: datetime
class Config:
populate_by_name = True
from_attributes = True
class PipelineListResponse(BaseModel):
"""Schema for pipeline list response"""
pipelines: List[PipelineResponse]
total: int
class PipelineLog(BaseModel):
"""Schema for pipeline log entry"""
timestamp: datetime
level: str = Field(..., description="Log level: INFO, WARNING, ERROR")
message: str
details: Optional[Dict[str, Any]] = None

View File

@ -0,0 +1,55 @@
from datetime import datetime
from typing import Optional
from pydantic import BaseModel, Field, EmailStr
class UserBase(BaseModel):
"""Base user schema"""
username: str = Field(..., min_length=3, max_length=50)
email: EmailStr
full_name: str = Field(..., min_length=1, max_length=100)
role: str = Field(default="viewer", description="Role: admin, editor, viewer")
class UserCreate(UserBase):
"""Schema for creating a new user"""
password: str = Field(..., min_length=8, max_length=100)
class UserUpdate(BaseModel):
"""Schema for updating a user (all fields optional)"""
email: Optional[EmailStr] = None
full_name: Optional[str] = Field(None, min_length=1, max_length=100)
role: Optional[str] = None
disabled: Optional[bool] = None
password: Optional[str] = Field(None, min_length=8, max_length=100)
class UserResponse(UserBase):
"""Schema for user response (without password)"""
id: str = Field(..., alias="_id")
disabled: bool
created_at: datetime
last_login: Optional[datetime] = None
class Config:
populate_by_name = True
from_attributes = True
class UserLogin(BaseModel):
"""Schema for user login"""
username: str
password: str
class Token(BaseModel):
"""Schema for JWT token response"""
access_token: str
token_type: str = "bearer"
expires_in: int
class TokenData(BaseModel):
"""Schema for decoded token data"""
username: Optional[str] = None