a43ddb4b63
## 主要变更 ### 1. 清理废弃代码(18个文件删除) - 删除 TaskModel/MilestoneModel/TaskIssueModel 及相关文件 - 删除 ProjectTitleModel/EditPlanModel/EditPlanClipModel 及相关文件 - 清理 domain/ports/adapters/application/api 各层引用 - 从 GenerationTaskModel 移除 edit_plan_id 字段 ### 2. 新建标题库 API(/api/v1/titles) - Domain: TitleLibraryItem 数据类 - Ports: TitleLibraryRepository 接口 - Adapters: SQLAlchemy 实现(软删除) - Application: CRUD Use Cases + 配额检查(max_titles: free=50, basic=500, premium=500) - API: GET/POST/PUT/DELETE 端点 - Schema: Pydantic 请求/响应模型 ### 3. 新建配音库 API(/api/v1/voices) - Domain: VoiceLibraryItem 数据类 - Ports: VoiceLibraryRepository 接口 - Adapters: SQLAlchemy 实现(软删除) - Application: CRUD Use Cases + 配额检查(max_voiceovers: free=10, basic=100, premium=100) - API: GET/POST/PUT/DELETE 端点 - Schema: Pydantic 请求/响应模型 ### 4. 去掉 Project 层依赖 - 修复 authenticated_user.id → authenticated_user.user.id bug - asset_libraries.py: project_id 改为可选查询参数 - generated_videos.py: project_id 改为可选查询参数 - 无 project_id 时通过 find_accessible_projects 获取用户可访问的所有项目 ### 5. 数据库迁移 - 创建 011_phase1_core_refactor.py - 删除 6 个废弃表:tasks, milestones, task_issues, project_titles, edit_plans, edit_plan_clips - 从 generation_tasks 表删除 edit_plan_id 列 ### 6. 其他改进 - 迁移 EditingMode 到独立模块 packages/domain/editing_mode.py - 注册 titles_router 和 voices_router - 添加 get_title_library_repository 和 get_voice_library_repository 依赖 - 更新 domain/ports __init__.py 导出新实体和仓储接口 ## 技术细节 - 遵循六边形架构模式 - 配额检查通过 QuotaRegistry 实现 - 软删除:标题库用 is_active=False,配音库用 status='deleted' - 配音库支持可选的 project_id 关联 ## 破坏性变更 - 删除 6 个废弃表(需先备份数据) - 删除 /api/v1/edit-plans, /api/v1/project-titles, /api/v1/project-management 端点 - generation_tasks API 不再包含 edit_plan_id 字段
160 lines
5.9 KiB
Python
160 lines
5.9 KiB
Python
"""Dependency injection providers for FastAPI endpoints.
|
|
|
|
All repository and service factories are defined here as FastAPI dependencies,
|
|
ensuring proper lifecycle management and testability.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import redis
|
|
from typing import Generator
|
|
|
|
from app.config import settings
|
|
from fastapi import Depends
|
|
from sqlalchemy.orm import Session
|
|
|
|
from packages.adapters.redis import NoopSessionStore, SessionStore
|
|
from packages.adapters.smtp import EmailConfig, NoopEmailService, get_email_service
|
|
from packages.adapters.sqlalchemy_impl.asset_library_repository import (
|
|
SQLAlchemyAssetLibraryRepository,
|
|
)
|
|
from packages.adapters.sqlalchemy_impl.asset_repository import SQLAlchemyAssetRepository
|
|
from packages.adapters.sqlalchemy_impl.classification_job_repository import (
|
|
SQLAlchemyClassificationJobRepository,
|
|
)
|
|
from packages.adapters.sqlalchemy_impl.generated_video_repository import (
|
|
SQLAlchemyGeneratedVideoRepository,
|
|
)
|
|
from packages.adapters.sqlalchemy_impl.generation_task_repository import (
|
|
SQLAlchemyGenerationTaskRepository,
|
|
)
|
|
from packages.adapters.sqlalchemy_impl.title_library_repository import (
|
|
SQLAlchemyTitleLibraryRepository,
|
|
)
|
|
from packages.adapters.sqlalchemy_impl.voice_library_repository import (
|
|
SQLAlchemyVoiceLibraryRepository,
|
|
)
|
|
from packages.adapters.sqlalchemy_impl.ingest_job_repository import (
|
|
SQLAlchemyIngestJobRepository,
|
|
)
|
|
from packages.adapters.sqlalchemy_impl.project_repository import (
|
|
SQLAlchemyProjectRepository,
|
|
)
|
|
from packages.adapters.sqlalchemy_impl.session import build_session_factory
|
|
from packages.adapters.sqlalchemy_impl.user_repository import SQLAlchemyUserRepository
|
|
from packages.ports.asset_repository import AssetRepository
|
|
from packages.ports.asset_library_repository import AssetLibraryRepository
|
|
from packages.ports.user_repository import UserRepository
|
|
from packages.ports.classification_job_repository import ClassificationJobRepository
|
|
from packages.ports.generation_task_repository import GenerationTaskRepository
|
|
from packages.ports.title_library_repository import TitleLibraryRepository
|
|
from packages.ports.voice_library_repository import VoiceLibraryRepository
|
|
from packages.ports.generated_video_repository import GeneratedVideoRepository
|
|
from packages.ports.ingest_job_repository import IngestJobRepository
|
|
from packages.ports.project_repository import ProjectRepository
|
|
|
|
_engine, _SessionLocal = build_session_factory(settings.DATABASE_URL)
|
|
|
|
|
|
def get_db_session() -> Generator[Session, None, None]:
|
|
"""Provide a database session with automatic cleanup."""
|
|
session: Session = _SessionLocal()
|
|
try:
|
|
yield session
|
|
finally:
|
|
session.close()
|
|
|
|
|
|
def get_asset_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> SQLAlchemyAssetRepository:
|
|
"""Provide the SQLAlchemy asset repository implementation."""
|
|
return SQLAlchemyAssetRepository(session)
|
|
|
|
|
|
def get_asset_library_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> SQLAlchemyAssetLibraryRepository:
|
|
"""Provide the SQLAlchemy asset library repository implementation."""
|
|
return SQLAlchemyAssetLibraryRepository(session)
|
|
|
|
|
|
def get_ingest_job_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> SQLAlchemyIngestJobRepository:
|
|
"""Provide the SQLAlchemy ingest job repository implementation."""
|
|
return SQLAlchemyIngestJobRepository(session)
|
|
|
|
|
|
def get_classification_job_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> SQLAlchemyClassificationJobRepository:
|
|
"""Provide the SQLAlchemy classification job repository implementation."""
|
|
return SQLAlchemyClassificationJobRepository(session)
|
|
|
|
|
|
def get_generation_task_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> SQLAlchemyGenerationTaskRepository:
|
|
"""Provide the SQLAlchemy generation task repository implementation."""
|
|
return SQLAlchemyGenerationTaskRepository(session)
|
|
|
|
|
|
def get_generated_video_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> SQLAlchemyGeneratedVideoRepository:
|
|
"""Provide the SQLAlchemy generated video repository implementation."""
|
|
return SQLAlchemyGeneratedVideoRepository(session)
|
|
|
|
|
|
def get_project_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> SQLAlchemyProjectRepository:
|
|
"""Provide the SQLAlchemy project repository implementation."""
|
|
return SQLAlchemyProjectRepository(session)
|
|
|
|
|
|
|
|
def get_user_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> UserRepository:
|
|
"""Provide the SQLAlchemy user repository implementation."""
|
|
return SQLAlchemyUserRepository(session)
|
|
|
|
|
|
def get_auth_session_store() -> SessionStore | NoopSessionStore:
|
|
"""Provide the session store based on configuration."""
|
|
if not settings.ENABLE_REDIS_SESSIONS:
|
|
return NoopSessionStore()
|
|
return SessionStore(redis_client=redis.from_url(settings.REDIS_URL, decode_responses=True))
|
|
|
|
|
|
def get_auth_email_service() -> NoopEmailService | EmailService:
|
|
"""Provide the email service based on configuration."""
|
|
if not settings.ENABLE_EMAIL_DELIVERY:
|
|
return NoopEmailService()
|
|
return get_email_service(
|
|
EmailConfig(
|
|
smtp_host=settings.SMTP_HOST,
|
|
smtp_port=settings.SMTP_PORT,
|
|
smtp_user=settings.SMTP_USER,
|
|
smtp_password=settings.SMTP_PASSWORD,
|
|
from_email=settings.SMTP_FROM_EMAIL,
|
|
from_name=settings.SMTP_FROM_NAME,
|
|
use_tls=settings.SMTP_USE_TLS,
|
|
),
|
|
enabled=True,
|
|
)
|
|
|
|
def get_title_library_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> SQLAlchemyTitleLibraryRepository:
|
|
"""Provide the SQLAlchemy title library repository implementation."""
|
|
return SQLAlchemyTitleLibraryRepository(session)
|
|
|
|
|
|
def get_voice_library_repository(
|
|
session: Session = Depends(get_db_session),
|
|
) -> SQLAlchemyVoiceLibraryRepository:
|
|
"""Provide the SQLAlchemy voice library repository implementation."""
|
|
return SQLAlchemyVoiceLibraryRepository(session)
|