From c2f95a0f2dfdd8327314e12896e89465fc1dee21 Mon Sep 17 00:00:00 2001 From: agatha Date: Sun, 31 Mar 2024 21:37:10 -0400 Subject: [PATCH] feat(api): add post viewing and creation --- backend/README.md | 0 backend/forum.py | 51 ++++++++++++++++++++++++++++++++++++++++ backend/requirements.txt | 2 ++ 3 files changed, 53 insertions(+) create mode 100644 backend/README.md create mode 100644 backend/forum.py create mode 100644 backend/requirements.txt diff --git a/backend/README.md b/backend/README.md new file mode 100644 index 0000000..e69de29 diff --git a/backend/forum.py b/backend/forum.py new file mode 100644 index 0000000..a217698 --- /dev/null +++ b/backend/forum.py @@ -0,0 +1,51 @@ +from fastapi import Body, FastAPI +from pydantic import BaseModel + +app = FastAPI() + + +class Post: + id: int + title: str + content: str + + def __init__(self, id, title, content): + self.id = id + self.title = title + self.content = content + + +class PostCreate(BaseModel): + title: str + content: str + + +POSTS = [ + Post(1, 'first post', 'fbpb'), + Post(2, 'second_post', 'second post bitttttch') +] + + +@app.get("/") +async def home(): + return POSTS + + +@app.post("/post") +async def create_post(data: PostCreate): + post = Post( + id=len(POSTS) + 1, + title=data.title, + content=data.content + ) + POSTS.append(post) + return POSTS[-1] + + +@app.get("/{post_id}") +async def get_post(post_id: int): + for post in POSTS: + if post.get('post_id') == post_id: + return post + + return {} diff --git a/backend/requirements.txt b/backend/requirements.txt new file mode 100644 index 0000000..364e2ee --- /dev/null +++ b/backend/requirements.txt @@ -0,0 +1,2 @@ +fastapi +uvicorn[standard]