|
| 1 | +from typing import Optional |
| 2 | +from uuid import UUID |
| 3 | + |
| 4 | +from fastapi import APIRouter, Depends, HTTPException, Query |
| 5 | + |
| 6 | +from app.middleware.auth import has_roles |
| 7 | +from app.schemas.task import ( |
| 8 | + TaskAssignRequest, |
| 9 | + TaskCreateRequest, |
| 10 | + TaskListResponse, |
| 11 | + TaskResponse, |
| 12 | + TaskUpdateRequest, |
| 13 | +) |
| 14 | +from app.schemas.user import UserRole |
| 15 | +from app.services.implementations.task_service import TaskService |
| 16 | +from app.utilities.service_utils import get_task_service |
| 17 | + |
| 18 | +router = APIRouter( |
| 19 | + prefix="/tasks", |
| 20 | + tags=["tasks"], |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +@router.post("/", response_model=TaskResponse) |
| 25 | +async def create_task( |
| 26 | + task: TaskCreateRequest, |
| 27 | + task_service: TaskService = Depends(get_task_service), |
| 28 | + authorized: bool = has_roles([UserRole.ADMIN]), |
| 29 | +): |
| 30 | + """ |
| 31 | + Create a new task (admin only). |
| 32 | + This endpoint can also be called internally from other services to automatically create tasks. |
| 33 | + """ |
| 34 | + try: |
| 35 | + return await task_service.create_task(task) |
| 36 | + except HTTPException as http_ex: |
| 37 | + raise http_ex |
| 38 | + except Exception as e: |
| 39 | + raise HTTPException(status_code=500, detail=str(e)) |
| 40 | + |
| 41 | + |
| 42 | +@router.get("/", response_model=TaskListResponse) |
| 43 | +async def get_tasks( |
| 44 | + status: Optional[str] = Query(None, description="Filter by task status"), |
| 45 | + priority: Optional[str] = Query(None, description="Filter by task priority"), |
| 46 | + task_type: Optional[str] = Query(None, description="Filter by task type"), |
| 47 | + assignee_id: Optional[str] = Query(None, description="Filter by assignee ID"), |
| 48 | + task_service: TaskService = Depends(get_task_service), |
| 49 | + authorized: bool = has_roles([UserRole.ADMIN]), |
| 50 | +): |
| 51 | + """ |
| 52 | + Get all tasks with optional filters (admin only) |
| 53 | + """ |
| 54 | + try: |
| 55 | + assignee_uuid = UUID(assignee_id) if assignee_id else None |
| 56 | + tasks = await task_service.get_all_tasks( |
| 57 | + status=status, priority=priority, task_type=task_type, assignee_id=assignee_uuid |
| 58 | + ) |
| 59 | + return TaskListResponse(tasks=tasks, total=len(tasks)) |
| 60 | + except ValueError: |
| 61 | + raise HTTPException(status_code=400, detail="Invalid assignee_id format") |
| 62 | + except HTTPException as http_ex: |
| 63 | + raise http_ex |
| 64 | + except Exception as e: |
| 65 | + raise HTTPException(status_code=500, detail=str(e)) |
| 66 | + |
| 67 | + |
| 68 | +@router.get("/{task_id}", response_model=TaskResponse) |
| 69 | +async def get_task( |
| 70 | + task_id: str, |
| 71 | + task_service: TaskService = Depends(get_task_service), |
| 72 | + authorized: bool = has_roles([UserRole.ADMIN]), |
| 73 | +): |
| 74 | + """ |
| 75 | + Get a single task by ID (admin only) |
| 76 | + """ |
| 77 | + try: |
| 78 | + return await task_service.get_task_by_id(UUID(task_id)) |
| 79 | + except ValueError: |
| 80 | + raise HTTPException(status_code=400, detail="Invalid task ID format") |
| 81 | + except HTTPException as http_ex: |
| 82 | + raise http_ex |
| 83 | + except Exception as e: |
| 84 | + raise HTTPException(status_code=500, detail=str(e)) |
| 85 | + |
| 86 | + |
| 87 | +@router.put("/{task_id}", response_model=TaskResponse) |
| 88 | +async def update_task( |
| 89 | + task_id: str, |
| 90 | + task_update: TaskUpdateRequest, |
| 91 | + task_service: TaskService = Depends(get_task_service), |
| 92 | + authorized: bool = has_roles([UserRole.ADMIN]), |
| 93 | +): |
| 94 | + """ |
| 95 | + Update a task (admin only) |
| 96 | + """ |
| 97 | + try: |
| 98 | + return await task_service.update_task(UUID(task_id), task_update) |
| 99 | + except ValueError: |
| 100 | + raise HTTPException(status_code=400, detail="Invalid task ID format") |
| 101 | + except HTTPException as http_ex: |
| 102 | + raise http_ex |
| 103 | + except Exception as e: |
| 104 | + raise HTTPException(status_code=500, detail=str(e)) |
| 105 | + |
| 106 | + |
| 107 | +@router.put("/{task_id}/assign", response_model=TaskResponse) |
| 108 | +async def assign_task( |
| 109 | + task_id: str, |
| 110 | + assign_request: TaskAssignRequest, |
| 111 | + task_service: TaskService = Depends(get_task_service), |
| 112 | + authorized: bool = has_roles([UserRole.ADMIN]), |
| 113 | +): |
| 114 | + """ |
| 115 | + Assign a task to an admin user (admin only) |
| 116 | + """ |
| 117 | + try: |
| 118 | + return await task_service.assign_task(UUID(task_id), assign_request.assignee_id) |
| 119 | + except ValueError: |
| 120 | + raise HTTPException(status_code=400, detail="Invalid task ID format") |
| 121 | + except HTTPException as http_ex: |
| 122 | + raise http_ex |
| 123 | + except Exception as e: |
| 124 | + raise HTTPException(status_code=500, detail=str(e)) |
| 125 | + |
| 126 | + |
| 127 | +@router.put("/{task_id}/complete", response_model=TaskResponse) |
| 128 | +async def complete_task( |
| 129 | + task_id: str, |
| 130 | + task_service: TaskService = Depends(get_task_service), |
| 131 | + authorized: bool = has_roles([UserRole.ADMIN]), |
| 132 | +): |
| 133 | + """ |
| 134 | + Mark a task as completed (admin only) |
| 135 | + """ |
| 136 | + try: |
| 137 | + return await task_service.complete_task(UUID(task_id)) |
| 138 | + except ValueError: |
| 139 | + raise HTTPException(status_code=400, detail="Invalid task ID format") |
| 140 | + except HTTPException as http_ex: |
| 141 | + raise http_ex |
| 142 | + except Exception as e: |
| 143 | + raise HTTPException(status_code=500, detail=str(e)) |
| 144 | + |
| 145 | + |
| 146 | +@router.delete("/{task_id}") |
| 147 | +async def delete_task( |
| 148 | + task_id: str, |
| 149 | + task_service: TaskService = Depends(get_task_service), |
| 150 | + authorized: bool = has_roles([UserRole.ADMIN]), |
| 151 | +): |
| 152 | + """ |
| 153 | + Delete a task (admin only) |
| 154 | + """ |
| 155 | + try: |
| 156 | + await task_service.delete_task(UUID(task_id)) |
| 157 | + return {"message": "Task deleted successfully"} |
| 158 | + except ValueError: |
| 159 | + raise HTTPException(status_code=400, detail="Invalid task ID format") |
| 160 | + except HTTPException as http_ex: |
| 161 | + raise http_ex |
| 162 | + except Exception as e: |
| 163 | + raise HTTPException(status_code=500, detail=str(e)) |
0 commit comments