Spaces:
Sleeping
Sleeping
File size: 5,959 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 | """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(),
}
@router.get("")
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},
},
}
@router.post("", status_code=status.HTTP_201_CREATED)
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",
}
@router.get("/{todo_id}")
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)}}
@router.put("/{todo_id}")
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",
}
@router.delete("/{todo_id}")
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"}
@router.patch("/{todo_id}/toggle")
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",
}
|