修改页面分页情况

This commit is contained in:
2026-06-15 17:50:47 +08:00
parent 3aa6c7cf2f
commit d71b50c200
12 changed files with 187 additions and 64 deletions
+7 -2
View File
@@ -282,10 +282,15 @@ async def refund_credits(
)
async def get_records(db: AsyncSession, user_id: str) -> list[CreditRecord]:
async def get_records(db: AsyncSession, user_id: str, page: int = 1, page_size: int = 20) -> tuple[list[CreditRecord], int]:
count_query = select(func.count(CreditRecord.id)).where(CreditRecord.user_id == user_id)
total = (await db.execute(count_query)).scalar() or 0
result = await db.execute(
select(CreditRecord)
.where(CreditRecord.user_id == user_id)
.order_by(CreditRecord.created_at.desc())
.offset((page - 1) * page_size)
.limit(page_size)
)
return list(result.scalars().all())
return list(result.scalars().all()), total
+18 -6
View File
@@ -74,16 +74,18 @@ async def create_notification(
async def get_notifications(
db: AsyncSession, user_id: str, page: int = 1, page_size: int = 20
db: AsyncSession, user_id: str, page: int = 1, page_size: int = 20, is_read: bool = None
) -> tuple[list[dict], int]:
"""Get notifications for a user (personal + broadcast), with per-user read state."""
user_is_read_expr = case(
(Notification.user_id.is_(None), NotificationRead.id.is_not(None)),
else_=Notification.is_read,
).label("user_is_read")
query = (
select(
Notification,
case(
(Notification.user_id.is_(None), NotificationRead.id.is_not(None)),
else_=Notification.is_read,
).label("user_is_read"),
user_is_read_expr,
)
.outerjoin(
NotificationRead,
@@ -96,11 +98,15 @@ async def get_notifications(
Notification.user_id.is_(None),
)
)
.order_by(Notification.created_at.desc())
)
count_query = (
select(func.count(Notification.id))
.outerjoin(
NotificationRead,
(Notification.id == NotificationRead.notification_id)
& (NotificationRead.user_id == user_id),
)
.where(
or_(
Notification.user_id == user_id,
@@ -109,6 +115,12 @@ async def get_notifications(
)
)
if is_read is not None:
query = query.where(user_is_read_expr == is_read)
count_query = count_query.where(user_is_read_expr == is_read)
query = query.order_by(Notification.created_at.desc())
total = (await db.execute(count_query)).scalar() or 0
result = await db.execute(query.offset((page - 1) * page_size).limit(page_size))
rows = result.all()