Step 4:评论 / 附件 / 审计日志

This commit is contained in:
Cheng Zhou
2025-12-16 17:02:11 +08:00
parent f3692e1145
commit 31892247bf
69 changed files with 506 additions and 2 deletions
+59
View File
@@ -0,0 +1,59 @@
import uuid
from typing import Sequence
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.attachment import Attachment
async def create_attachment(
db: AsyncSession,
*,
study_id: uuid.UUID,
entity_type: str,
entity_id: uuid.UUID,
filename: str,
file_path: str,
file_size: int,
content_type: str | None,
uploaded_by: uuid.UUID,
) -> Attachment:
attachment = Attachment(
study_id=study_id,
entity_type=entity_type,
entity_id=entity_id,
filename=filename,
file_path=file_path,
file_size=file_size,
content_type=content_type,
uploaded_by=uploaded_by,
)
db.add(attachment)
await db.commit()
await db.refresh(attachment)
return attachment
async def list_attachments(
db: AsyncSession,
study_id: uuid.UUID,
entity_type: str,
entity_id: uuid.UUID,
) -> Sequence[Attachment]:
result = await db.execute(
select(Attachment)
.where(
Attachment.study_id == study_id,
Attachment.entity_type == entity_type,
Attachment.entity_id == entity_id,
Attachment.is_deleted.is_(False),
)
.order_by(Attachment.uploaded_at.desc())
)
return result.scalars().all()
async def get_attachment(db: AsyncSession, attachment_id: uuid.UUID) -> Attachment | None:
result = await db.execute(select(Attachment).where(Attachment.id == attachment_id, Attachment.is_deleted.is_(False)))
return result.scalar_one_or_none()
+55
View File
@@ -0,0 +1,55 @@
import uuid
from typing import Sequence
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.audit_log import AuditLog
async def log_action(
db: AsyncSession,
*,
study_id: uuid.UUID | None,
entity_type: str,
entity_id: uuid.UUID | None,
action: str,
detail: str | None,
operator_id: uuid.UUID,
operator_role: str,
) -> AuditLog:
log = AuditLog(
study_id=study_id,
entity_type=entity_type,
entity_id=entity_id,
action=action,
detail=detail,
operator_id=operator_id,
operator_role=operator_role,
)
db.add(log)
await db.commit()
await db.refresh(log)
return log
async def list_logs(
db: AsyncSession,
*,
study_id: uuid.UUID,
entity_type: str | None = None,
entity_id: uuid.UUID | None = None,
action: str | None = None,
skip: int = 0,
limit: int = 100,
) -> Sequence[AuditLog]:
stmt = select(AuditLog).where(AuditLog.study_id == study_id)
if entity_type:
stmt = stmt.where(AuditLog.entity_type == entity_type)
if entity_id:
stmt = stmt.where(AuditLog.entity_id == entity_id)
if action:
stmt = stmt.where(AuditLog.action == action)
stmt = stmt.order_by(AuditLog.created_at.desc()).offset(skip).limit(limit)
result = await db.execute(stmt)
return result.scalars().all()
+48
View File
@@ -0,0 +1,48 @@
import uuid
from typing import Sequence
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.comment import Comment
from app.schemas.comment import CommentCreate
async def create_comment(
db: AsyncSession,
study_id: uuid.UUID,
entity_type: str,
entity_id: uuid.UUID,
comment_in: CommentCreate,
created_by: uuid.UUID,
) -> Comment:
comment = Comment(
study_id=study_id,
entity_type=entity_type,
entity_id=entity_id,
content=comment_in.content,
created_by=created_by,
)
db.add(comment)
await db.commit()
await db.refresh(comment)
return comment
async def list_comments(
db: AsyncSession,
study_id: uuid.UUID,
entity_type: str,
entity_id: uuid.UUID,
) -> Sequence[Comment]:
result = await db.execute(
select(Comment)
.where(
Comment.study_id == study_id,
Comment.entity_type == entity_type,
Comment.entity_id == entity_id,
Comment.is_deleted.is_(False),
)
.order_by(Comment.created_at.asc())
)
return result.scalars().all()