初始化提交
This commit is contained in:
commit
887c1f449e
963 files changed
+166869
No files matched your search
@@ -0,0 +1,44 @@
|
||||
"""演化候选数据访问。"""
|
||||
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.models.evolution import EvolutionCandidate
|
||||
|
||||
|
||||
async def add_candidates(session: AsyncSession, candidates: list[EvolutionCandidate]) -> None:
|
||||
session.add_all(candidates)
|
||||
await session.flush()
|
||||
|
||||
|
||||
async def get_candidate(session: AsyncSession, candidate_id: str) -> EvolutionCandidate | None:
|
||||
return await session.get(EvolutionCandidate, candidate_id)
|
||||
|
||||
|
||||
async def list_candidates(
|
||||
session: AsyncSession,
|
||||
character_id: str,
|
||||
*,
|
||||
status: str | None = None,
|
||||
candidate_type: str | None = None,
|
||||
page: int = 1,
|
||||
page_size: int = 20,
|
||||
) -> tuple[list[EvolutionCandidate], int]:
|
||||
conditions = [EvolutionCandidate.character_id == character_id]
|
||||
if status is not None:
|
||||
conditions.append(EvolutionCandidate.status == status)
|
||||
if candidate_type is not None:
|
||||
conditions.append(EvolutionCandidate.type == candidate_type)
|
||||
total = (
|
||||
await session.execute(
|
||||
select(func.count()).select_from(EvolutionCandidate).where(*conditions)
|
||||
)
|
||||
).scalar_one()
|
||||
items_result = await session.execute(
|
||||
select(EvolutionCandidate)
|
||||
.where(*conditions)
|
||||
.order_by(EvolutionCandidate.created_at.desc())
|
||||
.offset(max(page - 1, 0) * page_size)
|
||||
.limit(page_size)
|
||||
)
|
||||
return list(items_result.scalars().all()), int(total or 0)
|
||||
Reference in new issue
Block a user