Test if user exists & if already friends before creating friend request
continuous-integration/drone/push Build is failing Details

master
Alix JEUDI--LEMOINE 2 months ago
parent b7a23aff9f
commit 7977460d85

@ -44,7 +44,6 @@ async def get_friend(id: str, current_user: User = Depends(get_current_user)):
responses={401: {"model": HTTPError}, 409: {"model": HTTPError}} responses={401: {"model": HTTPError}, 409: {"model": HTTPError}}
) )
async def add_friend(friend_to_add: FriendAddDTO, current_user: User = Depends(get_current_user)): async def add_friend(friend_to_add: FriendAddDTO, current_user: User = Depends(get_current_user)):
# TODO: test if exists
friend: Friend = friend_to_add.model_dump() friend: Friend = friend_to_add.model_dump()
if(current_user.uid == friend["friend_user_id"]): if(current_user.uid == friend["friend_user_id"]):
@ -53,6 +52,20 @@ async def add_friend(friend_to_add: FriendAddDTO, current_user: User = Depends(g
detail="Cannot add yourself as a friend" detail="Cannot add yourself as a friend"
) )
# Test if already friends
if friends_collection.find_one({"user_id": current_user.uid, "friend_user_id": friend["friend_user_id"]}) is not None:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail="Friend already exists"
)
# Test if user exists
if db["users"].find_one({"_id": ObjectId(friend["friend_user_id"])}):
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="User not found"
)
friend["user_id"] = current_user.uid friend["user_id"] = current_user.uid
friend["status"] = "pending" friend["status"] = "pending"
friend_id = friends_collection.insert_one(friend).inserted_id friend_id = friends_collection.insert_one(friend).inserted_id

Loading…
Cancel
Save