feat: add page, limit
This commit is contained in:
parent
c3c421f66f
commit
e47e449a36
@ -1,8 +1,9 @@
|
||||
from typing import Optional
|
||||
import math
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from sqlalchemy import insert, select
|
||||
from sqlalchemy import insert, select, func
|
||||
from sqlalchemy.ext.asyncio import AsyncConnection
|
||||
from enum import Enum
|
||||
|
||||
@ -11,20 +12,34 @@ from api.db.tables.account import account_table
|
||||
from api.schemas.account.account import User
|
||||
from api.schemas.endpoints.account import UserUpdate, Role, Status
|
||||
|
||||
async def get_all_users_login_and_id(connection: AsyncConnection) -> Optional[User]:
|
||||
async def get_all_users_login_and_id(connection: AsyncConnection,page,limit) -> Optional[User]:
|
||||
"""
|
||||
Получает id и login всех юзеров
|
||||
"""
|
||||
query = select(account_table.c.id, account_table.c.login)
|
||||
first_user = page*limit-(limit-1)
|
||||
last_user = first_user+(limit-1)
|
||||
|
||||
query = select(account_table.c.id, account_table.c.login).where(account_table.c.id.between(first_user, last_user ))
|
||||
|
||||
# Выполняем запрос
|
||||
result = await connection.execute(query)
|
||||
|
||||
# Получаем все результаты
|
||||
users = result.fetchall()
|
||||
|
||||
user_list = [{'id': user.id, 'login': user.login} for user in users]
|
||||
|
||||
count_query = select(func.count()).select_from(account_table)
|
||||
|
||||
count_result = await connection.execute(count_query)
|
||||
amount_count = count_result.scalar()
|
||||
amount_pages = math.ceil(amount_count / limit)
|
||||
|
||||
|
||||
return {
|
||||
'users': user_list,
|
||||
'amount_count': amount_count,
|
||||
'amount_pages': amount_pages
|
||||
}
|
||||
|
||||
return user_list
|
||||
|
||||
|
||||
|
@ -30,10 +30,12 @@ api_router = APIRouter(
|
||||
)
|
||||
|
||||
|
||||
@api_router.get("/")
|
||||
@api_router.get("")
|
||||
async def get_all_account(
|
||||
|
||||
request: Request,
|
||||
page: int = 3,
|
||||
limit: int = 10,
|
||||
connection: AsyncConnection = Depends(get_connection_dep)
|
||||
):
|
||||
|
||||
@ -41,7 +43,7 @@ async def get_all_account(
|
||||
current_user = request.state.current_user
|
||||
authorize_user = await db_user_role_validation(connection, current_user)
|
||||
|
||||
user_list = await get_all_users_login_and_id(connection)
|
||||
user_list = await get_all_users_login_and_id(connection,page,limit)
|
||||
|
||||
if user_list is None:
|
||||
raise HTTPException(
|
||||
|
Loading…
Reference in New Issue
Block a user