Automated Action 7a43bab909 Add simple todo app backend with FastAPI
- Create FastAPI application with CORS support
- Add SQLAlchemy models for Todo with timestamps
- Set up SQLite database with proper configuration
- Create CRUD operations for todo management
- Add REST API endpoints for all todo operations
- Configure Alembic for database migrations
- Add Pydantic schemas for request/response validation
- Include health check and root info endpoints
- Set up proper project structure following best practices
- Add comprehensive README with usage instructions
2025-06-19 12:10:12 +00:00

72 lines
2.2 KiB
Python

from fastapi import FastAPI, Depends, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy.orm import Session
from typing import List
from app.db.base import engine, Base
from app.db.session import get_db
from app import crud, schemas
# Create database tables
Base.metadata.create_all(bind=engine)
app = FastAPI(
title="Todo App API",
description="A simple todo application API",
version="1.0.0"
)
# CORS configuration
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def root():
return {
"title": "Todo App API",
"documentation": "/docs",
"health": "/health"
}
@app.get("/health")
async def health():
return {"status": "healthy"}
@app.post("/todos/", response_model=schemas.Todo)
def create_todo(todo: schemas.TodoCreate, db: Session = Depends(get_db)):
return crud.create_todo(db=db, todo=todo)
@app.get("/todos/", response_model=List[schemas.Todo])
def read_todos(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
todos = crud.get_todos(db, skip=skip, limit=limit)
return todos
@app.get("/todos/{todo_id}", response_model=schemas.Todo)
def read_todo(todo_id: int, db: Session = Depends(get_db)):
db_todo = crud.get_todo(db, todo_id=todo_id)
if db_todo is None:
raise HTTPException(status_code=404, detail="Todo not found")
return db_todo
@app.put("/todos/{todo_id}", response_model=schemas.Todo)
def update_todo(todo_id: int, todo: schemas.TodoUpdate, db: Session = Depends(get_db)):
db_todo = crud.update_todo(db, todo_id=todo_id, todo_update=todo)
if db_todo is None:
raise HTTPException(status_code=404, detail="Todo not found")
return db_todo
@app.delete("/todos/{todo_id}", response_model=schemas.Todo)
def delete_todo(todo_id: int, db: Session = Depends(get_db)):
db_todo = crud.delete_todo(db, todo_id=todo_id)
if db_todo is None:
raise HTTPException(status_code=404, detail="Todo not found")
return db_todo
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)