Spaces:
Sleeping
Sleeping
| """ | |
| Ticket Comments API - Team Collaboration | |
| Endpoints for: | |
| 1. Create comments on tickets | |
| 2. Update comments (by author) | |
| 3. Delete comments (by author or PM) | |
| 4. List comments with filtering | |
| 5. Get comment replies (threading) | |
| Authorization: | |
| - All authenticated users can create comments | |
| - Only author can edit their own comments | |
| - Author or PM can delete comments | |
| """ | |
| from fastapi import APIRouter, Depends, status, Query | |
| from sqlalchemy.orm import Session | |
| from typing import Optional, List | |
| from uuid import UUID | |
| from app.api.deps import get_db, get_current_user | |
| from app.models.user import User | |
| from app.services.ticket_comment_service import TicketCommentService | |
| from app.schemas.ticket_comment import ( | |
| TicketCommentCreate, | |
| TicketCommentUpdate, | |
| TicketCommentResponse, | |
| TicketCommentListResponse, | |
| COMMENT_TYPES | |
| ) | |
| router = APIRouter() | |
| # ============================================ | |
| # CREATE COMMENT | |
| # ============================================ | |
| def create_comment( | |
| ticket_id: UUID, | |
| data: TicketCommentCreate, | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(get_current_user) | |
| ): | |
| """Create a new comment on a ticket""" | |
| return TicketCommentService.create_comment( | |
| ticket_id=ticket_id, | |
| data=data, | |
| current_user=current_user, | |
| db=db | |
| ) | |
| # ============================================ | |
| # UPDATE COMMENT | |
| # ============================================ | |
| def update_comment( | |
| comment_id: UUID, | |
| data: TicketCommentUpdate, | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(get_current_user) | |
| ): | |
| """Update a comment (author only)""" | |
| return TicketCommentService.update_comment( | |
| comment_id=comment_id, | |
| data=data, | |
| current_user=current_user, | |
| db=db | |
| ) | |
| # ============================================ | |
| # DELETE COMMENT | |
| # ============================================ | |
| def delete_comment( | |
| comment_id: UUID, | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(get_current_user) | |
| ): | |
| """Delete a comment (soft delete)""" | |
| return TicketCommentService.delete_comment( | |
| comment_id=comment_id, | |
| current_user=current_user, | |
| db=db | |
| ) | |
| # ============================================ | |
| # GET SINGLE COMMENT | |
| # ============================================ | |
| def get_comment( | |
| comment_id: UUID, | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(get_current_user) | |
| ): | |
| """Get a single comment""" | |
| return TicketCommentService.get_comment( | |
| comment_id=comment_id, | |
| db=db | |
| ) | |
| # ============================================ | |
| # LIST COMMENTS | |
| # ============================================ | |
| def list_comments( | |
| ticket_id: UUID, | |
| page: int = Query(1, ge=1, description="Page number"), | |
| page_size: int = Query(50, ge=1, le=100, description="Items per page"), | |
| is_internal: Optional[bool] = Query(None, description="Filter by internal/external"), | |
| comment_type: Optional[str] = Query(None, description=f"Filter by type: {', '.join(COMMENT_TYPES)}"), | |
| parent_only: bool = Query(False, description="Show only top-level comments"), | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(get_current_user) | |
| ): | |
| """List comments for a ticket""" | |
| return TicketCommentService.list_comments( | |
| ticket_id=ticket_id, | |
| page=page, | |
| page_size=page_size, | |
| is_internal=is_internal, | |
| comment_type=comment_type, | |
| parent_only=parent_only, | |
| db=db | |
| ) | |
| # ============================================ | |
| # GET COMMENT REPLIES | |
| # ============================================ | |
| def get_comment_replies( | |
| comment_id: UUID, | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(get_current_user) | |
| ): | |
| """Get all replies to a comment""" | |
| return TicketCommentService.get_comment_replies( | |
| comment_id=comment_id, | |
| db=db | |
| ) | |