Automated Action 168878cd6f Create Simple Todo Application with FastAPI and SQLite
- Set up project structure and dependencies
- Implement Todo model with SQLAlchemy
- Configure SQLite database connection
- Create Alembic migration scripts
- Implement RESTful API endpoints for CRUD operations
- Add health check endpoint
- Update README with documentation

generated with BackendIM... (backend.im)
2025-05-13 02:14:09 +00:00

41 lines
1.2 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_todo(db: Session, todo_id: int) -> Optional[Todo]:
return db.query(Todo).filter(Todo.id == todo_id).first()
def get_todos(db: Session, skip: int = 0, limit: int = 100) -> List[Todo]:
return db.query(Todo).offset(skip).limit(limit).all()
def create_todo(db: Session, todo: TodoCreate) -> Todo:
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]:
db_todo = get_todo(db, todo_id)
if not db_todo:
return None
update_data = todo.model_dump(exclude_unset=True)
for key, value in update_data.items():
setattr(db_todo, key, value)
db.add(db_todo)
db.commit()
db.refresh(db_todo)
return db_todo
def delete_todo(db: Session, todo_id: int) -> bool:
db_todo = get_todo(db, todo_id)
if not db_todo:
return False
db.delete(db_todo)
db.commit()
return True