添加推荐问题修改接口
This commit is contained in:
1 parent
62292500ba
commit
2f2ba44c7c
13 files changed
+481
-126
No files matched your search
+44
@@ -0,0 +1,44 @@
|
||||
"""Create recommended_questions table.
|
||||
|
||||
Revision ID: 20260514_01
|
||||
Revises: 20260513_02
|
||||
Create Date: 2026-05-14
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
from sqlalchemy import inspect
|
||||
|
||||
|
||||
revision = "20260514_01"
|
||||
down_revision = "20260513_02"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def _has_table(table_name: str) -> bool:
|
||||
inspector = inspect(op.get_bind())
|
||||
return table_name in inspector.get_table_names()
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
if _has_table("recommended_questions"):
|
||||
return
|
||||
|
||||
op.create_table(
|
||||
"recommended_questions",
|
||||
sa.Column("id", sa.String(64), primary_key=True),
|
||||
sa.Column("title", sa.String(256), nullable=False),
|
||||
sa.Column("content", sa.Text(), nullable=False),
|
||||
sa.Column("sort_order", sa.Integer(), nullable=False, server_default="0"),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
|
||||
)
|
||||
op.create_index("ix_recommended_questions_sort_order", "recommended_questions", ["sort_order"])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
if _has_table("recommended_questions"):
|
||||
op.drop_table("recommended_questions")
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
"""Recommended questions persistence."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from deerflow.persistence.recommended_questions.model import RecommendedQuestionRow
|
||||
from deerflow.persistence.recommended_questions.sql import RecommendedQuestionRepository
|
||||
|
||||
__all__ = [
|
||||
"RecommendedQuestionRepository",
|
||||
"RecommendedQuestionRow",
|
||||
"make_recommended_question_store",
|
||||
]
|
||||
|
||||
|
||||
def make_recommended_question_store(session_factory):
|
||||
if session_factory is None:
|
||||
return None
|
||||
return RecommendedQuestionRepository(session_factory)
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
"""ORM model for recommended questions."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from sqlalchemy import DateTime, Integer, String, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from deerflow.persistence.base import Base
|
||||
|
||||
|
||||
class RecommendedQuestionRow(Base):
|
||||
__tablename__ = "recommended_questions"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
title: Mapped[str] = mapped_column(String(256), nullable=False)
|
||||
content: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
sort_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0, index=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, default=lambda: datetime.now(UTC))
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, default=lambda: datetime.now(UTC), onupdate=lambda: datetime.now(UTC))
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
"""SQLAlchemy-backed recommended questions repository."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from deerflow.persistence.recommended_questions.model import RecommendedQuestionRow
|
||||
|
||||
|
||||
def _row_to_dict(row: RecommendedQuestionRow) -> dict[str, Any]:
|
||||
data = row.to_dict()
|
||||
for key in ("created_at", "updated_at"):
|
||||
value = data.get(key)
|
||||
if isinstance(value, datetime):
|
||||
data[key] = value.isoformat()
|
||||
return data
|
||||
|
||||
|
||||
class RecommendedQuestionRepository:
|
||||
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
|
||||
self._sf = session_factory
|
||||
|
||||
async def list_all(self) -> list[dict[str, Any]]:
|
||||
stmt = select(RecommendedQuestionRow).order_by(RecommendedQuestionRow.sort_order.asc(), RecommendedQuestionRow.created_at.asc())
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(stmt)
|
||||
return [_row_to_dict(row) for row in result.scalars()]
|
||||
|
||||
async def get(self, question_id: str) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(RecommendedQuestionRow, question_id)
|
||||
return _row_to_dict(row) if row is not None else None
|
||||
|
||||
async def create(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
now = datetime.now(UTC)
|
||||
row = RecommendedQuestionRow(
|
||||
id=data["id"],
|
||||
title=data["title"],
|
||||
content=data["content"],
|
||||
sort_order=data.get("sort_order", 0),
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
async with self._sf() as session:
|
||||
session.add(row)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return _row_to_dict(row)
|
||||
|
||||
async def update(self, question_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(RecommendedQuestionRow, question_id)
|
||||
if row is None:
|
||||
return None
|
||||
for key in ("title", "content", "sort_order"):
|
||||
if key in data:
|
||||
setattr(row, key, data[key])
|
||||
row.updated_at = datetime.now(UTC)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return _row_to_dict(row)
|
||||
|
||||
async def delete(self, question_id: str) -> bool:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(RecommendedQuestionRow, question_id)
|
||||
if row is None:
|
||||
return False
|
||||
await session.delete(row)
|
||||
await session.commit()
|
||||
return True
|
||||
|
||||
async def reorder(self, ordered_ids: list[str]) -> list[dict[str, Any]]:
|
||||
async with self._sf() as session:
|
||||
now = datetime.now(UTC)
|
||||
for index, question_id in enumerate(ordered_ids):
|
||||
row = await session.get(RecommendedQuestionRow, question_id)
|
||||
if row is not None:
|
||||
row.sort_order = index
|
||||
row.updated_at = now
|
||||
await session.commit()
|
||||
return await self.list_all()
|
||||
Reference in new issue
Block a user