Automated Action 0976677a4c Build Todo application with FastAPI and SQLite
- Set up project structure with FastAPI and SQLAlchemy
- Create Todo model, schemas, and CRUD operations
- Add API endpoints for todo operations (create, read, update, delete)
- Set up Alembic for database migrations
- Add health check endpoint
- Update README with detailed instructions

generated with BackendIM... (backend.im)
2025-05-13 12:38:16 +00:00

41 lines
1.2 KiB
Python

from sqlalchemy.orm import Session
from typing import List, Optional
from . import models, schemas
def get_todo(db: Session, todo_id: int):
return db.query(models.Todo).filter(models.Todo.id == todo_id).first()
def get_todos(db: Session, skip: int = 0, limit: int = 100, completed: Optional[bool] = None):
query = db.query(models.Todo)
if completed is not None:
query = query.filter(models.Todo.completed == completed)
return query.order_by(models.Todo.created_at.desc()).offset(skip).limit(limit).all()
def create_todo(db: Session, todo: schemas.TodoCreate):
db_todo = models.Todo(
title=todo.title,
description=todo.description
)
db.add(db_todo)
db.commit()
db.refresh(db_todo)
return db_todo
def update_todo(db: Session, todo_id: int, todo: schemas.TodoUpdate):
db_todo = get_todo(db, todo_id)
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):
db_todo = get_todo(db, todo_id)
db.delete(db_todo)
db.commit()
return db_todo