from fastapi import APIRouter, Depends, HTTPException, status from sqlalchemy.orm import Session from typing import List import uuid from database import get_db from models import Post as PostModel, Comment as CommentModel from schemas import Comment, CreateCommentRequest, UpdateCommentRequest, Error router = APIRouter(prefix="/posts/{postId}/comments", tags=["Comments"]) @router.get("", response_model=List[Comment]) def get_comments(postId: str, db: Session = Depends(get_db)): """게시물의 댓글 조회""" post = db.query(PostModel).filter(PostModel.id == postId).first() if not post: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail={"message": "리소스를 찾을 수 없습니다", "details": "지정된 ID의 게시물이 존재하지 않습니다"} ) comments = db.query(CommentModel).filter(CommentModel.post_id == postId).all() return [{ "id": comment.id, "postId": comment.post_id, "username": comment.username, "content": comment.content, "createdAt": comment.created_at, "updatedAt": comment.updated_at } for comment in comments] @router.post("", response_model=Comment, status_code=status.HTTP_201_CREATED) def create_comment(postId: str, comment_request: CreateCommentRequest, db: Session = Depends(get_db)): """댓글 생성""" post = db.query(PostModel).filter(PostModel.id == postId).first() if not post: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail={"message": "리소스를 찾을 수 없습니다", "details": "지정된 ID의 게시물이 존재하지 않습니다"} ) comment_id = f"comment-{uuid.uuid4()}" new_comment = CommentModel( id=comment_id, post_id=postId, username=comment_request.username, content=comment_request.content ) db.add(new_comment) db.commit() db.refresh(new_comment) return { "id": new_comment.id, "postId": new_comment.post_id, "username": new_comment.username, "content": new_comment.content, "createdAt": new_comment.created_at, "updatedAt": new_comment.updated_at } @router.get("/{commentId}", response_model=Comment) def get_comment_by_id(postId: str, commentId: str, db: Session = Depends(get_db)): """특정 댓글 조회""" comment = db.query(CommentModel).filter( CommentModel.id == commentId, CommentModel.post_id == postId ).first() if not comment: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail={"message": "리소스를 찾을 수 없습니다", "details": "지정된 ID의 댓글이 존재하지 않습니다"} ) return { "id": comment.id, "postId": comment.post_id, "username": comment.username, "content": comment.content, "createdAt": comment.created_at, "updatedAt": comment.updated_at } @router.patch("/{commentId}", response_model=Comment) def update_comment( postId: str, commentId: str, comment_request: UpdateCommentRequest, db: Session = Depends(get_db) ): """댓글 업데이트""" comment = db.query(CommentModel).filter( CommentModel.id == commentId, CommentModel.post_id == postId ).first() if not comment: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail={"message": "리소스를 찾을 수 없습니다", "details": "지정된 ID의 댓글이 존재하지 않습니다"} ) comment.username = comment_request.username comment.content = comment_request.content db.commit() db.refresh(comment) return { "id": comment.id, "postId": comment.post_id, "username": comment.username, "content": comment.content, "createdAt": comment.created_at, "updatedAt": comment.updated_at } @router.delete("/{commentId}", status_code=status.HTTP_204_NO_CONTENT) def delete_comment(postId: str, commentId: str, db: Session = Depends(get_db)): """댓글 삭제""" comment = db.query(CommentModel).filter( CommentModel.id == commentId, CommentModel.post_id == postId ).first() if not comment: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail={"message": "리소스를 찾을 수 없습니다", "details": "지정된 ID의 댓글이 존재하지 않습니다"} ) db.delete(comment) db.commit() return None