修改页面分页情况
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user