Automated Action b0d71975a9 Create simple Todo application with FastAPI and SQLite
- Set up project structure with FastAPI and SQLite
- Created Todo model and database schemas
- Implemented CRUD operations for Todo items
- Added Alembic for database migrations
- Added health check endpoint
- Used Ruff for code linting and formatting
- Updated README with project documentation
2025-05-17 13:05:20 +00:00

69 lines
1.9 KiB
Python

from typing import Optional
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from app.db import crud
from app.db.session import get_db
from app.schemas.todo import Todo, TodoCreate, TodoUpdate
router = APIRouter()
@router.get("/", response_model=list[Todo])
def read_todos(
skip: int = 0,
limit: int = 100,
completed: Optional[bool] = None,
db: Session = Depends(get_db),
):
"""
Retrieve todos.
- Optional filter by completed status
- Optional pagination with skip and limit parameters
"""
todos = crud.get_todos(db, skip=skip, limit=limit, completed=completed)
return todos
@router.post("/", response_model=Todo, status_code=status.HTTP_201_CREATED)
def create_todo(todo: TodoCreate, db: Session = Depends(get_db)):
"""
Create a new todo item.
"""
return crud.create_todo(db=db, todo_create=todo)
@router.get("/{todo_id}", response_model=Todo)
def read_todo(todo_id: int, db: Session = Depends(get_db)):
"""
Get a specific todo by ID.
"""
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
@router.put("/{todo_id}", response_model=Todo)
def update_todo(todo_id: int, todo: TodoUpdate, db: Session = Depends(get_db)):
"""
Update a todo.
"""
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
@router.delete(
"/{todo_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None
)
def delete_todo(todo_id: int, db: Session = Depends(get_db)):
"""
Delete a todo.
"""
deleted = crud.delete_todo(db, todo_id=todo_id)
if not deleted:
raise HTTPException(status_code=404, detail="Todo not found")