feat: forum is now Thread based
This commit is contained in:
parent
e92bb1c616
commit
701d20bc60
@ -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):
|
||||
post = Post(
|
||||
id=len(POSTS) + 1,
|
||||
title=data.title,
|
||||
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 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 {}
|
||||
|
||||
@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'}
|
||||
|
Loading…
Reference in New Issue
Block a user