Automated Action 53566813bd Complete Todo API implementation with FastAPI and SQLite
- Add SQLite database configuration
- Create Todo model, schemas, and CRUD operations
- Implement Todo API endpoints
- Add Alembic migration for todo table
- Set up database initialization in main.py
- Update README with project details and instructions
- Add pyproject.toml with Ruff configuration
2025-06-10 15:40:42 +00:00

110 lines
2.6 KiB
Python

from typing import Any, Optional
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from app.api.deps import get_db
from app.crud import todo as crud_todo
from app.schemas.todo import Todo, TodoCreate, TodoList, TodoUpdate
router = APIRouter()
@router.get("/", response_model=TodoList)
def read_todos(
db: Session = Depends(get_db),
skip: int = 0,
limit: int = 100,
completed: Optional[bool] = None,
title: Optional[str] = None,
) -> Any:
"""
Retrieve todos.
- **skip**: Number of records to skip for pagination
- **limit**: Maximum number of records to return
- **completed**: Filter by completion status
- **title**: Filter by title (partial match)
"""
filters = {}
if completed is not None:
filters["completed"] = completed
if title:
filters["title"] = title
todos = crud_todo.get_multi(db, skip=skip, limit=limit, filters=filters)
count = crud_todo.count(db, filters=filters)
return {"items": todos, "count": count}
@router.post("/", response_model=Todo, status_code=status.HTTP_201_CREATED)
def create_todo(
*,
db: Session = Depends(get_db),
todo_in: TodoCreate,
) -> Any:
"""
Create new todo.
"""
todo = crud_todo.create(db, obj_in=todo_in)
return todo
@router.get("/{todo_id}", response_model=Todo)
def read_todo(
*,
db: Session = Depends(get_db),
todo_id: int,
) -> Any:
"""
Get todo by ID.
"""
todo = crud_todo.get(db, todo_id=todo_id)
if not todo:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Todo not found"
)
return todo
@router.put("/{todo_id}", response_model=Todo)
def update_todo(
*,
db: Session = Depends(get_db),
todo_id: int,
todo_in: TodoUpdate,
) -> Any:
"""
Update a todo.
"""
todo = crud_todo.get(db, todo_id=todo_id)
if not todo:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Todo not found"
)
todo = crud_todo.update(db, db_obj=todo, obj_in=todo_in)
return todo
@router.delete(
"/{todo_id}",
status_code=status.HTTP_204_NO_CONTENT,
response_model=None
)
def delete_todo(
*,
db: Session = Depends(get_db),
todo_id: int,
) -> None:
"""
Delete a todo.
"""
todo = crud_todo.get(db, todo_id=todo_id)
if not todo:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Todo not found"
)
crud_todo.remove(db, todo_id=todo_id)