feat(api): add post viewing and creation

This commit is contained in:
agatha 2024-03-31 21:37:10 -04:00
parent d466e16882
commit c2f95a0f2d
3 changed files with 53 additions and 0 deletions

0
backend/README.md Normal file
View File

51
backend/forum.py Normal file
View File

@ -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 {}

2
backend/requirements.txt Normal file
View File

@ -0,0 +1,2 @@
fastapi
uvicorn[standard]