From 2dedf30095f6072c9e5b8aee14d522af228e48bc Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 14 Oct 2023 11:53:31 +0200 Subject: [PATCH] add query limit and skip --- app/routers/users.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/routers/users.py b/app/routers/users.py index 1f7bc49..77e93c1 100644 --- a/app/routers/users.py +++ b/app/routers/users.py @@ -7,10 +7,16 @@ from typing import Annotated router = APIRouter() @router.get("/users", tags=["users"], response_model=list[users.UserOut]) -async def read_users(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]): +async def read_users(skip : int = 0, limit : int = 20, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]): + if limit < 1 or skip < 0: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="skip should be greater than 0 and limit should be greater than 1" + ) + limit = limit + skip listUsers = [] user_repository = users.UserRepository(database=database.database) - for user_index in user_repository.find_by({}, limit=20, skip=0): + for user_index in user_repository.find_by({}, limit=limit, skip=skip): user = users.UserOut(id=user_index.id, username=user_index.username, disabled=user_index.disabled, roles=user_index.roles) listUsers.append(user) return listUsers