from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Post: id: int thread_id: int title: str content: str def __init__(self, id, thread_id, title, content): self.id = id self.thread_id = thread_id self.title = title self.content = content class PostCreate(BaseModel): title: str content: str class Thread: id: int title: str closed: bool def __init__(self, id, title, closed=False): self.id = id self.title = title self.closed = closed POSTS = [] THREADS = [] @app.get("/") async def get_threads(): return THREADS @app.post("/post") async def create_post(data: PostCreate): id = len(POSTS) + 1 title = data.title content = data.content # Create thread thread = Thread( id=id, title=title ) THREADS.append(thread) # Create post post = Post( id=id, thread_id=id, title=title, content=content ) POSTS.append(post) return post @app.get("/{thread_id}") async def get_thread(thread_id: int): result = [] for post in POSTS: if post.thread_id == thread_id: result.append(post) return result @app.post("/{thread_id}/post") async def create_reply(thread_id: int, data: PostCreate): parent = None for thread in THREADS: if thread.id == thread_id: parent = thread if parent: id = len(POSTS) + 1 title = data.title content = data.content # Create post post = Post( id=id, thread_id=parent.id, title=title, content=content ) POSTS.append(post) return post return {'error': 'could not find parent thread'}