Spaces:
Runtime error
Runtime error
File size: 6,176 Bytes
18ade12 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | """
Task router for the Task Management API.
REST API endpoints for task CRUD operations with JWT authentication.
Per contracts/openapi.yaml specification.
"""
from fastapi import APIRouter, Body, Depends, HTTPException, Path, status
from uuid import UUID
from ..auth.dependencies import ValidatedUser, get_validated_user
from ..db.session import get_session
from ..schemas.task import TaskCreate, TaskUpdate, TaskResponse
from ..services.task_service import task_service
router = APIRouter(prefix="/api/{user_id}/tasks", tags=["tasks"])
@router.post("/", response_model=TaskResponse, status_code=status.HTTP_201_CREATED)
async def create_task(
user_id: str = Path(..., description="User ID from path"),
task_data: TaskCreate = Body(...),
validated_user: ValidatedUser = Depends(get_validated_user),
db_session = Depends(get_session)
):
"""Create a new task for the authenticated user."""
# Create the task using the service
task = await task_service.create_task(
db_session=db_session,
user_id=user_id,
task_data=task_data
)
# Convert SQLModel to Pydantic response model
return TaskResponse(
id=task.id,
user_id=task.user_id,
title=task.title,
description=task.description,
is_completed=task.is_completed,
created_at=task.created_at,
updated_at=task.updated_at
)
@router.get("/")
async def get_tasks(
user_id: str = Path(..., description="User ID from path"),
validated_user: ValidatedUser = Depends(get_validated_user),
limit: int = 50,
offset: int = 0,
db_session = Depends(get_session)
):
"""Get all tasks for the authenticated user."""
# Get tasks using the service
tasks, total = await task_service.get_tasks_by_user(
db_session=db_session,
user_id=user_id,
limit=limit,
offset=offset
)
# Convert SQLModels to Pydantic response models
task_responses = [
TaskResponse(
id=task.id,
user_id=task.user_id,
title=task.title,
description=task.description,
is_completed=task.is_completed,
created_at=task.created_at,
updated_at=task.updated_at
) for task in tasks
]
return {"tasks": task_responses, "total": total, "limit": limit, "offset": offset}
@router.get("/{task_id}/", response_model=TaskResponse)
async def get_task(
user_id: str = Path(..., description="User ID from path"),
task_id: UUID = Path(..., description="Task ID from path"),
validated_user: ValidatedUser = Depends(get_validated_user),
db_session = Depends(get_session)
):
"""Get a specific task for the authenticated user."""
# Get task using the service
task = await task_service.get_task_by_id(
db_session=db_session,
user_id=user_id,
task_id=task_id
)
if not task:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Task not found"
)
# Convert SQLModel to Pydantic response model
return TaskResponse(
id=task.id,
user_id=task.user_id,
title=task.title,
description=task.description,
is_completed=task.is_completed,
created_at=task.created_at,
updated_at=task.updated_at
)
@router.put("/{task_id}/", response_model=TaskResponse)
async def update_task(
user_id: str = Path(..., description="User ID from path"),
task_id: UUID = Path(..., description="Task ID from path"),
task_data: TaskUpdate = Body(...),
validated_user: ValidatedUser = Depends(get_validated_user),
db_session = Depends(get_session)
):
"""Update a specific task for the authenticated user."""
# Update task using the service
task = await task_service.update_task(
db_session=db_session,
user_id=user_id,
task_id=task_id,
task_data=task_data
)
if not task:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Task not found"
)
# Convert SQLModel to Pydantic response model
return TaskResponse(
id=task.id,
user_id=task.user_id,
title=task.title,
description=task.description,
is_completed=task.is_completed,
created_at=task.created_at,
updated_at=task.updated_at
)
@router.delete("/{task_id}/", status_code=status.HTTP_204_NO_CONTENT)
async def delete_task(
user_id: str = Path(..., description="User ID from path"),
task_id: UUID = Path(..., description="Task ID from path"),
validated_user: ValidatedUser = Depends(get_validated_user),
db_session = Depends(get_session)
):
"""Delete a specific task for the authenticated user."""
# Delete task using the service
success = await task_service.delete_task(
db_session=db_session,
user_id=user_id,
task_id=task_id
)
if not success:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Task not found"
)
return
@router.patch("/{task_id}/complete/", response_model=TaskResponse)
async def toggle_task_completion(
user_id: str = Path(..., description="User ID from path"),
task_id: UUID = Path(..., description="Task ID from path"),
validated_user: ValidatedUser = Depends(get_validated_user),
db_session = Depends(get_session)
):
"""Toggle the completion status of a specific task for the authenticated user."""
# Toggle task completion using the service
task = await task_service.toggle_task_completion(
db_session=db_session,
user_id=user_id,
task_id=task_id
)
if not task:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Task not found"
)
# Convert SQLModel to Pydantic response model
return TaskResponse(
id=task.id,
user_id=task.user_id,
title=task.title,
description=task.description,
is_completed=task.is_completed,
created_at=task.created_at,
updated_at=task.updated_at
)
|