小程序后端增强绑定能力并支持多设备列表
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import logging
|
||||
from datetime import datetime
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request, status
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request, status
|
||||
from pydantic import BaseModel
|
||||
|
||||
try:
|
||||
@@ -45,6 +45,20 @@ class BindingGetResponse(BaseModel):
|
||||
bound_at: datetime
|
||||
|
||||
|
||||
class BindingListItem(BaseModel):
|
||||
device_id: str
|
||||
child_id: int | None
|
||||
child_name: str | None = None
|
||||
status: int
|
||||
bound_at: datetime
|
||||
|
||||
|
||||
class BindingListResponse(BaseModel):
|
||||
items: list[BindingListItem]
|
||||
total: int
|
||||
next_cursor: int | None = None
|
||||
|
||||
|
||||
class BindHistoryItem(BaseModel):
|
||||
device_id: str
|
||||
child_id: int | None
|
||||
@@ -140,6 +154,30 @@ def get_current_binding(
|
||||
return binding
|
||||
|
||||
|
||||
@router.get("", response_model=BindingListResponse)
|
||||
def list_bindings(
|
||||
request: Request,
|
||||
cursor: int | None = Query(default=None, ge=1),
|
||||
limit: int = Query(default=20, ge=1, le=100),
|
||||
current_user_id: int = Depends(get_current_user_id),
|
||||
db=Depends(get_db_session),
|
||||
) -> BindingListResponse:
|
||||
service = BindingService(db)
|
||||
rows, has_more = service.list_bindings(current_user_id, limit, cursor)
|
||||
next_cursor = int(rows[-1]["id"]) if has_more and rows else None
|
||||
items = [
|
||||
BindingListItem(
|
||||
device_id=row["device_id"],
|
||||
child_id=row["child_id"],
|
||||
child_name=row["child_name"],
|
||||
status=row["status"],
|
||||
bound_at=row["bound_at"],
|
||||
)
|
||||
for row in rows
|
||||
]
|
||||
return BindingListResponse(items=items, total=len(items), next_cursor=next_cursor)
|
||||
|
||||
|
||||
@router.get("/{device_id}", response_model=BindingGetResponse)
|
||||
def get_binding(
|
||||
device_id: str,
|
||||
|
||||
Reference in New Issue
Block a user