
- Set up project structure and FastAPI application - Create Todo database model with SQLAlchemy - Configure Alembic for database migrations - Implement CRUD endpoints for managing Todo items - Add health check endpoint - Include comprehensive documentation in README.md - Configure and apply Ruff linting
70 lines
2.0 KiB
Python
70 lines
2.0 KiB
Python
from typing import List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import crud
|
|
from app.db.session import get_db
|
|
from app.schemas.todo import Todo, TodoCreate, TodoUpdate
|
|
|
|
router = APIRouter(prefix="/todos", tags=["todos"])
|
|
|
|
|
|
@router.get("/", response_model=List[Todo])
|
|
def get_todos(
|
|
skip: int = Query(0, description="Skip the first N items"),
|
|
limit: int = Query(100, description="Limit the number of items returned"),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""
|
|
Get all todos with pagination
|
|
"""
|
|
return crud.todo.get_todos(db=db, skip=skip, limit=limit)
|
|
|
|
|
|
@router.post("/", response_model=Todo, status_code=status.HTTP_201_CREATED)
|
|
def create_todo(todo: TodoCreate, db: Session = Depends(get_db)):
|
|
"""
|
|
Create a new todo
|
|
"""
|
|
return crud.todo.create_todo(db=db, todo=todo)
|
|
|
|
|
|
@router.get("/{todo_id}", response_model=Todo)
|
|
def get_todo(todo_id: int, db: Session = Depends(get_db)):
|
|
"""
|
|
Get a specific todo by ID
|
|
"""
|
|
db_todo = crud.todo.get_todo(db=db, todo_id=todo_id)
|
|
if db_todo is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail=f"Todo with ID {todo_id} not found"
|
|
)
|
|
return db_todo
|
|
|
|
|
|
@router.put("/{todo_id}", response_model=Todo)
|
|
def update_todo(todo_id: int, todo: TodoUpdate, db: Session = Depends(get_db)):
|
|
"""
|
|
Update a todo
|
|
"""
|
|
db_todo = crud.todo.update_todo(db=db, todo_id=todo_id, todo=todo)
|
|
if db_todo is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail=f"Todo with ID {todo_id} not found"
|
|
)
|
|
return db_todo
|
|
|
|
|
|
@router.delete("/{todo_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_todo(todo_id: int, db: Session = Depends(get_db)):
|
|
"""
|
|
Delete a todo
|
|
"""
|
|
success = crud.todo.delete_todo(db=db, todo_id=todo_id)
|
|
if not success:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail=f"Todo with ID {todo_id} not found"
|
|
)
|
|
return None
|