Automated Action 6d123e4b89 Create simple todo app with FastAPI and SQLite
- Set up project structure
- Create database models with SQLAlchemy
- Add Alembic migrations
- Implement CRUD API endpoints
- Update README with documentation

generated with BackendIM... (backend.im)
2025-05-14 01:42:06 +00:00

34 lines
1.1 KiB
Python

from sqlalchemy.orm import Session
from app.models.todo import Todo
from app.schemas.todo import TodoCreate, TodoUpdate
from typing import List, Optional
def get_todos(db: Session, skip: int = 0, limit: int = 100) -> List[Todo]:
return db.query(Todo).offset(skip).limit(limit).all()
def get_todo_by_id(db: Session, todo_id: int) -> Optional[Todo]:
return db.query(Todo).filter(Todo.id == todo_id).first()
def create_todo(db: Session, todo: TodoCreate) -> 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]:
db_todo = get_todo_by_id(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) -> Optional[Todo]:
db_todo = get_todo_by_id(db, todo_id)
if db_todo:
db.delete(db_todo)
db.commit()
return db_todo