From 76a46806221a9aa7f883813725636f9ee3da3fc8 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 21 Oct 2023 10:10:02 +0200 Subject: [PATCH] add count get --- app/routers/users.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/routers/users.py b/app/routers/users.py index 6b79929..30f44be 100644 --- a/app/routers/users.py +++ b/app/routers/users.py @@ -1,4 +1,5 @@ from fastapi import APIRouter, Depends, HTTPException, status +from fastapi.responses import JSONResponse from ..dependencies import users_token, permissions_checker, database from ..models import users from typing import Annotated @@ -47,6 +48,14 @@ async def read_users_id(authorize: Annotated[bool, Depends(permissions_checker.P async def read_users_me(current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))]): return current_user +@router.get("/users/count", tags=["users"]) +async def read_users_count(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]): + count = database.database.get_collection("users").estimated_document_count() + content = {"count":count} + response = JSONResponse(content=content) + return response + + @router.get("/users/{item_id}", tags=["users"], response_model=users.User) async def read_users_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]): user_repository = users.UserRepository(database=database.database) @@ -54,7 +63,6 @@ async def read_users_id(item_id : str, authorize: Annotated[bool, Depends(permis return user - @router.delete("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["id", "password", "roles", "disabled"]) async def read_users_me(current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], remove: bool = False): user_repository = users.UserRepository(database=database.database)