Automated Action 2204ae214d Create a simple Todo app with FastAPI and SQLite
- Set up project structure and FastAPI application
- Create Todo database model with SQLAlchemy
- Configure Alembic for database migrations
- Implement CRUD endpoints for managing Todo items
- Add health check endpoint
- Include comprehensive documentation in README.md
- Configure and apply Ruff linting
2025-05-19 13:36:18 +00:00

63 lines
1.4 KiB
Python

from typing import List, Optional
from sqlalchemy.orm import Session
from app.models.todo import Todo
from app.schemas.todo import TodoCreate, TodoUpdate
def get_todos(db: Session, skip: int = 0, limit: int = 100) -> List[Todo]:
"""
Get all todo items with pagination
"""
return db.query(Todo).offset(skip).limit(limit).all()
def get_todo(db: Session, todo_id: int) -> Optional[Todo]:
"""
Get a specific todo item by ID
"""
return db.query(Todo).filter(Todo.id == todo_id).first()
def create_todo(db: Session, todo: TodoCreate) -> Todo:
"""
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
def update_todo(db: Session, todo_id: int, todo: TodoUpdate) -> Optional[Todo]:
"""
Update a todo item
"""
db_todo = get_todo(db, todo_id)
if not db_todo:
return None
update_data = todo.model_dump(exclude_unset=True)
for field, value in update_data.items():
setattr(db_todo, field, value)
db.commit()
db.refresh(db_todo)
return db_todo
def delete_todo(db: Session, todo_id: int) -> bool:
"""
Delete a todo item
"""
db_todo = get_todo(db, todo_id)
if not db_todo:
return False
db.delete(db_todo)
db.commit()
return True