from fastapi import FastAPI, Depends | |
from pydantic import BaseModel | |
class Comment(BaseModel): | |
username: str | |
content: str | |
app = FastAPI() | |
database = { | |
"articles": { | |
1: { | |
"title": "Top 3 Reasons to Start Using FastAPI Now", | |
"comments": [] | |
} | |
} | |
} | |
def get_database(): | |
return database | |
@app.post("/articles/{article_id}/comments") | |
def post_comment(article_id: int, comment: Comment, database = Depends(get_database)): | |
database["articles"][article_id]["comments"].append(comment) | |
return {"msg": "comment posted!"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment