morethanadiagnosis-hub/backend/app/api/v1/forum.py
2025-11-19 09:42:15 -08:00

79 lines
2.6 KiB
Python

"""Forum MVP API endpoints. Job ID: MTAD-IMPL-2025-11-18-CL"""
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from app.database import get_db
from app.models import ForumCategory, ForumThread, ForumPost
import uuid
router = APIRouter()
@router.get("/categories")
async def list_categories(db: Session = Depends(get_db)):
"""List all forum categories."""
categories = db.query(ForumCategory).order_by(ForumCategory.order).all()
return {"items": categories}
@router.get("/categories/{category_id}/threads")
async def list_threads(category_id: str, skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
"""List threads in a category."""
threads = db.query(ForumThread).filter(ForumThread.category_id == category_id).offset(skip).limit(limit).all()
return {"items": threads}
@router.get("/threads/{thread_id}/posts")
async def list_posts(thread_id: str, skip: int = 0, limit: int = 20, db: Session = Depends(get_db)):
"""List posts in a thread."""
posts = db.query(ForumPost).filter(ForumPost.thread_id == thread_id, ForumPost.deleted_at.is_(None)).offset(skip).limit(limit).all()
return {"items": posts}
@router.post("/categories")
async def create_category(name: str, db: Session = Depends(get_db)):
"""Create forum category (admin only)."""
# Mock implementation for MVP
category = ForumCategory(
id=str(uuid.uuid4()),
name=name,
slug=name.lower().replace(" ", "-"),
description=f"Discussions about {name}",
order=0
)
db.add(category)
db.commit()
return category
@router.post("/threads")
async def create_thread(category_id: str, title: str, content: str, user_id: str = "test-user", db: Session = Depends(get_db)):
"""Create forum thread."""
# Mock user for MVP
thread = ForumThread(
id=str(uuid.uuid4()),
category_id=category_id,
author_id=user_id,
title=title,
slug=title.lower().replace(" ", "-"),
)
db.add(thread)
# Create initial post
post = ForumPost(
id=str(uuid.uuid4()),
thread_id=thread.id,
author_id=user_id,
content=content
)
db.add(post)
db.commit()
return thread
@router.post("/posts")
async def create_post(thread_id: str, content: str, user_id: str = "test-user", db: Session = Depends(get_db)):
"""Create forum post."""
post = ForumPost(
id=str(uuid.uuid4()),
thread_id=thread_id,
author_id=user_id,
content=content
)
db.add(post)
db.commit()
return post