Automated Action 1fca93299a Complete Todo App implementation
- Added filtering and pagination for todo listings
- Fixed Alembic migration setup
- Enhanced CRUD operations
- Updated documentation with comprehensive README
- Linted code with Ruff
2025-06-17 02:13:03 +00:00

116 lines
3.0 KiB
Python

from typing import Any, List, Optional
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from app.api.deps import get_current_active_user, get_db
from app.crud.crud_todo import (
create_todo,
delete_todo,
get_todo,
get_todos,
update_todo,
)
from app.models.user import User
from app.schemas.todo import Todo, TodoCreate, TodoUpdate
router = APIRouter()
@router.get("/", response_model=List[Todo])
def read_todos(
db: Session = Depends(get_db),
skip: int = 0,
limit: int = 100,
title: Optional[str] = None,
is_completed: Optional[bool] = None,
current_user: User = Depends(get_current_active_user),
) -> Any:
"""
Retrieve todos with optional filtering.
- **skip**: Number of records to skip for pagination
- **limit**: Maximum number of records to return
- **title**: Filter by title (contains search)
- **is_completed**: Filter by completion status
"""
todos = get_todos(
db=db,
owner_id=current_user.id,
skip=skip,
limit=limit,
title=title,
is_completed=is_completed
)
return todos
@router.post("/", response_model=Todo)
def create_todo_item(
*,
db: Session = Depends(get_db),
todo_in: TodoCreate,
current_user: User = Depends(get_current_active_user),
) -> Any:
"""
Create new todo.
"""
todo = create_todo(db=db, todo_in=todo_in, owner_id=current_user.id)
return todo
@router.get("/{id}", response_model=Todo)
def read_todo(
*,
db: Session = Depends(get_db),
id: int,
current_user: User = Depends(get_current_active_user),
) -> Any:
"""
Get todo by ID.
"""
todo = get_todo(db=db, todo_id=id)
if not todo:
raise HTTPException(status_code=404, detail="Todo not found")
if todo.owner_id != current_user.id:
raise HTTPException(status_code=400, detail="Not enough permissions")
return todo
@router.put("/{id}", response_model=Todo)
def update_todo_item(
*,
db: Session = Depends(get_db),
id: int,
todo_in: TodoUpdate,
current_user: User = Depends(get_current_active_user),
) -> Any:
"""
Update a todo.
"""
todo = get_todo(db=db, todo_id=id)
if not todo:
raise HTTPException(status_code=404, detail="Todo not found")
if todo.owner_id != current_user.id:
raise HTTPException(status_code=400, detail="Not enough permissions")
todo = update_todo(db=db, db_obj=todo, obj_in=todo_in)
return todo
@router.delete("/{id}", response_model=None, status_code=status.HTTP_204_NO_CONTENT)
def delete_todo_item(
*,
db: Session = Depends(get_db),
id: int,
current_user: User = Depends(get_current_active_user),
) -> Any:
"""
Delete a todo.
"""
todo = get_todo(db=db, todo_id=id)
if not todo:
raise HTTPException(status_code=404, detail="Todo not found")
if todo.owner_id != current_user.id:
raise HTTPException(status_code=400, detail="Not enough permissions")
delete_todo(db=db, todo_id=id)
return None