Spaces:
Sleeping
Sleeping
File size: 7,605 Bytes
507973e | 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 | """Todo API endpoints for the Phase II Full-Stack Todo App."""
from fastapi import APIRouter, Depends, HTTPException, status, Query
from sqlmodel import Session
from typing import List, Optional
from ..models.todo import Todo, TodoCreate, TodoUpdate, TodoRead
from ..services.todo_service import TodoService
from ..core.database import get_session
from ..core.security import SecurityUtils
from ..models.user import User
router = APIRouter(prefix="/todos", tags=["Todos"])
@router.get("/", response_model=dict)
def get_todos(
status_filter: Optional[str] = Query(None, alias="status", regex="^(all|pending|completed)$"),
limit: int = Query(50, ge=1, le=100),
offset: int = Query(0, ge=0),
current_user: User = Depends(SecurityUtils.get_current_user_from_token),
session: Session = Depends(get_session)
):
"""
Get all todos for the current user.
Args:
status_filter: Optional filter for todo status ("all", "pending", "completed")
limit: Number of items to return (default: 50, max: 100)
offset: Offset for pagination (default: 0)
current_user: Current authenticated user
session: Database session
Returns:
Dict containing todos and pagination info
"""
todos = TodoService.get_todos_by_user(session, current_user.id, status_filter)
# Apply pagination
paginated_todos = todos[offset:offset + limit]
# Convert to read format
todo_list = []
for todo in paginated_todos:
todo_list.append(
TodoRead.from_orm(todo) if hasattr(TodoRead, 'from_orm') else {
"id": todo.id,
"title": todo.title,
"description": todo.description,
"completed": todo.completed,
"user_id": todo.user_id,
"created_at": todo.created_at,
"updated_at": todo.updated_at
}
)
return {
"success": True,
"data": {
"todos": todo_list,
"pagination": {
"total": len(todos),
"limit": limit,
"offset": offset
}
},
"message": "Todos retrieved successfully"
}
@router.post("/", response_model=dict)
def create_todo(
todo_create: TodoCreate,
current_user: User = Depends(SecurityUtils.get_current_user_from_token),
session: Session = Depends(get_session)
):
"""
Create a new todo item.
Args:
todo_create: Todo creation data
current_user: Current authenticated user
session: Database session
Returns:
Dict containing created todo
"""
todo = TodoService.create_todo(session, todo_create, current_user.id)
return {
"success": True,
"data": {
"todo": TodoRead.from_orm(todo) if hasattr(TodoRead, 'from_orm') else {
"id": todo.id,
"title": todo.title,
"description": todo.description,
"completed": todo.completed,
"user_id": todo.user_id,
"created_at": todo.created_at,
"updated_at": todo.updated_at
}
},
"message": "Todo created successfully"
}
@router.get("/{todo_id}", response_model=dict)
def get_todo(
todo_id: int,
current_user: User = Depends(SecurityUtils.get_current_user_from_token),
session: Session = Depends(get_session)
):
"""
Get a specific todo item.
Args:
todo_id: Todo ID
current_user: Current authenticated user
session: Database session
Returns:
Dict containing todo data
"""
todo = TodoService.get_todo_by_id(session, todo_id, current_user.id)
if not todo:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Todo not found"
)
return {
"success": True,
"data": {
"todo": TodoRead.from_orm(todo) if hasattr(TodoRead, 'from_orm') else {
"id": todo.id,
"title": todo.title,
"description": todo.description,
"completed": todo.completed,
"user_id": todo.user_id,
"created_at": todo.created_at,
"updated_at": todo.updated_at
}
},
"message": "Todo retrieved successfully"
}
@router.put("/{todo_id}", response_model=dict)
def update_todo(
todo_id: int,
todo_update: TodoUpdate,
current_user: User = Depends(SecurityUtils.get_current_user_from_token),
session: Session = Depends(get_session)
):
"""
Update a specific todo item.
Args:
todo_id: Todo ID to update
todo_update: Todo update data
current_user: Current authenticated user
session: Database session
Returns:
Dict containing updated todo
"""
todo = TodoService.update_todo(session, todo_id, todo_update, current_user.id)
if not todo:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Todo not found"
)
return {
"success": True,
"data": {
"todo": TodoRead.from_orm(todo) if hasattr(TodoRead, 'from_orm') else {
"id": todo.id,
"title": todo.title,
"description": todo.description,
"completed": todo.completed,
"user_id": todo.user_id,
"created_at": todo.created_at,
"updated_at": todo.updated_at
}
},
"message": "Todo updated successfully"
}
@router.patch("/{todo_id}/toggle", response_model=dict)
def toggle_todo_completion(
todo_id: int,
current_user: User = Depends(SecurityUtils.get_current_user_from_token),
session: Session = Depends(get_session)
):
"""
Toggle the completion status of a todo item.
Args:
todo_id: Todo ID to toggle
current_user: Current authenticated user
session: Database session
Returns:
Dict containing updated todo
"""
todo = TodoService.toggle_todo_completion(session, todo_id, current_user.id)
if not todo:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Todo not found"
)
return {
"success": True,
"data": {
"todo": TodoRead.from_orm(todo) if hasattr(TodoRead, 'from_orm') else {
"id": todo.id,
"title": todo.title,
"description": todo.description,
"completed": todo.completed,
"user_id": todo.user_id,
"created_at": todo.created_at,
"updated_at": todo.updated_at
}
},
"message": "Todo status toggled successfully"
}
@router.delete("/{todo_id}", response_model=dict)
def delete_todo(
todo_id: int,
current_user: User = Depends(SecurityUtils.get_current_user_from_token),
session: Session = Depends(get_session)
):
"""
Delete a specific todo item.
Args:
todo_id: Todo ID to delete
current_user: Current authenticated user
session: Database session
Returns:
Dict containing success message
"""
success = TodoService.delete_todo(session, todo_id, current_user.id)
if not success:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Todo not found"
)
return {
"success": True,
"message": "Todo deleted successfully"
} |