Files
skill-template/content-manager/content_manager/db/articles_repository.py
2026-04-04 10:35:02 +08:00

123 lines
3.0 KiB
Python

"""articles 表:仅负责 SQL 读写,不含业务规则。"""
from __future__ import annotations
import sqlite3
from typing import Any, List, Optional, Tuple
def insert_article(
conn: sqlite3.Connection,
title: str,
body: str,
content_html: Optional[str],
status: str,
source: str,
account_id: Optional[str],
error_msg: Optional[str],
llm_target: Optional[str],
extra_json: Optional[str],
created_at: int,
updated_at: int,
) -> int:
cur = conn.cursor()
cur.execute(
"""
INSERT INTO articles (
title, body, content_html, status, source, account_id, error_msg, llm_target, extra_json,
created_at, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
title,
body,
content_html,
status,
source,
account_id,
error_msg,
llm_target,
extra_json,
created_at,
updated_at,
),
)
return int(cur.lastrowid)
def update_article_body(
conn: sqlite3.Connection,
article_id: int,
title: str,
body: str,
updated_at: int,
) -> None:
cur = conn.cursor()
cur.execute(
"""
UPDATE articles SET title = ?, body = ?, updated_at = ?
WHERE id = ?
""",
(title, body, updated_at, article_id),
)
def fetch_by_id(conn: sqlite3.Connection, article_id: int) -> Optional[Tuple[Any, ...]]:
cur = conn.cursor()
cur.execute(
"""
SELECT id, title, body, content_html, status, source, account_id, error_msg,
llm_target, extra_json, created_at, updated_at
FROM articles WHERE id = ?
""",
(article_id,),
)
return cur.fetchone()
def exists_id(conn: sqlite3.Connection, article_id: int) -> bool:
cur = conn.cursor()
cur.execute("SELECT id FROM articles WHERE id = ?", (article_id,))
return cur.fetchone() is not None
def list_recent(conn: sqlite3.Connection, limit: int) -> List[Tuple[Any, ...]]:
cur = conn.cursor()
cur.execute(
"""
SELECT
id, title, body, content_html,
status, source, account_id, error_msg, llm_target, extra_json,
created_at, updated_at
FROM articles ORDER BY updated_at DESC, id DESC
LIMIT ?
""",
(int(limit),),
)
return list(cur.fetchall())
def delete_by_id(conn: sqlite3.Connection, article_id: int) -> int:
cur = conn.cursor()
cur.execute("DELETE FROM articles WHERE id = ?", (article_id,))
return int(cur.rowcount)
def update_feedback(
conn: sqlite3.Connection,
article_id: int,
status: str,
account_id: Optional[str],
error_msg: Optional[str],
updated_at: int,
) -> None:
cur = conn.cursor()
cur.execute(
"""
UPDATE articles
SET status = ?, account_id = ?, error_msg = ?, updated_at = ?
WHERE id = ?
""",
(status, account_id, error_msg, updated_at, article_id),
)