"""Comments router.""" from typing import List from fastapi import APIRouter, Depends, HTTPException, status from sqlalchemy.orm import Session from app.core.config import get_db from app.models import models from app.schemas import schemas router = APIRouter(tags=["Comments"]) @router.post("/comments", response_model=schemas.CommentResponse, status_code=status.HTTP_201_CREATED) def create_comment(comment: schemas.CommentCreate, db: Session = Depends(get_db)): db_comment = models.Comment(**comment.model_dump()) db.add(db_comment) db.commit() db.refresh(db_comment) return db_comment @router.get("/issues/{issue_id}/comments", response_model=List[schemas.CommentResponse]) def list_comments(issue_id: int, db: Session = Depends(get_db)): return db.query(models.Comment).filter(models.Comment.issue_id == issue_id).all() @router.patch("/comments/{comment_id}", response_model=schemas.CommentResponse) def update_comment(comment_id: int, comment_update: schemas.CommentUpdate, db: Session = Depends(get_db)): comment = db.query(models.Comment).filter(models.Comment.id == comment_id).first() if not comment: raise HTTPException(status_code=404, detail="Comment not found") for field, value in comment_update.model_dump(exclude_unset=True).items(): setattr(comment, field, value) db.commit() db.refresh(comment) return comment @router.delete("/comments/{comment_id}", status_code=status.HTTP_204_NO_CONTENT) def delete_comment(comment_id: int, db: Session = Depends(get_db)): comment = db.query(models.Comment).filter(models.Comment.id == comment_id).first() if not comment: raise HTTPException(status_code=404, detail="Comment not found") db.delete(comment) db.commit() return None