|
|
|
@ -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:
|
|
|
|
|