文件版本管理-初步优化
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import uuid
|
||||
from typing import Sequence
|
||||
|
||||
from sqlalchemy import select, update as sa_update
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.models.document_version import DocumentVersion, DocumentVersionStatus
|
||||
|
||||
|
||||
async def create(db: AsyncSession, version: DocumentVersion, *, commit: bool = True) -> DocumentVersion:
|
||||
db.add(version)
|
||||
if commit:
|
||||
await db.commit()
|
||||
await db.refresh(version)
|
||||
return version
|
||||
|
||||
|
||||
async def get(db: AsyncSession, version_id: uuid.UUID) -> DocumentVersion | None:
|
||||
result = await db.execute(select(DocumentVersion).where(DocumentVersion.id == version_id))
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
async def get_by_document_version_no(
|
||||
db: AsyncSession,
|
||||
document_id: uuid.UUID,
|
||||
version_no: str,
|
||||
) -> DocumentVersion | None:
|
||||
result = await db.execute(
|
||||
select(DocumentVersion).where(
|
||||
DocumentVersion.document_id == document_id,
|
||||
DocumentVersion.version_no == version_no,
|
||||
)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
async def list_by_document(
|
||||
db: AsyncSession,
|
||||
document_id: uuid.UUID,
|
||||
skip: int = 0,
|
||||
limit: int = 200,
|
||||
) -> Sequence[DocumentVersion]:
|
||||
result = await db.execute(
|
||||
select(DocumentVersion)
|
||||
.where(DocumentVersion.document_id == document_id)
|
||||
.order_by(DocumentVersion.created_at.desc())
|
||||
.offset(skip)
|
||||
.limit(limit)
|
||||
)
|
||||
return result.scalars().all()
|
||||
|
||||
|
||||
async def list_pending_versions(db: AsyncSession, document_id: uuid.UUID) -> Sequence[DocumentVersion]:
|
||||
result = await db.execute(
|
||||
select(DocumentVersion).where(
|
||||
DocumentVersion.document_id == document_id,
|
||||
DocumentVersion.status.in_(
|
||||
[
|
||||
DocumentVersionStatus.SUBMITTED,
|
||||
DocumentVersionStatus.APPROVED,
|
||||
]
|
||||
),
|
||||
)
|
||||
)
|
||||
return result.scalars().all()
|
||||
|
||||
|
||||
async def update(db: AsyncSession, version_id: uuid.UUID, values: dict, *, commit: bool = True) -> None:
|
||||
if values:
|
||||
await db.execute(
|
||||
sa_update(DocumentVersion)
|
||||
.where(DocumentVersion.id == version_id)
|
||||
.values(**values)
|
||||
)
|
||||
if commit:
|
||||
await db.commit()
|
||||
Reference in New Issue
Block a user