Updating entries
PUT method
class Post(BaseModel):
title: str
content: str
published: bool = True
rating: Optional[int] = Noneclass Post(BaseModel):
title: str
content: str
published: bool = True
rating: Optional[int] = None
@app.put("/posts/{id}")
def update_posts(id: int, post: Post):
index = find_index_post(id)
# Raising a 404 if post index position of post is not found
if index == None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"post with {id} does not exist")
post_dict = post.dict() # Converting the data sent by frontend to python dict
post_dict["id"] = id # Setting the ID to be equal to the ID from the frontend
my_posts[index] = post_dict # Replace the post with the updated post
return {"data": post_dict} # Return new post postLast updated