From 632f61a376c7dfecc2335ce4b656ca15b4d9f2a1 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Fri, 3 Nov 2023 21:36:08 +0100 Subject: [PATCH] add route users --- app/routers/users.py | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/app/routers/users.py b/app/routers/users.py index 1f33b0e..309ce13 100644 --- a/app/routers/users.py +++ b/app/routers/users.py @@ -145,8 +145,8 @@ async def update_users_me(current_user: Annotated[users.User, Depends(users_toke user_repository.save(current_user) return current_user -@router.put("/users", tags=["users"], response_model=users.User, status_code=status.HTTP_200_OK) -async def update_users_id(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userSingle: users.UserIn | None = None, response: Response = Response): +@router.put("/users", tags=["users"], response_model=users.User, status_code=status.HTTP_201_CREATED) +async def update_users(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userSingle: users.UserIn | None = None, response: Response = Response): if userSingle is None: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, @@ -155,9 +155,18 @@ async def update_users_id(authorize: Annotated[bool, Depends(permissions_checker user_repository = users.UserRepository(database=database.database) user = user_repository.find_one_by({"$or":[{"username": {'$eq': userSingle.username}}, {"email": {"$eq": userSingle.email}}]}) - if user is None: + if user is not None: response.status_code = status.HTTP_201_CREATED - user = users.User(username=userSingle.username, password=users_token.get_password_hash(userSingle.password), email=userSingle.email) + if user.username == userSingle.username: + raise HTTPException( + status_code=status.HTTP_204_NO_CONTENT, + detail="username" + ) + if user.email == userSingle.email: + raise HTTPException( + status_code=status.HTTP_204_NO_CONTENT, + detail="email" + ) user.username = userSingle.username user.password = users_token.get_password_hash(userSingle.password) user.roles = userSingle.roles @@ -170,6 +179,33 @@ async def update_users_id(authorize: Annotated[bool, Depends(permissions_checker return user +@router.put("/users/{item_id}", tags=["users"], response_model=users.User, status_code=status.HTTP_200_OK) +async def update_users_id(item_id: str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userSingle: users.UserIn | None = None, response: Response = Response): + if userSingle is None: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Body request is empty" + ) + user_repository = users.UserRepository(database=database.database) + + user = user_repository.find_one_by({"id": {'$eq': ObjectId(item_id)}}) + if user is None: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="User not found" + ) + + user.username = userSingle.username + user.password = users_token.get_password_hash(userSingle.password) + user.roles = userSingle.roles + user.email = userSingle.email + user.firstName = userSingle.firstName + user.name = userSingle.name + user.birth = userSingle.birth + user.updated_at = datetime.today() + user_repository.save(user) + return user + @router.patch("/users/groups",tags=["users"]) async def patch_users_groups(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userids: users.UserIDS | None = None):