Automated Action 8a1b373ff6 feat: Implement Todo API with FastAPI and SQLite
- Setup project structure and dependencies
- Create Todo model and SQLAlchemy database connection
- Set up Alembic for database migrations
- Implement CRUD operations and API endpoints
- Add health check endpoint
- Update README with project documentation

generated with BackendIM... (backend.im)
2025-05-12 10:11:52 +00:00

45 lines
1.3 KiB
Python

from sqlalchemy.orm import Session
from typing import List, Optional
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 todos with pagination."""
return db.query(Todo).order_by(Todo.created_at.desc()).offset(skip).limit(limit).all()
def get_todo(db: Session, todo_id: int) -> Optional[Todo]:
"""Get a single todo by ID."""
return db.query(Todo).filter(Todo.id == todo_id).first()
def create_todo(db: Session, todo: TodoCreate) -> Todo:
"""Create a new todo."""
db_todo = Todo(**todo.dict())
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 by ID."""
db_todo = get_todo(db, todo_id)
if db_todo:
update_data = todo.dict(exclude_unset=True)
for key, value in update_data.items():
setattr(db_todo, key, value)
db.commit()
db.refresh(db_todo)
return db_todo
def delete_todo(db: Session, todo_id: int) -> bool:
"""Delete a todo by ID."""
db_todo = get_todo(db, todo_id)
if db_todo:
db.delete(db_todo)
db.commit()
return True
return False