
- Created FastAPI application with Todo CRUD operations - Implemented GET /api/v1/todos/ for listing todos with pagination - Implemented POST /api/v1/todos/ for creating new todos - Implemented GET /api/v1/todos/{id} for retrieving specific todos - Implemented PUT /api/v1/todos/{id} for updating todos - Implemented DELETE /api/v1/todos/{id} for deleting todos - Added proper error handling with 404 responses - Configured SQLAlchemy with SQLite database - Set up Alembic for database migrations - Added Pydantic schemas for request/response validation - Enabled CORS for all origins - Added health check endpoint at /health - Updated README with complete API documentation
88 lines
2.5 KiB
Python
88 lines
2.5 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
|
|
from app.db.session import get_db
|
|
from app.models.todo import Todo
|
|
from app.schemas.todo import TodoCreate, TodoUpdate, TodoResponse
|
|
|
|
router = APIRouter(prefix="/todos", tags=["todos"])
|
|
|
|
|
|
@router.get("/", response_model=List[TodoResponse])
|
|
def get_todos(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
|
"""
|
|
Retrieve all todos with optional pagination.
|
|
"""
|
|
todos = db.query(Todo).offset(skip).limit(limit).all()
|
|
return todos
|
|
|
|
|
|
@router.post("/", response_model=TodoResponse, status_code=status.HTTP_201_CREATED)
|
|
def create_todo(todo: TodoCreate, db: Session = Depends(get_db)):
|
|
"""
|
|
Create a new todo item.
|
|
"""
|
|
db_todo = Todo(
|
|
title=todo.title,
|
|
description=todo.description,
|
|
completed=todo.completed
|
|
)
|
|
db.add(db_todo)
|
|
db.commit()
|
|
db.refresh(db_todo)
|
|
return db_todo
|
|
|
|
|
|
@router.get("/{todo_id}", response_model=TodoResponse)
|
|
def get_todo(todo_id: int, db: Session = Depends(get_db)):
|
|
"""
|
|
Retrieve a specific todo by ID.
|
|
"""
|
|
todo = db.query(Todo).filter(Todo.id == todo_id).first()
|
|
if todo is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Todo with id {todo_id} not found"
|
|
)
|
|
return todo
|
|
|
|
|
|
@router.put("/{todo_id}", response_model=TodoResponse)
|
|
def update_todo(todo_id: int, todo_update: TodoUpdate, db: Session = Depends(get_db)):
|
|
"""
|
|
Update a specific todo by ID.
|
|
"""
|
|
todo = db.query(Todo).filter(Todo.id == todo_id).first()
|
|
if todo is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Todo with id {todo_id} not found"
|
|
)
|
|
|
|
# Update only the fields that are provided
|
|
update_data = todo_update.model_dump(exclude_unset=True)
|
|
|
|
for field, value in update_data.items():
|
|
setattr(todo, field, value)
|
|
|
|
db.commit()
|
|
db.refresh(todo)
|
|
return todo
|
|
|
|
|
|
@router.delete("/{todo_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
def delete_todo(todo_id: int, db: Session = Depends(get_db)):
|
|
"""
|
|
Delete a specific todo by ID.
|
|
"""
|
|
todo = db.query(Todo).filter(Todo.id == todo_id).first()
|
|
if todo is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Todo with id {todo_id} not found"
|
|
)
|
|
|
|
db.delete(todo)
|
|
db.commit()
|
|
return None |