diff --git a/backend/forum.py b/backend/forum.py index e5b39a1..c4c4515 100644 --- a/backend/forum.py +++ b/backend/forum.py @@ -6,11 +6,13 @@ app = FastAPI() class Post: id: int + thread_id: int title: str content: str - def __init__(self, id, title, content): + def __init__(self, id, thread_id, title, content): self.id = id + self.thread_id = thread_id self.title = title self.content = content @@ -36,25 +38,66 @@ THREADS = [] @app.get("/") -async def home(): - return POSTS +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=len(POSTS) + 1, - title=data.title, - content=data.content + id=id, + thread_id=id, + title=title, + content=content ) POSTS.append(post) - return POSTS[-1] + + return post -@app.get("/{post_id}") -async def get_post(post_id: int): +@app.get("/{thread_id}") +async def get_thread(thread_id: int): + result = [] for post in POSTS: - if post.get('post_id') == post_id: - return post + if post.thread_id == thread_id: + result.append(post) - return {} + 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'}