
- Implement Todo database model with SQLAlchemy - Set up Alembic for database migrations - Create CRUD operations for Todo items - Implement RESTful API endpoints - Add health check endpoint - Include comprehensive README with usage instructions
115 lines
3.1 KiB
Python
115 lines
3.1 KiB
Python
from typing import List
|
|
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.database import get_db
|
|
from app.models.todo import Todo
|
|
from app.schemas.todo import TodoCreate, TodoRead, TodoUpdate
|
|
from app.crud.todo import create_todo, get_todo, get_todos, update_todo, delete_todo
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/todos", response_model=TodoRead, status_code=status.HTTP_201_CREATED)
|
|
def create_todo_api(todo: TodoCreate, db: Session = Depends(get_db)) -> Todo:
|
|
"""
|
|
Create a new todo item.
|
|
|
|
Args:
|
|
todo: Todo item data
|
|
db: Database session
|
|
|
|
Returns:
|
|
Created todo item
|
|
"""
|
|
return create_todo(db=db, todo=todo)
|
|
|
|
|
|
@router.get("/todos", response_model=List[TodoRead])
|
|
def read_todos(
|
|
skip: int = Query(0, ge=0, description="Number of todos to skip"),
|
|
limit: int = Query(100, ge=1, le=100, description="Maximum number of todos to return"),
|
|
db: Session = Depends(get_db)
|
|
) -> List[Todo]:
|
|
"""
|
|
Get all todo items with pagination.
|
|
|
|
Args:
|
|
skip: Number of records to skip
|
|
limit: Maximum number of records to return
|
|
db: Database session
|
|
|
|
Returns:
|
|
List of todo items
|
|
"""
|
|
return get_todos(db=db, skip=skip, limit=limit)
|
|
|
|
|
|
@router.get("/todos/{todo_id}", response_model=TodoRead)
|
|
def read_todo(todo_id: int, db: Session = Depends(get_db)) -> Todo:
|
|
"""
|
|
Get a specific todo item by ID.
|
|
|
|
Args:
|
|
todo_id: ID of the todo to retrieve
|
|
db: Database session
|
|
|
|
Returns:
|
|
Todo item if found
|
|
|
|
Raises:
|
|
HTTPException: If todo is not found
|
|
"""
|
|
db_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.patch("/todos/{todo_id}", response_model=TodoRead)
|
|
def update_todo_api(todo_id: int, todo: TodoUpdate, db: Session = Depends(get_db)) -> Todo:
|
|
"""
|
|
Update a todo item.
|
|
|
|
Args:
|
|
todo_id: ID of the todo to update
|
|
todo: Updated todo data
|
|
db: Database session
|
|
|
|
Returns:
|
|
Updated todo item
|
|
|
|
Raises:
|
|
HTTPException: If todo is not found
|
|
"""
|
|
db_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("/todos/{todo_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_todo_api(todo_id: int, db: Session = Depends(get_db)) -> None:
|
|
"""
|
|
Delete a todo item.
|
|
|
|
Args:
|
|
todo_id: ID of the todo to delete
|
|
db: Database session
|
|
|
|
Raises:
|
|
HTTPException: If todo is not found
|
|
"""
|
|
success = 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 |