forked from SamohvalovAndreyBPI2302/mtusi2026lab1
refactor: share pagination validation and sort keys
This commit is contained in:
+8
-6
@@ -29,6 +29,12 @@ def current_user(x_user_id: str | None = Header(None, alias="X-User-Id")) -> dic
|
||||
return store.user(x_user_id)
|
||||
|
||||
|
||||
def parse_limit(value: str) -> int:
|
||||
if not value.isdecimal() or not 1 <= int(value) <= 100:
|
||||
raise HTTPException(400)
|
||||
return int(value)
|
||||
|
||||
|
||||
@router.get("/api/v1/users/me")
|
||||
def user_me(actor: dict = Depends(current_user)):
|
||||
return actor
|
||||
@@ -43,9 +49,7 @@ def create_chat(body: CreateChat, actor: dict = Depends(current_user)):
|
||||
def list_chats(
|
||||
limit: str = Query("50"), cursor: str | None = Query(None, min_length=1), actor: dict = Depends(current_user)
|
||||
):
|
||||
if not limit.isdecimal() or not 1 <= int(limit) <= 100:
|
||||
raise HTTPException(400)
|
||||
return store.list_chats(actor["id"], int(limit), cursor)
|
||||
return store.list_chats(actor["id"], parse_limit(limit), cursor)
|
||||
|
||||
|
||||
@router.get("/api/v1/chats/{chat_id}")
|
||||
@@ -65,6 +69,4 @@ def list_messages(
|
||||
cursor: str | None = Query(None, min_length=1),
|
||||
actor: dict = Depends(current_user),
|
||||
):
|
||||
if not limit.isdecimal() or not 1 <= int(limit) <= 100:
|
||||
raise HTTPException(400)
|
||||
return store.list_messages(actor["id"], str(chat_id), int(limit), cursor)
|
||||
return store.list_messages(actor["id"], str(chat_id), parse_limit(limit), cursor)
|
||||
|
||||
Reference in New Issue
Block a user