Spaces:
Sleeping
Sleeping
Download build/lib/api/todos.py from hashir-qt/tasklert-api: direct link, hf CLI and curl.
- Browser
- Download file 7.61 kB
-
https://huggingface.co/spaces/hashir-qt/tasklert-api/resolve/main/build/lib/api/todos.py
- Command line
-
hf download hf://spaces/hashir-qt/tasklert-api/build/lib/api/todos.py
-
curl -L -o todos.py https://huggingface.co/spaces/hashir-qt/tasklert-api/resolve/main/build/lib/api/todos.py
7.61 kB
| """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"]) | |
| 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" | |
| } | |
| 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" | |
| } | |
| 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" | |
| } | |
| 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" | |
| } | |
| 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" | |
| } | |
| 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" | |
| } |