24b28bfc89
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (pull_request) Successful in 1s
CI/CD Pipeline / Check push changed paths (pull_request) Has been skipped
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 1s
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 17s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 51s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 1m5s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m44s
CI/CD Pipeline / Validate - Style (pull_request) Successful in 1m58s
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Successful in 1m59s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 2m59s
CI/CD Pipeline / Unit Tests (pull_request) Failing after 6m16s
AI Code Review / AI Code Review (pull_request) Successful in 6m23s
CI/CD Pipeline / Validate - Security (pull_request) Has been cancelled
CI/CD Pipeline / Build Production API Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Web Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Production (pull_request) Has been cancelled
CI/CD Pipeline / Production Browser E2E (pull_request) Has been cancelled
CI/CD Pipeline / Canary Release to Production (pull_request) Has been cancelled
CI/CD Pipeline / CI Gate (pull_request) Has been cancelled
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 5m28s
- 新建 ScriptModel (packages/adapters/sqlalchemy_impl/models.py) 字段: id, user_id(indexed), title, content, segments(JSON), tags(JSON), created_at, updated_at - 新建 script_service.py: CRUD 封装,用户隔离,tag 筛选,分页 - 新建 schemas/script.py: Pydantic request/response schemas - 新建 routes/scripts.py: RESTful API (GET/POST/PUT/DELETE /api/v1/scripts) - 新建 alembic 070_add_scripts_table.py: scripts 表 + user_id 索引 - 注册路由到 router.py (prefix=/scripts, tag=ScriptLibrary) - 35 单元测试: service 15 + schema/route 20
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 datetime, timezone
|
|
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(timezone.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
|