67a1ed6430
CI/CD Pipeline / Check if frontend-only change (push) Has been skipped
CI/CD Pipeline / Check push changed paths (push) Successful in 2s
CI/CD Pipeline / PR Build API Image (push) Has been skipped
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (push) Successful in 2s
CI/CD Pipeline / PR Build Web Image (push) Has been skipped
CI/CD Pipeline / PR Build Worker Image (push) Has been skipped
CI/CD Pipeline / Frontend Lint (push) Has been skipped
CI/CD Pipeline / Build Staging Web Image (push) Has been skipped
CI/CD Pipeline / Build Staging API Image (push) Successful in 37s
CI/CD Pipeline / Build Staging Worker Image (push) Successful in 38s
CI/CD Pipeline / Retag skipped Staging API Image (push) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker Image (push) Has been skipped
CI/CD Pipeline / Retag skipped Staging Web Image (push) Successful in 20s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Successful in 43s
CI/CD Pipeline / Validate - Python (mypy + alembic) (push) Successful in 3m3s
CI/CD Pipeline / Integration Tests (push) Successful in 3m7s
CI/CD Pipeline / Staging E2E Tests (push) Successful in 1m40s
CI/CD Pipeline / Validate - Style (push) Successful in 3m52s
CI/CD Pipeline / Frontend Unit Tests (push) Successful in 4m17s
CI/CD Pipeline / ACR Image Cleanup (push) Successful in 3m6s
CI/CD Pipeline / Validate - Security (push) Successful in 7m14s
CI/CD Pipeline / Unit Tests (push) Successful in 9m33s
CI/CD Pipeline / Build Production API Image (push) Has been skipped
CI/CD Pipeline / Build Production Web Image (push) Has been skipped
CI/CD Pipeline / Build Production Worker Image (push) Has been skipped
CI/CD Pipeline / CI Gate (push) Has been skipped
CI/CD Pipeline / Deploy Production (push) Has been skipped
CI/CD Pipeline / Production Browser E2E (push) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (push) Successful in 35m54s
CI/CD Pipeline / Canary Release to Production (push) Has been skipped
Co-authored-by: xiaoxia <dev@xiaoxiajianji.com> Co-committed-by: xiaoxia <dev@xiaoxiajianji.com>
110 lines
3.9 KiB
Python
110 lines
3.9 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,
|
|
) -> 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 [],
|
|
)
|
|
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,
|
|
) -> 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
|
|
script.updated_at = datetime.now(UTC)
|
|
self.db.commit()
|
|
self.db.refresh(script)
|
|
return script
|
|
|
|
# ── 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
|