Spaces:
Runtime error
Runtime error
File size: 10,283 Bytes
094b94b | 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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | """
Todo API routes.
Provides endpoints for todo CRUD operations.
"""
from typing import Optional
from uuid import UUID
from datetime import datetime
from fastapi import APIRouter, HTTPException, Query, status, Depends
from sqlmodel import Session, select
from src.api.deps import get_current_user_id, get_db
from src.models.todo import Priority, Status, Todo
from src.schemas.todo import TodoCreateRequest, TodoResponse, TodoUpdateRequest
router = APIRouter()
@router.get(
'/',
response_model=list[TodoResponse],
summary='List todos',
description='Get all todos for the current user with optional filtering',
)
async def list_todos(
skip: int = Query(0, ge=0, description='Number of todos to skip'),
limit: int = Query(20, ge=1, le=100, description='Number of todos to return'),
status_filter: Optional[str] = Query(None, alias='status', description='Filter by status'),
priority: Optional[str] = Query(None, description='Filter by priority'),
search: Optional[str] = Query(None, description='Search in title and description'),
sort_by: str = Query('created_at', description='Sort by field'),
current_user_id: str = Depends(get_current_user_id),
db: Session = Depends(get_db),
):
"""List todos for the current user with filtering and pagination."""
# Build base query with user isolation
query = select(Todo).where(Todo.user_id == UUID(current_user_id))
# Apply filters
if status_filter:
query = query.where(Todo.status == Status(status_filter))
if priority:
query = query.where(Todo.priority == Priority(priority))
if search:
search_pattern = f'%{search}%'
query = query.where(
(Todo.title.ilike(search_pattern)) | (Todo.description.ilike(search_pattern))
)
# Apply sorting
if sort_by == 'created_at':
query = query.order_by(Todo.created_at.desc())
elif sort_by == 'due_date':
query = query.order_by(Todo.due_date.asc().nulls_last())
elif sort_by == 'priority':
query = query.order_by(Todo.priority.desc())
# Apply pagination
query = query.offset(skip).limit(limit)
# Execute query
todos = db.exec(query).all()
return [
TodoResponse(
id=str(todo.id),
user_id=str(todo.user_id),
title=todo.title,
description=todo.description,
status=todo.status.value,
priority=todo.priority.value,
tags=todo.tags,
due_date=todo.due_date.isoformat() if todo.due_date else None,
reminder_sent=todo.reminder_sent,
created_at=todo.created_at.isoformat(),
updated_at=todo.updated_at.isoformat(),
)
for todo in todos
]
@router.post(
'/',
response_model=TodoResponse,
status_code=status.HTTP_201_CREATED,
summary='Create todo',
description='Create a new todo',
)
async def create_todo(
todo_data: TodoCreateRequest,
current_user_id: str = Depends(get_current_user_id),
db: Session = Depends(get_db),
):
"""Create a new todo for the current user."""
todo = Todo(
title=todo_data.title,
description=todo_data.description,
priority=Priority(todo_data.priority) if todo_data.priority else Priority.MEDIUM,
due_date=todo_data.due_date,
tags=todo_data.tags,
user_id=UUID(current_user_id),
status=Status.PENDING,
)
db.add(todo)
db.commit()
db.refresh(todo)
return TodoResponse(
id=str(todo.id),
user_id=str(todo.user_id),
title=todo.title,
description=todo.description,
status=todo.status.value,
priority=todo.priority.value,
tags=todo.tags,
due_date=todo.due_date.isoformat() if todo.due_date else None,
reminder_sent=todo.reminder_sent,
created_at=todo.created_at.isoformat(),
updated_at=todo.updated_at.isoformat(),
)
# IMPORTANT: More specific routes must come BEFORE parameterized routes
@router.post(
'/{todo_id}/toggle',
response_model=TodoResponse,
summary='Toggle todo completion (POST)',
description='Toggle todo completion status - POST method for frontend compatibility',
)
async def toggle_todo_post(
todo_id: str,
current_user_id: str = Depends(get_current_user_id),
db: Session = Depends(get_db),
):
"""Toggle todo completion status using POST method."""
query = select(Todo).where(
Todo.id == UUID(todo_id),
Todo.user_id == UUID(current_user_id)
)
todo = db.exec(query).first()
if not todo:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail='Todo not found',
)
# Toggle status - flip between completed and pending
if todo.status == Status.PENDING:
todo.status = Status.COMPLETED
if not todo.completed_at:
todo.completed_at = datetime.utcnow()
else:
todo.status = Status.PENDING
todo.completed_at = None
db.add(todo)
db.commit()
db.refresh(todo)
return TodoResponse(
id=str(todo.id),
user_id=str(todo.user_id),
title=todo.title,
description=todo.description,
status=todo.status.value,
priority=todo.priority.value,
tags=todo.tags,
due_date=todo.due_date.isoformat() if todo.due_date else None,
reminder_sent=todo.reminder_sent,
created_at=todo.created_at.isoformat(),
updated_at=todo.updated_at.isoformat(),
)
@router.patch(
'/{todo_id}/complete',
response_model=TodoResponse,
summary='Toggle todo completion',
description='Toggle todo completion status',
)
async def toggle_complete(
todo_id: str,
completed: bool = True,
current_user_id: str = Depends(get_current_user_id),
db: Session = Depends(get_db),
):
"""Toggle todo completion status."""
query = select(Todo).where(
Todo.id == UUID(todo_id),
Todo.user_id == UUID(current_user_id)
)
todo = db.exec(query).first()
if not todo:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail='Todo not found',
)
# Toggle status
todo.status = Status.COMPLETED if completed else Status.PENDING
if completed and not todo.completed_at:
todo.completed_at = datetime.utcnow()
elif not completed:
todo.completed_at = None
db.add(todo)
db.commit()
db.refresh(todo)
return TodoResponse(
id=str(todo.id),
user_id=str(todo.user_id),
title=todo.title,
description=todo.description,
status=todo.status.value,
priority=todo.priority.value,
tags=todo.tags,
due_date=todo.due_date.isoformat() if todo.due_date else None,
reminder_sent=todo.reminder_sent,
created_at=todo.created_at.isoformat(),
updated_at=todo.updated_at.isoformat(),
)
@router.get(
'/{todo_id}',
response_model=TodoResponse,
summary='Get todo',
description='Get a specific todo by ID',
)
async def get_todo(
todo_id: str,
current_user_id: str = Depends(get_current_user_id),
db: Session = Depends(get_db),
):
"""Get a specific todo."""
query = select(Todo).where(
Todo.id == UUID(todo_id),
Todo.user_id == UUID(current_user_id)
)
todo = db.exec(query).first()
if not todo:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail='Todo not found',
)
return TodoResponse(
id=str(todo.id),
user_id=str(todo.user_id),
title=todo.title,
description=todo.description,
status=todo.status.value,
priority=todo.priority.value,
tags=todo.tags,
due_date=todo.due_date.isoformat() if todo.due_date else None,
reminder_sent=todo.reminder_sent,
created_at=todo.created_at.isoformat(),
updated_at=todo.updated_at.isoformat(),
)
@router.put(
'/{todo_id}',
response_model=TodoResponse,
summary='Update todo',
description='Update a todo',
)
async def update_todo(
todo_id: str,
todo_data: TodoUpdateRequest,
current_user_id: str = Depends(get_current_user_id),
db: Session = Depends(get_db),
):
"""Update a todo."""
query = select(Todo).where(
Todo.id == UUID(todo_id),
Todo.user_id == UUID(current_user_id)
)
todo = db.exec(query).first()
if not todo:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail='Todo not found',
)
# Update fields
if todo_data.title is not None:
todo.title = todo_data.title
if todo_data.description is not None:
todo.description = todo_data.description
if todo_data.priority is not None:
todo.priority = Priority(todo_data.priority)
if todo_data.due_date is not None:
todo.due_date = todo_data.due_date
if todo_data.tags is not None:
todo.tags = todo_data.tags
db.add(todo)
db.commit()
db.refresh(todo)
return TodoResponse(
id=str(todo.id),
user_id=str(todo.user_id),
title=todo.title,
description=todo.description,
status=todo.status.value,
priority=todo.priority.value,
tags=todo.tags,
due_date=todo.due_date.isoformat() if todo.due_date else None,
reminder_sent=todo.reminder_sent,
created_at=todo.created_at.isoformat(),
updated_at=todo.updated_at.isoformat(),
)
@router.delete(
'/{todo_id}',
status_code=status.HTTP_204_NO_CONTENT,
summary='Delete todo',
description='Delete a todo',
)
async def delete_todo(
todo_id: str,
current_user_id: str = Depends(get_current_user_id),
db: Session = Depends(get_db),
):
"""Delete a todo."""
query = select(Todo).where(
Todo.id == UUID(todo_id),
Todo.user_id == UUID(current_user_id)
)
todo = db.exec(query).first()
if not todo:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail='Todo not found',
)
db.delete(todo)
db.commit()
return None
__all__ = ['router']
|