Spaces:
Sleeping
Sleeping
Download src/api/todos.py from hashir-qt/tasklert-api: direct link, hf CLI and curl.
- Browser
- Download file 5.96 kB
-
https://huggingface.co/spaces/hashir-qt/tasklert-api/resolve/main/src/api/todos.py
- Command line
-
hf download hf://spaces/hashir-qt/tasklert-api/src/api/todos.py
-
curl -L -o todos.py https://huggingface.co/spaces/hashir-qt/tasklert-api/resolve/main/src/api/todos.py
5.96 kB
| """Todo API endpoints for Phase II Full-Stack Todo App.""" | |
| from typing import Annotated | |
| from fastapi import APIRouter, Depends, Header, HTTPException, Query, status | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| from ..core.database import get_session | |
| from ..core.better_auth import get_current_user_from_session | |
| from ..models.todo import TodoCreate, TodoUpdate | |
| from ..services import todo_service | |
| router = APIRouter(prefix="/todos", tags=["Todos"]) | |
| def _todo_dict(todo) -> dict: | |
| return { | |
| "id": todo.id, | |
| "title": todo.title, | |
| "description": todo.description, | |
| "completed": todo.completed, | |
| "user_id": todo.user_id, | |
| "priority": todo.priority, | |
| "tags": todo.tags or [], | |
| "due_date": todo.due_date.isoformat() if todo.due_date else None, | |
| "reminder_at": todo.reminder_at.isoformat() if todo.reminder_at else None, | |
| "recurrence": todo.recurrence, | |
| "parent_id": todo.parent_id, | |
| "created_at": todo.created_at.isoformat(), | |
| "updated_at": todo.updated_at.isoformat(), | |
| } | |
| async def list_todos( | |
| authorization: Annotated[str | None, Header()] = None, | |
| cookie: Annotated[str | None, Header()] = None, | |
| status_filter: str = Query("all", alias="status", pattern="^(all|pending|completed)$"), | |
| priority: str | None = Query(None, pattern="^(high|medium|low)$"), | |
| tag: str | None = Query(None), | |
| search: str | None = Query(None), | |
| sort_by: str = Query("created_at", pattern="^(created_at|due_date|priority|title)$"), | |
| sort_order: str = Query("desc", pattern="^(asc|desc)$"), | |
| due_before: str | None = Query(None), | |
| due_after: str | None = Query(None), | |
| limit: int = Query(50, ge=1, le=200), | |
| offset: int = Query(0, ge=0), | |
| session: AsyncSession = Depends(get_session), | |
| ): | |
| """List todos with filtering, search, and sorting.""" | |
| from datetime import datetime as dt | |
| current_user = await get_current_user_from_session(authorization, cookie, session) | |
| todos, total = await todo_service.list_todos( | |
| session, | |
| current_user.id, | |
| status=status_filter, | |
| priority=priority, | |
| tag=tag, | |
| search=search, | |
| sort_by=sort_by, | |
| sort_order=sort_order, | |
| due_before=dt.fromisoformat(due_before) if due_before else None, | |
| due_after=dt.fromisoformat(due_after) if due_after else None, | |
| limit=limit, | |
| offset=offset, | |
| ) | |
| return { | |
| "success": True, | |
| "data": { | |
| "todos": [_todo_dict(t) for t in todos], | |
| "pagination": {"total": total, "limit": limit, "offset": offset}, | |
| }, | |
| } | |
| async def create_todo( | |
| data: TodoCreate, | |
| authorization: Annotated[str | None, Header()] = None, | |
| cookie: Annotated[str | None, Header()] = None, | |
| session: AsyncSession = Depends(get_session), | |
| ): | |
| """Create a new todo item.""" | |
| current_user = await get_current_user_from_session(authorization, cookie, session) | |
| todo = await todo_service.create(session, data, current_user.id) | |
| return { | |
| "success": True, | |
| "data": {"todo": _todo_dict(todo)}, | |
| "message": "Todo created successfully", | |
| } | |
| async def get_todo( | |
| todo_id: int, | |
| authorization: Annotated[str | None, Header()] = None, | |
| cookie: Annotated[str | None, Header()] = None, | |
| session: AsyncSession = Depends(get_session), | |
| ): | |
| """Get a specific todo item.""" | |
| current_user = await get_current_user_from_session(authorization, cookie, session) | |
| todo = await todo_service.get(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": _todo_dict(todo)}} | |
| async def update_todo( | |
| todo_id: int, | |
| data: TodoUpdate, | |
| authorization: Annotated[str | None, Header()] = None, | |
| cookie: Annotated[str | None, Header()] = None, | |
| session: AsyncSession = Depends(get_session), | |
| ): | |
| """Update a specific todo item.""" | |
| current_user = await get_current_user_from_session(authorization, cookie, session) | |
| todo = await todo_service.update(session, todo_id, data, current_user.id) | |
| if not todo: | |
| raise HTTPException( | |
| status_code=status.HTTP_404_NOT_FOUND, detail="Todo not found" | |
| ) | |
| return { | |
| "success": True, | |
| "data": {"todo": _todo_dict(todo)}, | |
| "message": "Todo updated successfully", | |
| } | |
| async def delete_todo( | |
| todo_id: int, | |
| authorization: Annotated[str | None, Header()] = None, | |
| cookie: Annotated[str | None, Header()] = None, | |
| session: AsyncSession = Depends(get_session), | |
| ): | |
| """Delete a specific todo item.""" | |
| current_user = await get_current_user_from_session(authorization, cookie, session) | |
| success = await todo_service.delete(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"} | |
| async def toggle_todo( | |
| todo_id: int, | |
| authorization: Annotated[str | None, Header()] = None, | |
| cookie: Annotated[str | None, Header()] = None, | |
| session: AsyncSession = Depends(get_session), | |
| ): | |
| """Toggle the completion status of a todo item.""" | |
| current_user = await get_current_user_from_session(authorization, cookie, session) | |
| todo = await todo_service.toggle(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": _todo_dict(todo)}, | |
| "message": "Todo status toggled successfully", | |
| } | |