
- Set up FastAPI application with CORS support - Created SQLite database configuration with absolute paths - Implemented Todo model with SQLAlchemy - Added full CRUD operations for todos - Created API endpoints with proper error handling - Set up Alembic for database migrations - Added health check and base URL endpoints - Updated README with comprehensive documentation - Configured project structure following best practices Features: - Complete Todo CRUD API - SQLite database with proper path configuration - Database migrations with Alembic - API documentation at /docs and /redoc - Health check endpoint at /health - CORS enabled for all origins - Proper error handling and validation
57 lines
1.9 KiB
Python
57 lines
1.9 KiB
Python
from typing import List
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.db.session import get_db
|
|
from app.db import crud
|
|
from app.models.schemas import TodoCreate, TodoResponse, TodoUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/todos", response_model=List[TodoResponse])
|
|
def get_todos(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
|
"""Get all todos with pagination"""
|
|
todos = crud.get_todos(db, skip=skip, limit=limit)
|
|
return todos
|
|
|
|
|
|
@router.get("/todos/{todo_id}", response_model=TodoResponse)
|
|
def get_todo(todo_id: int, db: Session = Depends(get_db)):
|
|
"""Get a specific todo by ID"""
|
|
todo = crud.get_todo(db, todo_id=todo_id)
|
|
if todo is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Todo not found"
|
|
)
|
|
return todo
|
|
|
|
|
|
@router.post("/todos", response_model=TodoResponse, status_code=status.HTTP_201_CREATED)
|
|
def create_todo(todo: TodoCreate, db: Session = Depends(get_db)):
|
|
"""Create a new todo"""
|
|
return crud.create_todo(db=db, todo=todo)
|
|
|
|
|
|
@router.put("/todos/{todo_id}", response_model=TodoResponse)
|
|
def update_todo(todo_id: int, todo_update: TodoUpdate, db: Session = Depends(get_db)):
|
|
"""Update an existing todo"""
|
|
todo = crud.update_todo(db, todo_id=todo_id, todo_update=todo_update)
|
|
if todo is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Todo not found"
|
|
)
|
|
return todo
|
|
|
|
|
|
@router.delete("/todos/{todo_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
def delete_todo(todo_id: int, db: Session = Depends(get_db)):
|
|
"""Delete a todo"""
|
|
success = crud.delete_todo(db, todo_id=todo_id)
|
|
if not success:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Todo not found"
|
|
) |