2024-04-01 02:15:34 +00:00
|
|
|
from fastapi import FastAPI
|
2024-04-01 01:37:10 +00:00
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
class Post:
|
2024-04-01 03:55:00 +00:00
|
|
|
"""Post is a single forum post or reply"""
|
2024-04-01 01:37:10 +00:00
|
|
|
id: int
|
2024-04-01 03:27:57 +00:00
|
|
|
thread_id: int
|
2024-04-01 03:45:39 +00:00
|
|
|
author: str
|
2024-04-01 01:37:10 +00:00
|
|
|
title: str
|
|
|
|
content: str
|
|
|
|
|
2024-04-01 03:45:39 +00:00
|
|
|
def __init__(self, id, thread_id, author, title, content):
|
2024-04-01 01:37:10 +00:00
|
|
|
self.id = id
|
2024-04-01 03:27:57 +00:00
|
|
|
self.thread_id = thread_id
|
2024-04-01 03:45:39 +00:00
|
|
|
self.author = author
|
2024-04-01 01:37:10 +00:00
|
|
|
self.title = title
|
|
|
|
self.content = content
|
|
|
|
|
|
|
|
|
|
|
|
class PostCreate(BaseModel):
|
2024-04-01 03:55:00 +00:00
|
|
|
"""Used when creating posts and replies"""
|
|
|
|
author: str = 'anon'
|
2024-04-01 01:37:10 +00:00
|
|
|
title: str
|
|
|
|
content: str
|
|
|
|
|
|
|
|
|
2024-04-01 02:55:50 +00:00
|
|
|
class Thread:
|
2024-04-01 03:55:00 +00:00
|
|
|
"""Thread is a collection of a post and its replies"""
|
2024-04-01 02:55:50 +00:00
|
|
|
id: int
|
2024-04-01 03:45:39 +00:00
|
|
|
author: str
|
2024-04-01 02:55:50 +00:00
|
|
|
title: str
|
2024-04-01 03:09:22 +00:00
|
|
|
closed: bool
|
2024-04-01 02:55:50 +00:00
|
|
|
|
2024-04-01 03:45:39 +00:00
|
|
|
def __init__(self, id, author, title, closed=False):
|
2024-04-01 02:55:50 +00:00
|
|
|
self.id = id
|
2024-04-01 03:45:39 +00:00
|
|
|
self.author = author
|
2024-04-01 02:55:50 +00:00
|
|
|
self.title = title
|
2024-04-01 03:09:22 +00:00
|
|
|
self.closed = closed
|
2024-04-01 02:55:50 +00:00
|
|
|
|
|
|
|
|
2024-04-01 02:25:10 +00:00
|
|
|
POSTS = []
|
2024-04-01 03:09:22 +00:00
|
|
|
THREADS = []
|
2024-04-01 01:37:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.get("/")
|
2024-04-01 03:27:57 +00:00
|
|
|
async def get_threads():
|
|
|
|
return THREADS
|
2024-04-01 01:37:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.post("/post")
|
|
|
|
async def create_post(data: PostCreate):
|
2024-04-01 03:27:57 +00:00
|
|
|
id = len(POSTS) + 1
|
2024-04-01 03:55:00 +00:00
|
|
|
author = data.author
|
2024-04-01 03:27:57 +00:00
|
|
|
title = data.title
|
|
|
|
content = data.content
|
|
|
|
|
|
|
|
# Create thread
|
|
|
|
thread = Thread(
|
|
|
|
id=id,
|
2024-04-01 03:45:39 +00:00
|
|
|
author=author,
|
2024-04-01 03:27:57 +00:00
|
|
|
title=title
|
|
|
|
)
|
|
|
|
THREADS.append(thread)
|
|
|
|
|
|
|
|
# Create post
|
2024-04-01 01:37:10 +00:00
|
|
|
post = Post(
|
2024-04-01 03:27:57 +00:00
|
|
|
id=id,
|
|
|
|
thread_id=id,
|
2024-04-01 03:45:39 +00:00
|
|
|
author=author,
|
2024-04-01 03:27:57 +00:00
|
|
|
title=title,
|
|
|
|
content=content
|
2024-04-01 01:37:10 +00:00
|
|
|
)
|
|
|
|
POSTS.append(post)
|
2024-04-01 03:27:57 +00:00
|
|
|
|
|
|
|
return post
|
2024-04-01 01:37:10 +00:00
|
|
|
|
|
|
|
|
2024-04-01 03:27:57 +00:00
|
|
|
@app.get("/{thread_id}")
|
|
|
|
async def get_thread(thread_id: int):
|
|
|
|
result = []
|
2024-04-01 01:37:10 +00:00
|
|
|
for post in POSTS:
|
2024-04-01 03:27:57 +00:00
|
|
|
if post.thread_id == thread_id:
|
|
|
|
result.append(post)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/{thread_id}/post")
|
2024-04-01 03:29:23 +00:00
|
|
|
async def reply_to_post(thread_id: int, data: PostCreate):
|
2024-04-01 03:27:57 +00:00
|
|
|
parent = None
|
|
|
|
for thread in THREADS:
|
|
|
|
if thread.id == thread_id:
|
|
|
|
parent = thread
|
|
|
|
|
2024-04-01 03:57:09 +00:00
|
|
|
if parent is None:
|
|
|
|
return {'error': 'could not find parent thread'}
|
|
|
|
|
|
|
|
id = len(POSTS) + 1
|
|
|
|
author = data.author
|
|
|
|
title = data.title
|
|
|
|
content = data.content
|
|
|
|
|
|
|
|
# Create post
|
|
|
|
post = Post(
|
|
|
|
id=id,
|
|
|
|
thread_id=parent.id,
|
|
|
|
author=author,
|
|
|
|
title=title,
|
|
|
|
content=content
|
|
|
|
)
|
|
|
|
POSTS.append(post)
|
|
|
|
|
|
|
|
return post
|