From 594c5c6de8c9c6927daca72c49447cf9289f64c1 Mon Sep 17 00:00:00 2001 From: agatha Date: Sat, 6 Apr 2024 14:50:59 -0400 Subject: [PATCH] chore: refactor --- backend/main.py | 2 +- backend/routers/forum.py | 28 +++++++++++++--------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/backend/main.py b/backend/main.py index a8de0cb..e392f81 100644 --- a/backend/main.py +++ b/backend/main.py @@ -8,5 +8,5 @@ app = FastAPI() models.Base.metadata.create_all(bind=engine) -app.include_router(auth.router) app.include_router(forum.router) +app.include_router(auth.router) diff --git a/backend/routers/forum.py b/backend/routers/forum.py index a7fda60..ee0a75b 100644 --- a/backend/routers/forum.py +++ b/backend/routers/forum.py @@ -27,11 +27,24 @@ class PostCreate(BaseModel): content: str = Field('') +@router.get('/catalog', status_code=status.HTTP_200_OK) +async def get_catalog(db: db_dependency): + return db.query(Thread).all() + + @router.get('/', status_code=status.HTTP_200_OK) async def get_posts(db: db_dependency): return db.query(Post).all() +@router.get('/{thread_id}', status_code=status.HTTP_200_OK) +async def get_thread_by_id(db: db_dependency, thread_id: int = Path(gt=0)): + posts = db.query(Post).filter(Post.thread_id == thread_id).all() + if posts: + return posts + + raise HTTPException(404, f'Could not find thread') + @router.post('/', status_code=status.HTTP_201_CREATED) async def create_thread(db: db_dependency, data: PostCreate): try: @@ -68,15 +81,6 @@ async def create_thread(db: db_dependency, data: PostCreate): raise HTTPException(status_code=400, detail=str(e)) -@router.get('/{thread_id}', status_code=status.HTTP_200_OK) -async def get_thread_by_id(db: db_dependency, thread_id: int = Path(gt=0)): - posts = db.query(Post).filter(Post.thread_id == thread_id).all() - if posts: - return posts - - raise HTTPException(404, f'Could not find thread') - - @router.post('/{thread_id}', status_code=status.HTTP_201_CREATED) async def create_reply(db: db_dependency, data: PostCreate, thread_id: int = Path(gt=0)): thread = db.query(Thread).filter(Thread.id == thread_id).first() @@ -98,9 +102,3 @@ async def create_reply(db: db_dependency, data: PostCreate, thread_id: int = Pat } raise HTTPException(status_code=404, detail='Could not find thread') - - -@router.get('/catalog', status_code=status.HTTP_200_OK) -async def get_catalog(db: db_dependency): - return db.query(Thread).all() -