diff --git a/app/routes/friends.py b/app/routes/friends.py index 275a93f..883ce64 100644 --- a/app/routes/friends.py +++ b/app/routes/friends.py @@ -135,7 +135,7 @@ async def deny_friend(id: str, current_user: User = Depends(get_current_user)): except bson.errors.InvalidId: objectid_misformatted() - #if result.matched_count == 0: friend_not_found() + if result.matched_count == 0: friend_not_found() return {"message": "Friend request denied"} diff --git a/app/routes/pins.py b/app/routes/pins.py index e4c58ff..b8080be 100644 --- a/app/routes/pins.py +++ b/app/routes/pins.py @@ -27,6 +27,10 @@ pins_router = APIRouter( tags=["Pins"] ) +def check_pin_is_null(pin): + if pin is None: + raise HTTPException(status_code=404, detail="Pin not found") + @pins_router.get( path="/{id}", responses={401: {"model": HTTPError}, 404: {"model": HTTPError}, 422: {"model": HTTPError}} @@ -34,8 +38,7 @@ pins_router = APIRouter( async def get_pin(id: str, current_user: User = Depends(get_current_user)): try: pin = pins_collection.find_one({"_id": ObjectId(id)}) - if pin is None: - raise HTTPException(status_code=404, detail="Pin not found") + check_pin_is_null(pin) # Vérifier si l'utilisateur a la permission de voir le pin if pin["user_id"] != current_user.uid: @@ -59,8 +62,7 @@ async def update_pin(id: str, pin: PinDTO, current_user: User = Depends(get_curr try: # Vérifier si le pin existe existing_pin = pins_collection.find_one({"_id": ObjectId(id)}) - if existing_pin is None: - raise HTTPException(status_code=404, detail="Pin not found") + check_pin_is_null(existing_pin) # Vérifier si l'utilisateur a la permission de modifier le pin if existing_pin["user_id"] != current_user.uid: @@ -73,7 +75,8 @@ async def update_pin(id: str, pin: PinDTO, current_user: User = Depends(get_curr raise HTTPException(status_code=403, detail="You don't have permission to edit this pin") # Mettre à jour le pin - result = pins_collection.update_one({"_id": ObjectId(id)}, {"$set": pin.model_dump()}) + pins_collection.update_one({"_id": ObjectId(id)}, {"$set": pin.model_dump()}) + return {"message": "Pin updated"} except bson.errors.InvalidId: @@ -123,8 +126,7 @@ async def share_pin(id: str, share_data: PinShareDTO, current_user: User = Depen try: # Vérifier si le pin existe et appartient à l'utilisateur courant pin = pins_collection.find_one({"_id": ObjectId(id)}) - if pin is None: - raise HTTPException(status_code=404, detail="Pin not found") + check_pin_is_null(pin) if pin["user_id"] != current_user.uid: raise HTTPException(status_code=403, detail="You can only share your own pins") @@ -176,8 +178,7 @@ async def share_pin(id: str, share_data: PinShareDTO, current_user: User = Depen async def delete_pin(id: str, current_user: User = Depends(get_current_user)): try: pin = pins_collection.find_one({"_id": ObjectId(id)}) - if pin is None: - raise HTTPException(status_code=404, detail="Pin not found") + check_pin_is_null(pin) # Si l'utilisateur est le propriétaire, supprimer le pin et toutes ses permissions if pin["user_id"] == current_user.uid: diff --git a/app/serializers/friend_serializer.py b/app/serializers/friend_serializer.py index 6f4fbea..b2503ea 100644 --- a/app/serializers/friend_serializer.py +++ b/app/serializers/friend_serializer.py @@ -1,7 +1,13 @@ from app.models.friend import Friend def friend_serialize(friend: list, is_external: bool) -> Friend: - status = friend['status'] if friend['status'] != 'pending' else ('pending_approval' if is_external else 'pending') + if friend['status'] != 'pending': + status = friend['status'] + else: + if is_external: + status = 'pending_approval' + else: + status = 'pending' return Friend(**{ "id": str(friend["_id"]),