修改页面分页情况
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select
|
||||
|
||||
@@ -17,14 +17,18 @@ router = APIRouter(prefix="/credits", tags=["credits"])
|
||||
|
||||
@router.get("", response_model=CreditBalanceOut)
|
||||
async def get_credits(
|
||||
page: int = Query(1, ge=1),
|
||||
page_size: int = Query(20, ge=1, le=100),
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
records = await get_records(db, current_user.id)
|
||||
return CreditBalanceOut(
|
||||
credits=round(current_user.credits, 2),
|
||||
records=[CreditRecordOut.model_validate(r) for r in records],
|
||||
)
|
||||
records, total = await get_records(db, current_user.id, page, page_size)
|
||||
from fastapi.responses import JSONResponse
|
||||
return JSONResponse(content={
|
||||
"credits": round(current_user.credits, 2),
|
||||
"records": [CreditRecordOut.model_validate(r) for r in records],
|
||||
"total": total,
|
||||
})
|
||||
|
||||
|
||||
@router.get(
|
||||
|
||||
@@ -8,7 +8,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from app.dependencies import get_db, get_current_user
|
||||
from app.models.user import User
|
||||
from app.models.notification import Notification
|
||||
from app.schemas.notification import NotificationOut, UnreadCountOut
|
||||
from app.schemas.notification import NotificationOut, NotificationListOut, UnreadCountOut
|
||||
from app.services.auth import decode_access_token
|
||||
from app.services.notification import (
|
||||
get_notifications,
|
||||
@@ -103,15 +103,16 @@ async def notifications_ws(websocket: WebSocket):
|
||||
_active_connections.pop(user_id, None)
|
||||
|
||||
|
||||
@router.get("", response_model=list[NotificationOut])
|
||||
@router.get("", response_model=NotificationListOut)
|
||||
async def list_notifications(
|
||||
page: int = Query(1, ge=1),
|
||||
page_size: int = Query(20, ge=1, le=100),
|
||||
is_read: bool = Query(None),
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
items, _ = await get_notifications(db, current_user.id, page, page_size)
|
||||
return items
|
||||
items, total = await get_notifications(db, current_user.id, page, page_size, is_read)
|
||||
return {"items": items, "total": total}
|
||||
|
||||
|
||||
@router.put("/{notification_id}/read")
|
||||
|
||||
@@ -290,20 +290,29 @@ async def alipay_callback(request: Request, db: AsyncSession = Depends(get_db)):
|
||||
|
||||
@router.get("/orders", response_model=list[PaymentOrderOut])
|
||||
async def list_orders(
|
||||
page: int = Query(1, ge=1),
|
||||
page_size: int = Query(20, ge=1, le=100),
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
# Auto-expire stale pending orders before returning
|
||||
from app.services.payment import _check_and_expire_order
|
||||
|
||||
count_query = select(func.count(PaymentOrder.id)).where(PaymentOrder.user_id == current_user.id)
|
||||
total = (await db.execute(count_query)).scalar() or 0
|
||||
|
||||
result = await db.execute(
|
||||
select(PaymentOrder)
|
||||
.where(PaymentOrder.user_id == current_user.id)
|
||||
.order_by(PaymentOrder.created_at.desc())
|
||||
.offset((page - 1) * page_size)
|
||||
.limit(page_size)
|
||||
)
|
||||
orders = result.scalars().all()
|
||||
for o in orders:
|
||||
await _check_and_expire_order(db, o)
|
||||
return orders
|
||||
|
||||
from fastapi.responses import JSONResponse
|
||||
return JSONResponse(content={"items": orders, "total": total})
|
||||
|
||||
|
||||
@router.get("/orders/{order_no}", response_model=PaymentOrderOut)
|
||||
|
||||
Reference in New Issue
Block a user