Automated Action af063c97fc Create Todo app with FastAPI and SQLite
- Set up project structure with FastAPI and SQLAlchemy
- Create Todo model with CRUD operations
- Implement database migrations with Alembic
- Add health endpoint and API documentation
- Create comprehensive README

generated with BackendIM... (backend.im)
2025-05-13 17:36:33 +00:00

66 lines
1.7 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, status, Response
from sqlalchemy.orm import Session
from typing import List
from app.db.database import get_db
from app.schemas.todo import Todo, TodoCreate, TodoUpdate
from app.services.todo import (
get_todos,
get_todo,
create_todo,
update_todo,
delete_todo,
)
todo_router = APIRouter()
health_router = APIRouter()
@todo_router.get("/todos/", response_model=List[Todo])
def read_todos(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
"""
Retrieve all todos with pagination support.
"""
todos = get_todos(db, skip=skip, limit=limit)
return todos
@todo_router.get("/todos/{todo_id}", response_model=Todo)
def read_todo(todo_id: int, db: Session = Depends(get_db)):
"""
Retrieve a specific todo by ID.
"""
return get_todo(db, todo_id)
@todo_router.post("/todos/", response_model=Todo, status_code=status.HTTP_201_CREATED)
def create_todo_endpoint(todo: TodoCreate, db: Session = Depends(get_db)):
"""
Create a new todo.
"""
return create_todo(db, todo)
@todo_router.put("/todos/{todo_id}", response_model=Todo)
def update_todo_endpoint(todo_id: int, todo: TodoUpdate, db: Session = Depends(get_db)):
"""
Update an existing todo.
"""
return update_todo(db, todo_id, todo)
@todo_router.delete("/todos/{todo_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_todo_endpoint(todo_id: int, db: Session = Depends(get_db)):
"""
Delete a todo.
"""
delete_todo(db, todo_id)
return Response(status_code=status.HTTP_204_NO_CONTENT)
@health_router.get("/health")
def health_check():
"""
Check the health of the application.
"""
return {"status": "healthy", "message": "Todo API is running"}