7564b50f7e
CI/CD Pipeline / Check push changed paths (push) Successful in 4s
CI/CD Pipeline / Build Staging Worker Image (push) Successful in 1m46s
CI/CD Pipeline / Build Staging Web Image (push) Successful in 1m51s
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (push) Successful in 15m40s
CI/CD Pipeline / Frontend Unit Tests (push) Failing after 1m50s
CI/CD Pipeline / Build Staging API Image (push) Successful in 18m32s
CI/CD Pipeline / Integration Tests (push) Successful in 3m4s
CI/CD Pipeline / Validate - Python (mypy + alembic) (push) Successful in 3m25s
CI/CD Pipeline / Validate - Style (push) Successful in 3m35s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Successful in 44s
CI/CD Pipeline / ACR Image Cleanup (push) Successful in 1m38s
CI/CD Pipeline / Staging API Integration Tests (push) Successful in 2m53s
CI/CD Pipeline / Staging E2E Tests (push) Failing after 3m4s
CI/CD Pipeline / Validate - Security (push) Successful in 8m10s
CI/CD Pipeline / Unit Tests (push) Successful in 9m9s
CI/CD Pipeline / Production Browser E2E (push) Has been skipped
CI/CD Pipeline / Deploy Production (push) Failing after 9h23m26s
CI/CD Pipeline / PR Build Worker Image (push) Failing after 9h48m20s
CI/CD Pipeline / Build Production Worker Image (push) Failing after 9h22m39s
CI/CD Pipeline / Check if frontend-only change (push) Failing after 9h47m32s
CI/CD Pipeline / PR Build Web Image (push) Failing after 9h47m31s
CI/CD Pipeline / PR Build API Image (push) Failing after 9h47m32s
CI/CD Pipeline / Retag skipped Staging API Image (push) Failing after 9h28m53s
CI/CD Pipeline / CI Gate (push) Failing after 9h22m39s
CI/CD Pipeline / Canary Release to Production (push) Failing after 9h22m37s
CI/CD Pipeline / Build Production Web Image (push) Failing after 9h22m39s
CI/CD Pipeline / Build Production API Image (push) Failing after 9h22m39s
CI/CD Pipeline / Retag skipped Staging Worker Image (push) Failing after 9h28m53s
CI/CD Pipeline / Frontend Lint (push) Failing after 9h31m49s
CI/CD Pipeline / Retag skipped Staging Web Image (push) Failing after 10h4m41s
Co-authored-by: xiaoxia <dev@xiaoxiajianji.com> Co-committed-by: xiaoxia <dev@xiaoxiajianji.com>
135 lines
5.1 KiB
Python
135 lines
5.1 KiB
Python
"""ScriptService — Issue #1795 口播文案库 CRUD.
|
|
|
|
纯 Service 层封装,routes 直接调用。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
from datetime import UTC, datetime
|
|
from typing import Optional
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from packages.adapters.sqlalchemy_impl.models import ScriptModel
|
|
|
|
|
|
class ScriptNotFoundError(Exception):
|
|
"""文案不存在或不属于当前用户."""
|
|
|
|
|
|
class ScriptService:
|
|
"""口播文案 CRUD."""
|
|
|
|
def __init__(self, db: Session) -> None:
|
|
self.db = db
|
|
|
|
# ── list ──────────────────────────────────────────────────────────────
|
|
|
|
def list_scripts(
|
|
self,
|
|
user_id: str,
|
|
skip: int = 0,
|
|
limit: int = 50,
|
|
tag: Optional[str] = None,
|
|
) -> tuple[list[ScriptModel], int]:
|
|
"""返回 (items, total)."""
|
|
q = self.db.query(ScriptModel).filter(ScriptModel.user_id == user_id)
|
|
if tag:
|
|
# JSON 数组包含查询
|
|
q = q.filter(ScriptModel.tags.contains([tag]))
|
|
total = q.count()
|
|
items = q.order_by(ScriptModel.created_at.desc()).offset(skip).limit(limit).all()
|
|
return items, total
|
|
|
|
# ── create ────────────────────────────────────────────────────────────
|
|
|
|
def create_script(
|
|
self,
|
|
user_id: str,
|
|
title: str,
|
|
content: str = "",
|
|
segments: list | None = None,
|
|
tags: list | None = None,
|
|
title_text: str = "",
|
|
title_category: str = "",
|
|
title_config: dict | None = None,
|
|
) -> ScriptModel:
|
|
script = ScriptModel(
|
|
id=str(uuid.uuid4()),
|
|
user_id=user_id,
|
|
title=title,
|
|
content=content,
|
|
segments=segments if segments is not None else [],
|
|
tags=tags if tags is not None else [],
|
|
title_text=title_text or "",
|
|
title_category=title_category or "",
|
|
title_config=title_config if title_config is not None else {},
|
|
)
|
|
self.db.add(script)
|
|
self.db.commit()
|
|
self.db.refresh(script)
|
|
return script
|
|
|
|
# ── get ───────────────────────────────────────────────────────────────
|
|
|
|
def get_script(self, script_id: str, user_id: str) -> ScriptModel:
|
|
script = self.db.query(ScriptModel).filter(ScriptModel.id == script_id, ScriptModel.user_id == user_id).first()
|
|
if script is None:
|
|
raise ScriptNotFoundError(f"Script {script_id} not found")
|
|
return script
|
|
|
|
# ── update ────────────────────────────────────────────────────────────
|
|
|
|
def update_script(
|
|
self,
|
|
script_id: str,
|
|
user_id: str,
|
|
title: Optional[str] = None,
|
|
content: Optional[str] = None,
|
|
segments: Optional[list] = None,
|
|
tags: Optional[list] = None,
|
|
title_text: Optional[str] = None,
|
|
title_category: Optional[str] = None,
|
|
title_config: Optional[dict] = None,
|
|
) -> ScriptModel:
|
|
script = self.get_script(script_id, user_id)
|
|
if title is not None:
|
|
script.title = title
|
|
if content is not None:
|
|
script.content = content
|
|
if segments is not None:
|
|
script.segments = segments
|
|
if tags is not None:
|
|
script.tags = tags
|
|
if title_text is not None:
|
|
script.title_text = title_text
|
|
if title_category is not None:
|
|
script.title_category = title_category
|
|
if title_config is not None:
|
|
script.title_config = title_config
|
|
script.updated_at = datetime.now(UTC)
|
|
self.db.commit()
|
|
self.db.refresh(script)
|
|
return script
|
|
|
|
# ── title config ─────────────────────────────────────────────────────
|
|
|
|
def get_title_config_for_script(self, script_id: str, user_id: str) -> dict:
|
|
"""从 script 读取标题配置,返回可直接用于渲染的 title_config dict."""
|
|
script = self.get_script(script_id, user_id)
|
|
config = dict(script.title_config or {})
|
|
if not config.get("text") and script.title_text:
|
|
config["text"] = script.title_text
|
|
return config
|
|
|
|
# ── delete ────────────────────────────────────────────────────────────
|
|
|
|
def delete_script(self, script_id: str, user_id: str) -> bool:
|
|
script = self.db.query(ScriptModel).filter(ScriptModel.id == script_id, ScriptModel.user_id == user_id).first()
|
|
if script is None:
|
|
return False
|
|
self.db.delete(script)
|
|
self.db.commit()
|
|
return True
|