49 lines
760 B
Python
49 lines
760 B
Python
from fastapi import 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 = []
|
|
|
|
|
|
@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 {}
|