remove forum.py
This commit is contained in:
parent
4a1dd775eb
commit
98439fd1e7
131
backend/forum.py
131
backend/forum.py
@ -1,131 +0,0 @@
|
||||
from fastapi import FastAPI
|
||||
import models
|
||||
from database import engine, SessionLocal
|
||||
from pydantic import BaseModel
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
models.Base.metadata.create_all(bind=engine)
|
||||
|
||||
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
class Post:
|
||||
"""Post is a single forum post or reply"""
|
||||
id: int
|
||||
thread_id: int
|
||||
author: str
|
||||
title: str
|
||||
content: str
|
||||
|
||||
def __init__(self, id, thread_id, author, title, content):
|
||||
self.id = id
|
||||
self.thread_id = thread_id
|
||||
self.author = author
|
||||
self.title = title
|
||||
self.content = content
|
||||
|
||||
|
||||
class PostCreate(BaseModel):
|
||||
"""Used when creating posts and replies"""
|
||||
author: str = 'anon'
|
||||
title: str
|
||||
content: str
|
||||
|
||||
|
||||
class Thread:
|
||||
"""Thread is a collection of a post and its replies"""
|
||||
id: int
|
||||
author: str
|
||||
title: str
|
||||
closed: bool
|
||||
|
||||
def __init__(self, id, author, title, closed=False):
|
||||
self.id = id
|
||||
self.author = author
|
||||
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
|
||||
author = data.author
|
||||
title = data.title
|
||||
content = data.content
|
||||
|
||||
# Create thread
|
||||
thread = Thread(
|
||||
id=id,
|
||||
author=author,
|
||||
title=title
|
||||
)
|
||||
THREADS.append(thread)
|
||||
|
||||
# Create post
|
||||
post = Post(
|
||||
id=id,
|
||||
thread_id=id,
|
||||
author=author,
|
||||
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)
|
||||
|
||||
if len(result) == 0:
|
||||
return {'error': 'could not find parent thread'}
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@app.post("/{thread_id}/post")
|
||||
async def reply_to_post(thread_id: int, data: PostCreate):
|
||||
parent = None
|
||||
for thread in THREADS:
|
||||
if thread.id == thread_id:
|
||||
parent = thread
|
||||
|
||||
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
|
Loading…
Reference in New Issue
Block a user