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 字段
126 lines
4.3 KiB
Python
126 lines
4.3 KiB
Python
"""SQLAlchemy implementation of VoiceLibraryRepository."""
|
|
from __future__ import annotations
|
|
|
|
from typing import List, Optional
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from packages.adapters.sqlalchemy_impl.models import VoiceLibraryModel
|
|
from packages.domain.voice_library import VoiceLibraryItem
|
|
|
|
|
|
class SQLAlchemyVoiceLibraryRepository:
|
|
"""SQLAlchemy 配音库仓储"""
|
|
|
|
def __init__(self, session: Session) -> None:
|
|
self.session = session
|
|
|
|
def list_by_user(
|
|
self,
|
|
user_id: str,
|
|
*,
|
|
status: Optional[str] = None,
|
|
skip: int = 0,
|
|
limit: int = 50,
|
|
) -> List[VoiceLibraryItem]:
|
|
query = self.session.query(VoiceLibraryModel).filter(
|
|
VoiceLibraryModel.user_id == user_id,
|
|
)
|
|
if status:
|
|
query = query.filter(VoiceLibraryModel.status == status)
|
|
query = query.order_by(VoiceLibraryModel.created_at.desc())
|
|
models = query.offset(skip).limit(limit).all()
|
|
return [self._model_to_entity(m) for m in models]
|
|
|
|
def get(self, voice_id: str, user_id: str) -> Optional[VoiceLibraryItem]:
|
|
model = self.session.query(VoiceLibraryModel).filter(
|
|
VoiceLibraryModel.id == voice_id,
|
|
VoiceLibraryModel.user_id == user_id,
|
|
).first()
|
|
if model is None:
|
|
return None
|
|
return self._model_to_entity(model)
|
|
|
|
def create(self, item: VoiceLibraryItem) -> VoiceLibraryItem:
|
|
model = VoiceLibraryModel(
|
|
id=item.id,
|
|
user_id=item.user_id,
|
|
project_id=item.project_id or "",
|
|
name=item.name,
|
|
text=item.text,
|
|
voice_provider=item.voice_provider,
|
|
voice_id=item.voice_id,
|
|
voice_name=item.voice_name,
|
|
audio_url=item.audio_url,
|
|
duration=item.duration,
|
|
file_size=item.file_size,
|
|
status=item.status,
|
|
tags=item.tags,
|
|
metadata=item.metadata_,
|
|
)
|
|
self.session.add(model)
|
|
self.session.commit()
|
|
self.session.refresh(model)
|
|
return self._model_to_entity(model)
|
|
|
|
def update(self, item: VoiceLibraryItem) -> VoiceLibraryItem:
|
|
model = self.session.query(VoiceLibraryModel).filter(
|
|
VoiceLibraryModel.id == item.id,
|
|
VoiceLibraryModel.user_id == item.user_id,
|
|
).first()
|
|
if model is None:
|
|
raise ValueError(f"VoiceLibraryItem {item.id} not found")
|
|
model.name = item.name
|
|
model.text = item.text
|
|
model.voice_provider = item.voice_provider
|
|
model.voice_id = item.voice_id
|
|
model.voice_name = item.voice_name
|
|
model.audio_url = item.audio_url
|
|
model.duration = item.duration
|
|
model.file_size = item.file_size
|
|
model.status = item.status
|
|
model.tags = item.tags
|
|
model.metadata = item.metadata_
|
|
self.session.commit()
|
|
self.session.refresh(model)
|
|
return self._model_to_entity(model)
|
|
|
|
def delete(self, voice_id: str, user_id: str) -> bool:
|
|
model = self.session.query(VoiceLibraryModel).filter(
|
|
VoiceLibraryModel.id == voice_id,
|
|
VoiceLibraryModel.user_id == user_id,
|
|
).first()
|
|
if model is None:
|
|
return False
|
|
# Soft delete by setting status to deleted
|
|
model.status = "deleted"
|
|
self.session.commit()
|
|
return True
|
|
|
|
def count_by_user(self, user_id: str) -> int:
|
|
return self.session.query(VoiceLibraryModel).filter(
|
|
VoiceLibraryModel.user_id == user_id,
|
|
VoiceLibraryModel.status != "deleted",
|
|
).count()
|
|
|
|
@staticmethod
|
|
def _model_to_entity(model: VoiceLibraryModel) -> VoiceLibraryItem:
|
|
return VoiceLibraryItem(
|
|
id=model.id,
|
|
user_id=model.user_id,
|
|
name=model.name,
|
|
text=model.text,
|
|
voice_provider=model.voice_provider,
|
|
voice_id=model.voice_id,
|
|
voice_name=model.voice_name,
|
|
audio_url=model.audio_url,
|
|
duration=model.duration or 0,
|
|
file_size=model.file_size or 0,
|
|
status=model.status,
|
|
project_id=model.project_id if model.project_id else None,
|
|
tags=model.tags or [],
|
|
metadata_=model.metadata or {},
|
|
created_at=model.created_at,
|
|
updated_at=model.updated_at,
|
|
)
|