细节优化——1
This commit is contained in:
@@ -21,6 +21,7 @@ async def create_comment(
|
||||
entity_type=entity_type,
|
||||
entity_id=entity_id,
|
||||
content=comment_in.content,
|
||||
quote_comment_id=comment_in.quote_comment_id,
|
||||
created_by=created_by,
|
||||
)
|
||||
db.add(comment)
|
||||
@@ -46,3 +47,35 @@ async def list_comments(
|
||||
.order_by(Comment.created_at.asc())
|
||||
)
|
||||
return result.scalars().all()
|
||||
|
||||
|
||||
async def get_comment(db: AsyncSession, comment_id: uuid.UUID, *, include_deleted: bool = False) -> Comment | None:
|
||||
stmt = select(Comment).where(Comment.id == comment_id)
|
||||
if not include_deleted:
|
||||
stmt = stmt.where(Comment.is_deleted.is_(False))
|
||||
result = await db.execute(stmt)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
async def get_comments_by_ids(
|
||||
db: AsyncSession,
|
||||
ids: set[uuid.UUID],
|
||||
*,
|
||||
include_deleted: bool = False,
|
||||
) -> dict[uuid.UUID, Comment]:
|
||||
if not ids:
|
||||
return {}
|
||||
stmt = select(Comment).where(Comment.id.in_(ids))
|
||||
if not include_deleted:
|
||||
stmt = stmt.where(Comment.is_deleted.is_(False))
|
||||
result = await db.execute(stmt)
|
||||
comments = result.scalars().all()
|
||||
return {c.id: c for c in comments}
|
||||
|
||||
|
||||
async def soft_delete_comment(db: AsyncSession, comment: Comment) -> Comment:
|
||||
comment.is_deleted = True
|
||||
db.add(comment)
|
||||
await db.commit()
|
||||
await db.refresh(comment)
|
||||
return comment
|
||||
|
||||
Reference in New Issue
Block a user